diff --git a/bar_gmail/__init__.py b/bar_gmail/__init__.py index c29cc50..db05ff5 100644 --- a/bar_gmail/__init__.py +++ b/bar_gmail/__init__.py @@ -27,6 +27,8 @@ def cli(): help='The duration, in milliseconds, for the notification to appear on screen.') parser.add_argument('-dn', '--no-notify', action='store_true', help='Disable new email notifications.') + parser.add_argument('-sm', '--spam', action='store_true', + help='Enable new spam email notifications.') parser.add_argument('-cr', '--credentials', default='credentials.json', help="Path to the credentials file, defaults to 'credentials.json'.") args = parser.parse_args() @@ -43,7 +45,7 @@ def cli(): 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, include_spam=args.spam) if args.subcommand == 'auth': if gmail.authenticate(): diff --git a/bar_gmail/gmail.py b/bar_gmail/gmail.py index 64e9099..78c68d9 100644 --- a/bar_gmail/gmail.py +++ b/bar_gmail/gmail.py @@ -9,9 +9,10 @@ class Gmail(): - def __init__(self, client_secrets_path: Path, credentials_path: Path): + def __init__(self, client_secrets_path: Path, credentials_path: Path, include_spam: bool): self.client_secrets_path = client_secrets_path self.credentials_path = credentials_path + self.include_spam = include_spam self._credentials = None self._gmail = None @@ -36,6 +37,15 @@ def _filter_headers(headers: list) -> dict: result[name] = header['value'] return result + def _check_label_ids(self, label_ids: list[str]) -> bool: + if 'UNREAD' in label_ids: + if self.include_spam: + return True + else: + return 'SPAM' not in label_ids + else: + return False + def authenticate(self) -> bool: flow = InstalledAppFlow.from_client_secrets_file(self.client_secrets_path, scopes=SCOPE) creds = flow.run_local_server(open_browser=False) @@ -61,7 +71,8 @@ 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'].get('labelIds', []): + label_ids = message['message'].get('labelIds', []) + if not self._check_label_ids(label_ids): continue message_id = message['message']['id'] try: diff --git a/pyproject.toml b/pyproject.toml index 66a720d..8c4b307 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "bar-gmail" -version = "1.0.4" +version = "1.0.5" description = "Get notifications and unread messages count from Gmail (Waybar/Polybar module)" license = "MIT" readme = "README.md"