Skip to content

Commit

Permalink
Fix getting labelIds of a message
Browse files Browse the repository at this point in the history
  • Loading branch information
crabvk committed Nov 19, 2023
1 parent 1a01823 commit ca33c40
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
22 changes: 10 additions & 12 deletions bar_gmail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ def cli():
if args.color is not None and args.format != 'polybar':
parser.error('`--color COLOR` can be used only with `--format polybar`.')

BASE_DIR = Path(__file__).resolve().parent
CLIENT_SECRETS_PATH = Path(BASE_DIR, 'client_secrets.json')
CACHE_DIR = Path(Path.home(), '.cache/bar-gmail')
CREDENTIALS_PATH = Path(CACHE_DIR, args.credentials)
SESSION_PATH = Path(CACHE_DIR, 'session.json')
base_dir = Path(__file__).resolve().parent
client_secrets_path = Path(base_dir, 'client_secrets.json')
cache_dir = Path(Path.home(), '.cache/bar-gmail')
credentials_path = Path(cache_dir, args.credentials)
session_path = Path(cache_dir, 'session.json')

if not CACHE_DIR.is_dir():
CACHE_DIR.mkdir(exist_ok=True)
if not cache_dir.is_dir():
cache_dir.mkdir(exist_ok=True)

gmail = Gmail(CLIENT_SECRETS_PATH, CREDENTIALS_PATH)
gmail = Gmail(client_secrets_path, credentials_path)

if args.subcommand == 'auth':
if gmail.authenticate():
print('Authenticated successfully.')
exit()

if not CREDENTIALS_PATH.is_file():
if not credentials_path.is_file():
print('Credentials not found. Run `bar-gmail auth` for authentication.', file=sys.stderr)
exit(1)

Expand All @@ -64,9 +64,7 @@ def cli():
elif args.format == 'polybar':
printer = PolybarPrinter(badge=args.badge, color=args.color)

app = Application(SESSION_PATH, gmail, printer,
badge=args.badge,
color=args.color,
app = Application(session_path, gmail, printer,
label=args.label,
sound_id=args.sound,
urgency_level=UrgencyLevel[args.urgency.upper()],
Expand Down
15 changes: 7 additions & 8 deletions bar_gmail/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from gi.repository import GLib
from dasbus.connection import SessionMessageBus
from dasbus.error import DBusError
from bar_gmail.gmail import Gmail
from bar_gmail.printer import WaybarPrinter, PolybarPrinter
from google.auth.exceptions import TransportError
from googleapiclient.errors import HttpError
from bar_gmail.gmail import Gmail
from bar_gmail.printer import WaybarPrinter, PolybarPrinter

APP_NAME = 'Bar Gmail'
NOTIFICATION_CATEGORY = 'email.arrived'
Expand All @@ -26,18 +26,16 @@ class UrgencyLevel(Enum):

class Application:
def __init__(self, session_path: Path, gmail: Gmail, printer: WaybarPrinter | PolybarPrinter,
badge: str, color: str | None, label: str, sound_id: str,
urgency_level: UrgencyLevel, expire_timeout: int, is_notify: bool):
label: str, sound_id: str, urgency_level: UrgencyLevel, expire_timeout: int,
is_notify: bool):
self.session_path = session_path
self.gmail = gmail
self.printer = printer
self.badge = badge
self.label = label
self.sound_id = sound_id
self.urgency_level = urgency_level
self.expire_timeout = expire_timeout
self.is_notify = is_notify
self.color = color

@staticmethod
def _is_innacurate(since: float) -> bool:
Expand Down Expand Up @@ -89,7 +87,7 @@ def run(self):

try:
unread = self.gmail.get_unread_messages_count(self.label)
if unread != session['unread'] or inaccurate == True:
if unread != session['unread'] or inaccurate is True:
self.printer.print(unread)
history_id = session['history_id'] or self.gmail.get_latest_history_id()
session = {
Expand All @@ -105,7 +103,8 @@ def run(self):
if any(history['messages']):
if self.sound_id:
self._play_sound()
self._send_notifications(history['messages'])
if self.is_notify:
self._send_notifications(history['messages'])
session['history_id'] = history['history_id']
with open(self.session_path, 'w') as f:
json.dump(session, f)
Expand Down
2 changes: 1 addition & 1 deletion bar_gmail/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_history_since(self, history_id: str) -> dict:
historyTypes=['messageAdded']).execute()
for record in history.get('history', []):
for message in record.get('messagesAdded', []):
if 'UNREAD' not in message['message']['labelIds']:
if 'UNREAD' not in message['message'].get('labelIds', []):
continue
message_id = message['message']['id']
try:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "bar-gmail"
version = "1.0.3"
version = "1.0.4"
description = "Get notifications and unread messages count from Gmail (Waybar/Polybar module)"
license = "MIT"
readme = "README.md"
Expand Down

0 comments on commit ca33c40

Please sign in to comment.