-
Notifications
You must be signed in to change notification settings - Fork 2
/
mail_stories.py
76 lines (61 loc) · 2.4 KB
/
mail_stories.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import requests
import json
from google.oauth2 import service_account
from fs.googledrivefs import GoogleDriveFS
import os
GOOGLE_DRIVE_SERVICE_ACCOUNT = json.loads(os.environ.get('GOOGLE_DRIVE_SERVICE_ACCOUNT', ''))
BASE_URL_HN_API = 'https://hacker-news.firebaseio.com'
NEW_STORIES = 'new_stories'
BASE_PATH_GOOGLE_DRIVE = 'datastores/hn/'
LEO_EMAIL = os.environ.get('LEO_EMAIL', '')
# MAILGUN_API_KEY = os.environ.get('MAILGUN_API_KEY', '')
# MAILGUN_SANDBOX = os.environ.get('MAILGUN_SANDBOX', '')
MAILJET_KEY = os.environ.get('MAILJET_KEY', '')
MAILJET_SECRET = os.environ.get('MAILJET_SECRET', '')
g_credentials = service_account.Credentials.from_service_account_info(GOOGLE_DRIVE_SERVICE_ACCOUNT)
fs = GoogleDriveFS(credentials=g_credentials)
with fs.open(f'{BASE_PATH_GOOGLE_DRIVE}{NEW_STORIES}') as f:
new_stories = f.read().splitlines()
mail_text = ''
for story in new_stories:
full_story = requests.get(f'{BASE_URL_HN_API}/v0/item/{story}.json').json()
if full_story is not None and 'deleted' not in full_story and 'title' in full_story:
if 'url' in full_story:
entry = f'{story} - {full_story["title"]} - {full_story["url"]}\n'
else:
entry = f'{story} - {full_story["title"]} - https://news.ycombinator.com/item?id={story}\n'
mail_text += entry
if mail_text != '':
print(mail_text)
# response = requests.post(
# f'https://api.mailgun.net/v3/{MAILGUN_SANDBOX}.mailgun.org/messages',
# auth=('api', MAILGUN_API_KEY),
# data={
# 'from': f'HN notifier <postmaster@{MAILGUN_SANDBOX}.mailgun.org>',
# 'to': f'Leonid <{LEO_EMAIL}>',
# 'subject': '[HN] new stories',
# 'text': mail_text
# }
# )
# response.raise_for_status()
response = requests.post(
'https://api.mailjet.com/v3.1/send',
auth=(MAILJET_KEY, MAILJET_SECRET),
json={
'Messages': [{
'From': {
'Email': LEO_EMAIL,
'Name': 'HN notifier'
},
'To': [{
'Email': LEO_EMAIL,
'Name': 'Leo'
}],
'Subject': '[HN] new stories',
'TextPart': mail_text
}]
}
)
response.raise_for_status()
with fs.open(f'{BASE_PATH_GOOGLE_DRIVE}{NEW_STORIES}', 'w') as f:
f.write('')