|
1 | | -import pickle, os.path, sys, logging |
| 1 | +import pickle |
| 2 | +import os.path |
| 3 | +import sys |
| 4 | +import logging |
2 | 5 | from logging.handlers import RotatingFileHandler |
3 | 6 | from googleapiclient.discovery import build |
4 | 7 | from google_auth_oauthlib.flow import InstalledAppFlow |
5 | 8 | from google.auth.transport.requests import Request |
6 | | -import base64, email |
| 9 | +import base64 |
| 10 | +import email |
7 | 11 | from email import parser |
8 | 12 | from email import policy |
9 | | -import patterns, constants, secrets |
| 13 | +import patterns |
| 14 | +import constants |
| 15 | +import secrets |
10 | 16 | from secrets import LINE_TOKEN |
11 | 17 | from line_notify import LineNotify |
12 | 18 |
|
|
19 | 25 | 'https://www.googleapis.com/auth/gmail.labels'] |
20 | 26 |
|
21 | 27 |
|
22 | | -def setupLogging(): |
| 28 | +def setup_logging(): |
23 | 29 | # supress google discovery_cache logging |
24 | 30 | # https://github.com/googleapis/google-api-python-client/issues/299 |
25 | 31 | logging.getLogger('googleapiclient.discovery_cache').setLevel( |
26 | 32 | logging.ERROR) |
27 | 33 | logger = logging.getLogger("gmail-notifier") |
28 | | - formatter = logging.Formatter( |
29 | | - '%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
30 | | - ) |
31 | | - # warning_handler = logging.FileHandler("gnotifier.log") |
32 | | - # warning_handler.setLevel(logging.WARNING) |
33 | | - # warning_handler.setFormatter(formatter) |
34 | | - # info_handler = logging.FileHandler("gnotifier.log") |
35 | | - # info_handler.setLevel(logging.INFO) |
36 | | - # info_handler.setFormatter(formatter) |
37 | | - # logger.addHandler(warning_handler) |
38 | | - # logger.addHandler(info_handler) |
39 | 34 | logging.basicConfig( |
40 | 35 | handlers=[RotatingFileHandler(LOG_FILE, |
41 | 36 | maxBytes=100000, |
@@ -209,18 +204,16 @@ def handle_each_email(service, message_id, logger) -> tuple: |
209 | 204 | logger.warning(f"Failed to process message_id: {message_id} " |
210 | 205 | f"Matched Subject: {subject} " |
211 | 206 | f"Sender not matched: {sender}") |
212 | | - pass |
213 | | - return notifier, data |
214 | | - else: |
215 | | - # Not an expected subject line. Ignore this email |
216 | | - logger.info("This is not a notifcation.") |
217 | | - logger.info(f"sender: {sender}\n\t") |
218 | | - logger.info(f"subject: {subject}") |
219 | 207 | return notifier, data |
| 208 | + # Not an expected subject line. Ignore this email |
| 209 | + logger.info("This is not a notifcation.") |
| 210 | + logger.info(f"sender: {sender}\n\t") |
| 211 | + logger.info(f"subject: {subject}") |
| 212 | + return notifier, data |
220 | 213 |
|
221 | 214 |
|
222 | 215 | def main(): |
223 | | - logger = setupLogging() |
| 216 | + logger = setup_logging() |
224 | 217 | logger.info("Looking for email for notification") |
225 | 218 | service = get_service() |
226 | 219 | # Stage 1 Check Connection |
@@ -263,13 +256,13 @@ def main(): |
263 | 256 | # Your custom code goes here. Bot / Line |
264 | 257 | processed = True |
265 | 258 | elif notifier is None and data is not None: |
266 | | - logger.warning(f"Subject matched but From was not matched") |
| 259 | + logger.warning("Subject matched but From was not matched") |
267 | 260 | elif notifier is None and data is None: |
268 | | - logger.info(f"Non-Notification email from expected sender") |
| 261 | + logger.info("Non-Notification email from expected sender") |
269 | 262 | else: |
270 | 263 | # We should not get here. But log it. |
271 | | - logger.warning(f"Something went wrong. Unexpected match. " |
272 | | - f"Dont know how to handle data." |
| 264 | + logger.warning("Something went wrong. Unexpected match. " |
| 265 | + "Dont know how to handle data." |
273 | 266 | ) |
274 | 267 | if processed: |
275 | 268 | # Mail was processed. Add label so its not processed again |
|
0 commit comments