Skip to content

Commit

Permalink
Ignore spam emails by default (fix #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
crabvk committed Dec 2, 2023
1 parent ca33c40 commit b989131
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 3 additions & 1 deletion bar_gmail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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():
Expand Down
15 changes: 13 additions & 2 deletions bar_gmail/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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:
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.4"
version = "1.0.5"
description = "Get notifications and unread messages count from Gmail (Waybar/Polybar module)"
license = "MIT"
readme = "README.md"
Expand Down

0 comments on commit b989131

Please sign in to comment.