Skip to content

Commit 3b25382

Browse files
committed
Update to support Train Pasmo usage
2 parents 1ceb7e8 + 69f8d82 commit 3b25382

File tree

6 files changed

+170
-88
lines changed

6 files changed

+170
-88
lines changed

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ Delete the next section of code:
7979
At this point you should have a working sample code.
8080

8181
## Customising the script.
82-
You will need to update the code in this function:
83-
```python
84-
handle_each_email()
85-
```
86-
87-
as well as this section in `main()`
82+
You will need to update the code in the following section:
8883
```python
8984
if notifier == "NOTE1":
9085
logger.debug("Note1")
@@ -95,8 +90,6 @@ elif notifier == "NOTE2":
9590
# Your custom code goes here. Bot / Line
9691
processed = True
9792
```
98-
99-
10093
If you are confused. Take a look at gmail.py which is my current working implementation.
10194

10295
## Automating Script Execution

constants.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
ENTER = "に入室しました"
77
LEAVE = "に退室しました"
88

9+
ENTERED_STATION = "入場"
10+
EXITED_STATION = "出場"
11+
912
FROM_BUS = "[email protected]"
1013
FROM_KIDZDUO = "[email protected]"
1114
FROM_GATE = "キッズセキュリティ ミマモルメ <[email protected]>"
15+
FROM_TRAIN = "東急セキュリティ <[email protected]>"
1216

1317
BUS_SUB = "エキッズ"
18+
TRAIN_SUB = "エキッズ"
1419
KIDZDUO_SUB = "入退室情報のお知らせ"
1520
GATE_SUB = "【キッズセキュリティミマモルメ】通過のお知らせ"
1621

17-
SUBJECTS = [BUS_SUB, KIDZDUO_SUB, GATE_SUB]
22+
23+
SUBJECTS = [BUS_SUB, TRAIN_SUB, KIDZDUO_SUB, GATE_SUB]

gmail-sample.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import pickle, os.path, sys, logging
1+
import pickle
2+
import os.path
3+
import sys
4+
import logging
25
from logging.handlers import RotatingFileHandler
36
from googleapiclient.discovery import build
47
from google_auth_oauthlib.flow import InstalledAppFlow
58
from google.auth.transport.requests import Request
6-
import base64, email
9+
import base64
10+
import email
711
from email import parser
812
from email import policy
9-
import patterns, constants, secrets
13+
import patterns
14+
import constants
15+
import secrets
1016
from secrets import LINE_TOKEN
1117
from line_notify import LineNotify
1218

@@ -19,23 +25,12 @@
1925
'https://www.googleapis.com/auth/gmail.labels']
2026

2127

22-
def setupLogging():
28+
def setup_logging():
2329
# supress google discovery_cache logging
2430
# https://github.com/googleapis/google-api-python-client/issues/299
2531
logging.getLogger('googleapiclient.discovery_cache').setLevel(
2632
logging.ERROR)
2733
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)
3934
logging.basicConfig(
4035
handlers=[RotatingFileHandler(LOG_FILE,
4136
maxBytes=100000,
@@ -209,18 +204,16 @@ def handle_each_email(service, message_id, logger) -> tuple:
209204
logger.warning(f"Failed to process message_id: {message_id} "
210205
f"Matched Subject: {subject} "
211206
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}")
219207
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
220213

221214

222215
def main():
223-
logger = setupLogging()
216+
logger = setup_logging()
224217
logger.info("Looking for email for notification")
225218
service = get_service()
226219
# Stage 1 Check Connection
@@ -263,13 +256,13 @@ def main():
263256
# Your custom code goes here. Bot / Line
264257
processed = True
265258
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")
267260
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")
269262
else:
270263
# 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."
273266
)
274267
if processed:
275268
# Mail was processed. Add label so its not processed again

0 commit comments

Comments
 (0)