added gmail module to polybar

This commit is contained in:
Syfi Malik 2018-08-20 00:06:35 +05:00
parent 0f6c5d5a30
commit e6d449493c
No known key found for this signature in database
GPG Key ID: 75C8C095D0730312
5 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,27 @@
# Gmail Notifications
![image](https://user-images.githubusercontent.com/36492651/44306373-08f5ea00-a3a7-11e8-9347-8a84bcb4efab.png)
I use [Polybar](https://github.com/jaagr/polybar) and use this repo as module to it for getting notified about my gmail.
## Installation
```
git clone https://github.com/SyfiMalik/gmail-notifications.git ~/.config/polybar/
mv ~/.config/polybar/gmail-notifications ~/.config/polybar/gmail
cd ~/.config/polybar/gmail
./run.py
```
You'll be prompted to login to your gmail, login there, allow gmail4polybar and you'll be given a code to enter in the terminal. Enter the code and you're good to go. Now follow below steps to recieve gmail notifications in the tray.
Add these lines to your polybar main config.
```ini
[module/gmail]
type = custom/script
exec = ~/.config/polybar/gmail/launch.py
tail = true
click-left = xdg-open https://mail.google.com
```
and yeah, don't forget to enable 'gmail' module.

28
.config/polybar/gmail/auth.py Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env python
import pathlib
import os
import webbrowser
import httplib2
from oauth2client import client, file
target = 'https://www.googleapis.com/auth/gmail.readonly'
redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
dir = os.path.dirname(os.path.realpath(__file__))
secrets = os.path.join(dir, 'secrets.json')
credentials = os.path.join(dir, 'mycredentials.json')
storage = file.Storage(credentials)
if pathlib.Path(credentials).is_file():
credentials = storage.get()
credentials.refresh(httplib2.Http())
print("credentials refreshed")
else:
flow = client.flow_from_clientsecrets(secrets, scope=target,redirect_uri=redirect_uri)
auth_uri = flow.step1_get_authorize_url()
webbrowser.open(auth_uri)
auth_code = input('Enter the auth code: ')
credentials = flow.step2_exchange(auth_code)
storage.put(credentials)
print('Credentials created')

11
.config/polybar/gmail/launch.py Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env python
import subprocess, os
s = subprocess.check_output("nmcli networking connectivity check", shell=True)
if b'full' in s:
os.system('~/.config/polybar/gmail/run.py')
if b'limited' in s:
print('')

52
.config/polybar/gmail/run.py Executable file
View File

@ -0,0 +1,52 @@
#!/usr/bin/env python
import os
import subprocess
import argparse
import time
import pathlib
from apiclient import discovery, errors
from httplib2 import ServerNotFoundError
from oauth2client import client, file
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--badge', default='\uf0e0')
parser.add_argument('-c', '--color', default='#ff69b4')
parser.add_argument('-m', '--mute', action='store_true')
args = parser.parse_args()
dir = os.path.dirname(os.path.realpath(__file__))
credentials = os.path.join(dir, 'mycredentials.json')
unread_badge = '%{F' + args.color + '}' + args.badge + ' %{F-}'
error_badge = '%{F' + args.color + '}\uf06a %{F-}'
count_was = 0
def update(count_was):
gmail = discovery.build('gmail', 'v1', credentials=file.Storage(credentials).get())
labels = gmail.users().labels().get(userId='me', id='INBOX').execute()
count = labels['messagesUnread']
if count > 0:
print(unread_badge + str(count), flush=True)
else:
print(args.badge, flush=True)
if not args.mute and count_was < count and count > 0:
subprocess.run(['canberra-gtk-play', '-f', '/home/z/bin/music4scripts/tora-yara.ogg'])
return count
while True:
try:
if pathlib.Path(credentials).is_file():
count_was = update(count_was)
time.sleep(10)
else:
print(error_badge + 'credentials not found', flush=True)
time.sleep(2)
except (errors.HttpError, ServerNotFoundError, OSError) as error:
print(error_badge + str(error), flush=True)
time.sleep(5)
except client.AccessTokenRefreshError:
print(error_badge + 'revoked credentials', flush=True)
time.sleep(5)

View File

@ -0,0 +1,11 @@
{
"installed": {
"client_id": "1066219363266-ghk8nofj3vkupjqcl2bjpku4e6khdpbl.apps.googleusercontent.com",
"project_id": "gmail4polybar",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "2I-MLoVvR5f6jIErK8Y6aNl0",
"redirect_uris": ["urn:ietf:wg:oauth:2.0:oob"]
}
}