-
Notifications
You must be signed in to change notification settings - Fork 1
/
crawl.py
143 lines (109 loc) · 4.37 KB
/
crawl.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
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
from bs4 import BeautifulSoup
import os
import pickle
import requests
import subprocess
from datetime import date
TARGETS = ['ui', 'chrome/browser/ui', 'chrome/browser/about_flags.cc']
DRY_RUN = False
MESSAGE_TOKEN = ''
COMMITS = {}
NEW_COMMITS = {}
class Commit(object):
def __init__(self, sha, target, title):
self.sha = sha
self.targets = [target]
self.title = title
self.link = 'https://chromium.googlesource.com/chromium/src/+/{}'.format(sha)
def add_target(self, target):
self.targets.append(target)
class DirectorySwitcher(object):
def __init__(self, origin, target):
self.origin = origin
self.target = target
def __enter__(self):
os.chdir(self.target)
def __exit__(self, exc_type, exc_val, exc_tb):
os.chdir(self.origin)
if __name__ == "__main__":
current_dir = os.getcwd()
script_dir = os.path.dirname(os.path.realpath(__file__))
token_path = os.path.join(script_dir, "message.token")
if os.path.exists(token_path):
with open(token_path, 'r') as file:
MESSAGE_TOKEN = file.read().strip()
pickle_path = os.path.join(script_dir, "commits.pickle")
if os.path.exists(pickle_path) and os.path.getsize(pickle_path) > 0:
with open(pickle_path, 'rb') as file:
COMMITS = pickle.load(file)
for target in TARGETS:
url = 'https://chromium.googlesource.com/chromium/src/+log/main/{}'.format(target)
response = requests.get(url)
if response.status_code == 200:
html = response.text
soup = BeautifulSoup(html, 'html.parser')
items = soup.select('li.CommitLog-item')
for item in items:
sha = item.select_one('.u-sha1').get_text()
if sha in COMMITS:
continue
if sha not in NEW_COMMITS:
title = item.select_one(':nth-child(2)').get_text()
NEW_COMMITS[sha] = Commit(sha, target, title)
else:
NEW_COMMITS[sha].add_target(target)
else:
print(response.status_code)
today = date.today().strftime("%Y-%m-%d")
markdown = "# Daily reports for Desktop folks\n"
markdown += "## {}\n".format(today);
if NEW_COMMITS:
for sha in NEW_COMMITS:
commit = NEW_COMMITS[sha]
markdown += "### {}\n".format(commit.title)
markdown += "* Affected: {}\n".format(", ".join(commit.targets))
markdown += "* {}\n\n".format(commit.link)
else:
markdown += "* Nothing has been commited today :)\n"
markdown = markdown.replace("`", "\\`");
page_path = os.path.join(script_dir, "reports/{}.html".format(today))
with open(page_path, 'w') as page:
page_template = '''<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Marked in the browser</title>
</head>
<body>
<div id="content"></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/marked.min.js"></script>
<script>
document.getElementById("content").innerHTML = marked.parse(`{}`);
</script>
</body>
</html>'''
page.write(page_template.format(markdown))
COMMITS.update(NEW_COMMITS)
with open(pickle_path, 'wb') as file:
pickle.dump(COMMITS, file)
with DirectorySwitcher(current_dir, script_dir):
try:
subprocess.check_output(["git", "add", "reports"])
subprocess.check_output(["git", "add", "commits.pickle"])
subprocess.check_output(["git", "commit", "-m", "'Reports on {}'".format(today)])
subprocess.check_output(["git", "push", "origin", "main"])
except:
pass
message = "# Daily reports for Desktop folks\n"
if NEW_COMMITS:
message += "* New commits: {}\n".format(len(NEW_COMMITS))
else:
message += "* There's no commit today :)\n"
message += "https://sangwoo108.github.io/ui-commit-cralwer/reports/{}.html".format(today)
if DRY_RUN:
print(markdown)
else:
subprocess.check_output(["curl", "-v", "-X", "POST", "-H", "Authorization: Bearer " + MESSAGE_TOKEN,
"-F", "message=" + message, "https://notify-api.line.me/api/notify"]);
exit(0)