-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.py
131 lines (116 loc) · 4.02 KB
/
task.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import base64
import os
import pickle
from datetime import datetime
# for working with github
from github import Github
from github.InputGitTreeElement import InputGitTreeElement
from google.auth.transport.requests import Request
# Gmail API utils
from googleapiclient.discovery import build
from logger import logger
def gmail_authenticate():
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
if not creds.valid:
logger.warning("Creds no longer valid, refreshing...")
creds.refresh(Request())
return build("gmail", "v1", credentials=creds)
def search_messages(service, query):
result = service.users().messages().list(userId="me", q=query).execute()
messages = []
if "messages" in result:
messages.extend(result["messages"])
while "nextPageToken" in result:
page_token = result["nextPageToken"]
result = (
service.users()
.messages()
.list(userId="me", q=query, pageToken=page_token)
.execute()
)
if "messages" in result:
messages.extend(result["messages"])
return messages
def commit_push(files):
g = Github(os.environ["GITHUB_TOKEN"])
logger.info("Authenticated Github")
repo = g.get_repo(os.environ["GITHUB_REPO"])
ref = repo.get_git_ref(os.environ["GITHUB_BRANCH"])
commit = repo.get_commit(ref.object.sha)
blobs = []
for file in files:
blobs.append(
{
"name": file["filename"],
"blob": repo.create_git_blob(file["rawdata"], "base64"),
}
)
tree = commit.commit.tree
new_tree = repo.create_git_tree(
[
InputGitTreeElement(
f"{os.environ.get('GITHUB_DESTIONATION', '')}/{file['name']}",
"100644",
"blob",
sha=file["blob"].sha,
)
for file in blobs
],
tree,
)
new_commit = repo.create_git_commit(
f"rocketbook upload: {str(datetime.utcnow()).split('.')[0]}",
new_tree,
[commit.commit],
)
ref.edit(new_commit.sha)
def main():
logger.info("## Starting main task ##")
service = gmail_authenticate()
logger.info("Authenticated Gmail")
results = search_messages(service, "from:[email protected]")
logger.info(f"Found {len(results)} new emails")
files = []
for message in results:
msg = (
service.users()
.messages()
.get(userId="me", id=message["id"], format="full")
.execute()
)
payload = msg["payload"]
headers = payload.get("headers")
relevant_headers = {"From", "To", "Subject", "Date"}
headers = {
header["name"].lower(): header["value"].lower()
for header in headers
if header["name"] in relevant_headers
}
logger.info(headers)
parts = payload.get("parts")
supported = {"image/jpeg", "application/pdf"}
for part in filter(lambda part: part["mimeType"] in supported, parts):
filename = part.get("filename")
logger.info(f"Found supported file: {filename}")
body = part.get("body")
attachment_id = body.get("attachmentId")
attachment = (
service.users()
.messages()
.attachments()
.get(id=attachment_id, userId="me", messageId=message["id"])
.execute()
)
data = attachment.get("data")
rawdata = base64.b64encode(base64.urlsafe_b64decode(data)).decode("utf-8")
files.append({"filename": filename, "rawdata": rawdata})
if files:
logger.info("Pushing new items to repo")
commit_push(files)
if results:
logger.info("Deleting pushed emails")
service.users().messages().batchDelete(
userId="me", body={"ids": [msg["id"] for msg in results]}
).execute()
logger.info("## Finished main task ##")