Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
labsync-tray
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alberto LIVIO BECCARIA
labsync-tray
Commits
f6224111
Commit
f6224111
authored
5 years ago
by
Alberto LIVIO BECCARIA
Browse files
Options
Downloads
Patches
Plain Diff
Added python example for gmail notifications
parent
7e8e3f7f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
snippets/notify.py
+100
-0
100 additions, 0 deletions
snippets/notify.py
with
100 additions
and
0 deletions
snippets/notify.py
0 → 100755
+
100
−
0
View file @
f6224111
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__version__
=
'
0.1
'
import
gi
gi
.
require_version
(
'
Gtk
'
,
'
3.0
'
)
gi
.
require_version
(
'
Notify
'
,
'
0.7
'
)
gi
.
require_version
(
'
AppIndicator3
'
,
'
0.1
'
)
from
gi.repository
import
Notify
,
Gtk
,
GLib
from
urllib.request
import
FancyURLopener
from
datetime
import
datetime
,
date
,
time
,
timedelta
import
os.path
,
sys
,
getopt
from
subprocess
import
call
serialisedvalue
=
0
;
serialiseddate
=
0
;
def
callback
():
call
([
"
chromium
"
,
"
gmail.com
"
])
def
serialise
(
unread
):
try
:
f
=
open
(
"
mailcount
"
,
"
w
"
)
try
:
f
.
write
(
unread
+
"
\n
"
)
# Write a string to a file
f
.
write
(
datetime
.
now
().
strftime
(
'
%b %d %Y %I:%M%p
'
))
finally
:
f
.
close
()
except
IOError
:
pass
def
deserialise
():
global
serialisedvalue
global
serialiseddate
try
:
f
=
open
(
"
mailcount
"
,
"
r
"
)
try
:
serialisedvalue
=
f
.
readline
().
rstrip
()
serialiseddate
=
datetime
.
strptime
(
f
.
readline
(),
'
%b %d %Y %I:%M%p
'
)
finally
:
f
.
close
()
except
IOError
:
pass
def
notif
(
unread
):
Notify
.
init
(
"
New Mail
"
)
if
unread
!=
"
1
"
:
Hello
=
Notify
.
Notification
.
new
(
"
New mail
"
,
"
You have
"
+
unread
+
"
unread mails
"
,
"
/usr/share/icons/Faenza/actions/96/mail-forward.png
"
)
else
:
Hello
=
Notify
.
Notification
.
new
(
"
New mail
"
,
"
You have
"
+
unread
+
"
unread mails
"
,
"
/usr/share/icons/Faenza/actions/96/mail-forward.png
"
)
Hello
.
add_action
(
'
action
'
,
'
Read
'
,
callback
,
None
,
None
)
Hello
.
show
()
def
main
(
argv
):
notify
=
0
forced
=
0
try
:
opts
,
args
=
getopt
.
getopt
(
argv
,
"
nf
"
,[
'
notify
'
,
'
force-notify
'
])
except
getopt
.
GetoptError
:
print
(
"
unreadgmail.py [-n --notify] [-f --force-notify
"
)
sys
.
exit
(
2
)
for
opt
,
args
in
opts
:
if
opt
in
(
"
-n
"
,
"
--notify
"
):
notify
=
1
elif
opt
in
(
"
-f
"
,
"
--force-notify
"
):
forced
=
1
#url = 'https://%s:%s@mail.google.com/mail/feed/atom' % ("myaccount", "mypassword")
url
=
'
https://%s:%s@mail.google.com/mail/feed/atom
'
%
(
"
ulisse.alb
"
,
"
oklahoma:2014
"
)
opener
=
FancyURLopener
()
page
=
opener
.
open
(
url
)
contents
=
page
.
read
().
decode
(
'
utf-8
'
)
ifrom
=
contents
.
index
(
'
<fullcount>
'
)
+
11
ito
=
contents
.
index
(
'
</fullcount>
'
)
unread
=
contents
[
ifrom
:
ito
]
print
(
"
Unread messages :
"
+
unread
)
if
notify
==
1
and
forced
==
0
:
if
os
.
path
.
exists
(
"
mailcount
"
):
deserialise
()
else
:
serialise
(
unread
)
deserialise
()
if
unread
!=
"
0
"
:
if
unread
!=
serialisedvalue
:
notif
(
unread
)
serialise
(
unread
)
elif
((
datetime
.
now
()
-
serialiseddate
)
>
timedelta
(
hours
=
1
)):
notif
(
unread
)
if
forced
==
1
:
notif
(
unread
)
GLib
.
MainLoop
().
run
()
if
__name__
==
"
__main__
"
:
main
(
sys
.
argv
[
1
:])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment