-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrawler.py
231 lines (191 loc) · 6.4 KB
/
crawler.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
import re
import sys
import time
import zipfile
import filecmp
from typing import List, Tuple
from shutil import copytree, rmtree
from urllib.request import urlretrieve
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.remote.webelement import WebElement
URL = "http://huma.ysepan.com"
ZIP_FILE = "/tmp/huma.zip"
CHANGELOG_FILE = "changelog.txt"
try:
os.unlink(ZIP_FILE)
except Exception:
pass
# options = Options()
# options.headless = True
# os.environ['MOZ_HEADLESS'] = '1'
# browser = webdriver.Firefox(options=options) # type: ignore
timeout = 10
def eprint(s, *args):
print(s, *args, file=sys.stderr)
def find_by_id(root, id: str) -> WebElement:
i = 0
while True:
try:
ele = root.find_element(by="id", value=id)
return ele
except Exception as e:
if i > timeout:
eprint(f"failed to find by id: {id}", repr(e))
exit(1)
time.sleep(1)
i += 1
def find_by_tag(root, tag_name: str) -> WebElement:
i = 0
while True:
try:
ele = root.find_element(by="tag name", value=tag_name)
return ele
except Exception as e:
if i > timeout:
eprint(f"failed to find by id: {id}", repr(e))
exit(1)
time.sleep(1)
i += 1
def find_elements_by_tag(root, tag_name: str) -> List[WebElement]:
i = 0
while True:
items = root.find_elements(by="tag name", value=tag_name)
if len(items) > 1:
return items
if i > timeout:
eprint(f'failed to find elements by tag {tag_name}')
exit(1)
time.sleep(1)
i += 1
def find_item_in_list(items, starts: str) -> WebElement:
for item in items:
if item.text.startswith(starts):
return item
eprint(f'failed to find elements starting with {starts}')
exit(1)
def find_item_in_list_by_tagname(items, tag_name: str, starts: str) -> Tuple[WebElement, WebElement]:
for item in items:
e = item.find_element(by='tag name', value=tag_name)
if e.text.startswith(starts):
return (item, e)
eprint(f'failed to find item in list by tagname {tag_name} that starts with {starts}')
exit(1)
def get_zip_and_extract(browser):
browser.get(URL)
menu_list = find_by_id(browser, 'dzx')
items = find_elements_by_tag(menu_list, 'li')
(target, a) = find_item_in_list_by_tagname(items, 'a', '03 虎码输入法下载')
a.click()
ul = find_by_tag(target, 'ul')
items = find_elements_by_tag(ul, 'li')
target = find_item_in_list(items, "④Mac")
target.click()
a = target.find_element(by="tag name", value="a")
a.click()
ul = find_by_tag(target, 'ul')
items = find_elements_by_tag(ul, 'li')
target = find_item_in_list(items, '鼠须管')
target.click()
a = target.find_element(by="tag name", value="a")
a.click()
ul = find_by_tag(target, 'ul')
items = find_elements_by_tag(ul, 'li')
target = find_item_in_list(items, "虎码秃版 鼠须管 (Mac)")
a = find_by_tag(target, 'a')
url = a.get_attribute('href')
name = target.text
assert url is not None
assert name != ''
assert ".zip" in name and "MB" in name
m = re.match(r"虎码秃版 鼠须管 (Mac)(?P<date>.*)\.zip", name)
if m is None:
eprint(f"failed to extract date from filename: {name}")
exit(1)
date = m.group("date")
r = ''
if re.match(r"\d{4}\.\d{2}\.\d{2}", date):
print(f"tag=v{date}")
r = f'v{date}'
else:
# not necessarily date, might be a tag
print(f"tag={date}")
r = date
eprint(f"downloading {name} with url {url}")
urlretrieve(url, ZIP_FILE)
ignore_files = [
".git",
".github",
".gitignore",
"README.md",
"crawler.py",
"geckodriver.log",
]
def delete_removed(diff, par='.'):
for file in diff.left_only:
os.unlink(os.path.join(par, file))
for d, cmp in diff.subdirs.items():
delete_removed(cmp, d)
with zipfile.ZipFile(ZIP_FILE, "r", metadata_encoding="cp936") as zip_ref:
folder = os.path.join("/tmp", zip_ref.filelist[0].filename[:-1])
rmtree(folder, ignore_errors=True)
zip_ref.extractall("/tmp")
diff = filecmp.dircmp(".", folder, ignore=ignore_files)
delete_removed(diff)
copytree(folder, ".", dirs_exist_ok=True)
os.unlink(ZIP_FILE)
rmtree(folder)
return r
def get_changelog(browser):
browser.get(URL)
menu_list = find_by_id(browser, 'dzx')
items = find_elements_by_tag(menu_list, 'li')
(target, a) = find_item_in_list_by_tagname(items, 'a', '05 虎码测评 更新日志')
a.click()
ul = find_by_tag(target, 'ul')
items = find_elements_by_tag(ul, 'li')
target = find_item_in_list(items, "虎码更新日志 ")
a_list = find_elements_by_tag(target, 'a')
name = target.text.splitlines()[0]
url = None
for a in a_list:
url_ = a.get_attribute('href')
if url_ is not None and url_.startswith('http'):
url = url_
break
assert url is not None
m = re.match(r"虎码更新日志 (?P<date>.*).txt", name)
if m is None:
eprint(f"failed to extract date from filename: {name}")
exit(1)
date = m.group("date")
r = ''
if re.match(r"\d{4}\.\d{2}\.\d{2}", date):
print(f"tag=v{date}")
r = f'v{date}'
else:
# not necessarily date, might be a tag
print(f"tag={date}")
r = date
eprint(f"downloading {name} with url {url}")
urlretrieve(url, CHANGELOG_FILE)
return r
def main():
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
browser = webdriver.Chrome(options=chrome_options) # type: ignore
tag = get_zip_and_extract(browser)
eprint(f'got tag {tag}')
browser.close()
browser = webdriver.Chrome(options=chrome_options) # type: ignore
changelog_tag = get_changelog(browser)
eprint(f'got changelog tag {changelog_tag}')
browser.close()
if tag != changelog_tag:
eprint("zip tag and changelog tag mismatch")
if tag > changelog_tag: # allow change log tag to be newer than zip tag for now
exit(1)
if __name__ == "__main__":
main()