From e6d449493c35cb037a02bbcb1dca2c29711c2aa0 Mon Sep 17 00:00:00 2001 From: Syfi Malik Date: Mon, 20 Aug 2018 00:06:35 +0500 Subject: [PATCH] added gmail module to polybar --- .config/polybar/gmail/README.md | 27 ++++++++++++++++ .config/polybar/gmail/auth.py | 28 ++++++++++++++++ .config/polybar/gmail/launch.py | 11 +++++++ .config/polybar/gmail/run.py | 52 ++++++++++++++++++++++++++++++ .config/polybar/gmail/secrets.json | 11 +++++++ 5 files changed, 129 insertions(+) create mode 100644 .config/polybar/gmail/README.md create mode 100755 .config/polybar/gmail/auth.py create mode 100755 .config/polybar/gmail/launch.py create mode 100755 .config/polybar/gmail/run.py create mode 100644 .config/polybar/gmail/secrets.json diff --git a/.config/polybar/gmail/README.md b/.config/polybar/gmail/README.md new file mode 100644 index 00000000..b5d6a455 --- /dev/null +++ b/.config/polybar/gmail/README.md @@ -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. diff --git a/.config/polybar/gmail/auth.py b/.config/polybar/gmail/auth.py new file mode 100755 index 00000000..4c345e83 --- /dev/null +++ b/.config/polybar/gmail/auth.py @@ -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') + diff --git a/.config/polybar/gmail/launch.py b/.config/polybar/gmail/launch.py new file mode 100755 index 00000000..0e65af99 --- /dev/null +++ b/.config/polybar/gmail/launch.py @@ -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('') + diff --git a/.config/polybar/gmail/run.py b/.config/polybar/gmail/run.py new file mode 100755 index 00000000..5648f4bd --- /dev/null +++ b/.config/polybar/gmail/run.py @@ -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) + + + diff --git a/.config/polybar/gmail/secrets.json b/.config/polybar/gmail/secrets.json new file mode 100644 index 00000000..077f20bd --- /dev/null +++ b/.config/polybar/gmail/secrets.json @@ -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"] + } +}