i3-gaps, polybar
This commit is contained in:
28
.config/polybar/polybar-gmail/auth.py
Executable file
28
.config/polybar/polybar-gmail/auth.py
Executable file
@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os
|
||||
import pathlib
|
||||
import httplib2
|
||||
import webbrowser
|
||||
from oauth2client import client, file
|
||||
|
||||
SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'
|
||||
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
|
||||
DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
CLIENT_SECRETS_PATH = os.path.join(DIR, 'client_secrets.json')
|
||||
CREDENTIALS_PATH = os.path.join(DIR, 'credentials.json')
|
||||
storage = file.Storage(CREDENTIALS_PATH)
|
||||
|
||||
if pathlib.Path(CREDENTIALS_PATH).is_file():
|
||||
credentials = storage.get()
|
||||
credentials.refresh(httplib2.Http())
|
||||
print('Credentials successfully refreshed')
|
||||
else:
|
||||
flow = client.flow_from_clientsecrets(CLIENT_SECRETS_PATH, scope=SCOPE,
|
||||
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 successfully created')
|
58
.config/polybar/polybar-gmail/launch.py
Executable file
58
.config/polybar/polybar-gmail/launch.py
Executable file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import time
|
||||
import argparse
|
||||
from apiclient import discovery, errors
|
||||
from oauth2client import client, file
|
||||
from httplib2 import ServerNotFoundError
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-p', '--prefix', default='\uf0e0')
|
||||
parser.add_argument('-c', '--color', default='#e06c75')
|
||||
parser.add_argument('-ns', '--nosound', action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
CREDENTIALS_PATH = os.path.join(DIR, 'credentials.json')
|
||||
|
||||
unread_prefix = '%{F' + args.color + '}' + args.prefix + ' %{F-}'
|
||||
error_prefix = '%{F' + args.color + '}\uf06a %{F-}'
|
||||
count_was = 0
|
||||
|
||||
def print_count(count, is_odd=False):
|
||||
tilde = '~' if is_odd else ''
|
||||
output = ''
|
||||
if count > 0:
|
||||
output = unread_prefix + tilde + str(count)
|
||||
else:
|
||||
output = (args.prefix + ' ' + tilde).strip()
|
||||
print(output, flush=True)
|
||||
|
||||
def update_count(count_was):
|
||||
gmail = discovery.build('gmail', 'v1', credentials=file.Storage(CREDENTIALS_PATH).get())
|
||||
labels = gmail.users().labels().get(userId='me', id='INBOX').execute()
|
||||
count = labels['messagesUnread']
|
||||
print_count(count)
|
||||
if not args.nosound and count_was < count and count > 0:
|
||||
subprocess.run(['canberra-gtk-play', '-i', 'message'])
|
||||
return count
|
||||
|
||||
print_count(0, True)
|
||||
|
||||
while True:
|
||||
try:
|
||||
if pathlib.Path(CREDENTIALS_PATH).is_file():
|
||||
count_was = update_count(count_was)
|
||||
time.sleep(10)
|
||||
else:
|
||||
print(error_prefix + 'credentials not found', flush=True)
|
||||
time.sleep(2)
|
||||
except (errors.HttpError, ServerNotFoundError, OSError) as error:
|
||||
print_count(count_was, True)
|
||||
time.sleep(5)
|
||||
except client.AccessTokenRefreshError:
|
||||
print(error_prefix + 'revoked/expired credentials', flush=True)
|
||||
time.sleep(5)
|
BIN
.config/polybar/polybar-gmail/preview.png
Normal file
BIN
.config/polybar/polybar-gmail/preview.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 497 B |
56
.config/polybar/polybar-gmail/readme.md
Normal file
56
.config/polybar/polybar-gmail/readme.md
Normal file
@ -0,0 +1,56 @@
|
||||
# Polybar Gmail
|
||||
|
||||
A [Polybar](https://github.com/jaagr/polybar) module to show unread messages from Gmail.
|
||||
|
||||

|
||||
|
||||
## Dependencies
|
||||
|
||||
```sh
|
||||
sudo pip install --upgrade oauth2client google-api-python-client
|
||||
```
|
||||
|
||||
**Font Awesome** - for email badge
|
||||
|
||||
**canberra-gtk-play** - for new email sound
|
||||
|
||||
You can change the badge or turn off sound, for more info see [script arguments](#script-arguments)
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
cd ~/.config/polybar
|
||||
curl -LO https://github.com/vyachkonovalov/polybar-gmail/archive/master.tar.gz
|
||||
tar zxf master.tar.gz && rm master.tar.gz
|
||||
mv polybar-gmail-master gmail
|
||||
```
|
||||
|
||||
and obtain/refresh credentials
|
||||
|
||||
```sh
|
||||
~/.config/polybar/gmail/auth.py
|
||||
```
|
||||
|
||||
### Module
|
||||
|
||||
```ini
|
||||
[module/gmail]
|
||||
type = custom/script
|
||||
exec = ~/.config/polybar/gmail/launch.py
|
||||
tail = true
|
||||
click-left = xdg-open https://mail.google.com
|
||||
```
|
||||
|
||||
## Script arguments
|
||||
|
||||
`-p` or `--prefix` - to change email badge, default:
|
||||
|
||||
`-c` or `--color` - to change new email badge color, default: #e06c75
|
||||
|
||||
`-ns` or `--nosound` - turn off new email sound
|
||||
|
||||
### Example
|
||||
|
||||
```sh
|
||||
launch.py --prefix '📧' --color '#be5046' --nosound
|
||||
```
|
Reference in New Issue
Block a user