-
Notifications
You must be signed in to change notification settings - Fork 3
/
zyxel_harvest_www2.py
335 lines (312 loc) · 11.4 KB
/
zyxel_harvest_www2.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
# coding: utf-8
import harvest_utils
from harvest_utils import waitVisible, waitText, getElems, getFirefox,driver,waitTextChanged, getElemText, elemWithText, waitClickable, waitUntilStable, isReadyState,waitUntil,retryStable,getNumElem,goToUrl
from selenium.common.exceptions import NoSuchElementException, TimeoutException, StaleElementReferenceException, WebDriverException
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.keys import Keys
import sys
import sqlite3
import re
import time
import datetime
from datetime import datetime
import ipdb
import traceback
from my_utils import uprint,ulog,getFuncName
from contextlib import suppress
import random
import math
import html2text
from urllib import parse
from os import path
driver,conn=None,None
modelName=''
prevTrail=[]
startTrail=[]
rootUrl='http://www2.zyxel.com/us/en/support/download_landing.shtml'
allModels=[]
def glocals()->dict:
""" globals() + locals()
"""
import inspect
ret = dict(inspect.stack()[1][0].f_locals)
ret.update(globals())
return ret
def getScriptName():
from os import path
return path.splitext(path.basename(__file__))[0]
def getStartIdx():
global startTrail
if startTrail:
return startTrail.pop(0)
else:
return 0
def sql(query:str, var=None):
global conn
csr=conn.cursor()
try:
if var:
rows = csr.execute(query,var)
else:
rows = csr.execute(query)
if not query.startswith('SELECT'):
conn.commit()
if query.startswith('SELECT'):
return rows.fetchall()
else:
return
except sqlite3.Error as ex:
print(ex)
raise ex
def retryUntilTrue(statement, timeOut:float=6.2, pollFreq:float=0.3):
timeElap=0
while timeElap<timeOut:
timeBegin=time.time()
try:
r = statement()
if r==True:
return r
except (StaleElementReferenceException,NoSuchElementException, StopIteration):
pass
except Exception as ex:
ulog('raise %s %s'%(type(ex),str(ex)))
raise ex
#ulog('sleep %f secs'%pollFreq)
time.sleep(pollFreq)
timeElap+=(time.time()-timeBegin)
raise TimeoutException(getFuncName()+': timeOut=%f'%timeOut)
def retryA(statement, timeOut:float=6.2, pollFreq:float=0.3):
timeElap=0
while timeElap<timeOut:
timeBegin=time.time()
try:
return statement()
except (StaleElementReferenceException,NoSuchElementException, StopIteration):
pass
except Exception as ex:
ulog('raise %s %s'%(type(ex),str(ex)))
raise ex
#ulog('sleep %f secs'%pollFreq)
time.sleep(pollFreq)
timeElap+=(time.time()-timeBegin)
raise TimeoutException(getFuncName()+': timeOut=%f'%timeOut)
def guessDate(txt:str)->datetime:
""" txt = '07-13-2015' """
try:
m = re.search(r'\d{2}-\d{2}-\d{4}', txt)
return datetime.strptime(m.group(0), '%m-%d-%Y')
except Exception as ex:
ipdb.set_trace()
traceback.print_exc()
def upsertOneVersion(row,imgUrl,prodName):
global driver,prevTrail,modelName
try:
fwVer = row.find_element_by_css_selector('td.versionTd').text.strip()
pageUrl = driver.current_url
ulog('row.text="%s"'%repr(row.text))
fileUrls = [_.get_attribute('data-filelink') for _ in row.find_elements_by_css_selector('td.downloadTd a') if _.is_displayed() ]
fileUrl = '\n'.join(_ for _ in fileUrls if _)
relDate = guessDate(row.text)
trailStr=str(prevTrail)
sql("INSERT OR REPLACE INTO TFiles(model, prod_name, "
" fw_ver, rel_date, image_url, page_url, file_url, "
" tree_trail) VALUES (:modelName,:prodName,"
" :fwVer,:relDate,:imgUrl,:pageUrl,:fileUrl,"
" :trailStr)",glocals())
ulog('UPSERT "%(modelName)s","%(fwVer)s",%(trailStr)s'%glocals())
except Exception as ex:
ipdb.set_trace()
traceback.print_exc()
driver.save_screenshot(getScriptName()+'_'+getFuncName()+'_excep.png')
def upsertOneModel():
global driver, prevTrail, modelName
try:
prodName = [_.text for _ in getElems('div.sectionTitle p.hidden-xs') if _.text.strip()]
if prodName:
assert len(prodName)==1
prodName=prodName[0]
else:
prodName=None
pageUrl=driver.current_url
try:
imgUrl=waitVisible('.productPic img.img-responsive',4,1).get_attribute('src')
except TimeoutException:
imgUrl=None
assert imgUrl is None or imgUrl.startswith('http')
trailStr=str(prevTrail)
sql("INSERT OR REPLACE INTO TFiles(model,prod_name,page_url,"
"image_url,tree_trail) VALUES(:modelName, :prodName, :pageUrl,"
":imgUrl,:trailStr)", glocals())
ulog('UPSERT "%(modelName)s" "%(prodName)s",%(trailStr)s,'
'%(pageUrl)s' %glocals())
except Exception as ex:
ipdb.set_trace()
traceback.print_exc()
driver.save_screenshot(getScriptName()+'_'+getFuncName()+'_excep.png')
def versionWalker():
global driver,prevTrail
try:
rows = getElems('#Firmware tr')
rows = [_ for _ in rows if _.text.startswith('Firmware')]
if not rows:
upsertOneModel()
return
assert len(rows)==1
row = rows[0]
try:
imgUrl=waitVisible('.productPic img.img-responsive',4,1).get_attribute('src')
except TimeoutException:
ulog('no Picture!')
imgUrl=None
prodName = [_.text for _ in getElems('div.sectionTitle p.hidden-xs') if _.text.strip()]
if prodName:
assert len(prodName)==1
prodName=prodName[0]
else:
prodName=None
try:
verBtn = row.find_element_by_css_selector('button')
except NoSuchElementException:
idx=0
ulog('only one version')
ulog('idx=%s'%idx)
prevTrail+=[idx]
upsertOneVersion(row,imgUrl,prodName)
prevTrail.pop()
return
verBtn.click()
versions = row.find_elements_by_css_selector('ul li a')
startIdx = getStartIdx()
numVersions = len(versions)
ulog('numVersions=%s'%numVersions)
for idx in range(startIdx, numVersions):
ulog('idx=%s'%idx)
ulog('click "%s"'%versions[idx].text.strip())
versions[idx].click()
time.sleep(0.1)
prevTrail+=[idx]
upsertOneVersion(row,imgUrl,prodName)
prevTrail.pop()
if idx < numVersions-1:
verBtn.click()
versions = row.find_elements_by_css_selector('ul li a')
except Exception as ex:
ipdb.set_trace()
traceback.print_exc()
driver.save_screenshot(getScriptName()+'_'+getFuncName()+'_excep.png')
def modelWalker():
global driver, prevTrail, modelName
rootUrl='http://www2.zyxel.com/us/en/support/DownloadLandingSR.shtml?c=us&l=en&md='
try:
startIdx = getStartIdx()
ulog('len(allModels)=%d'%len(allModels))
for idx in range(startIdx,len(allModels)):
ulog('idx=%s'%idx)
modelName=allModels[idx]
ulog('modelName="%s"'%modelName)
goToUrl(rootUrl+parse.quote(modelName))
waitUntil(isReadyState)
# click "Firmware" tab
tab = elemWithText('li.resp-tab-item','Firmware')
if not tab:
ulog('no Firmware tab,bypass!')
prevTrail+=[idx]
upsertOneModel()
prevTrail.pop()
continue
tab.click()
time.sleep(0.1)
prevTrail+=[idx]
versionWalker()
prevTrail.pop()
except Exception as ex:
ipdb.set_trace()
traceback.print_exc()
driver.save_screenshot(getScriptName()+'_'+getFuncName()+'_excep.png')
def getAllModels():
global driver, allModels
try:
if path.exists('zyxel_models.txt') and \
path.getsize('zyxel_models.txt')>2 and \
time.time() - path.getmtime('zyxel_models.txt')<3600*12:
with open('zyxel_models.txt','r',encoding='utf-8') as fin:
lines = fin.read()
allModels=[_ for _ in lines.splitlines()]
allModels=[_.strip() for _ in allModels if _.strip()]
return
# click 'Enter model number here'
btn = waitClickable('button[data-id=modelName]')
btn.click()
time.sleep(0.1)
inp = waitClickable('.form-control')
inp.click()
inp.send_keys(Keys.UP)
time.sleep(0.1)
inp.send_keys(Keys.UP)
oldNumModels = getNumElem('div.dropdown-menu.open ul li a')
while True:
inp.send_keys(Keys.UP)
time.sleep(0.1)
inp.send_keys(Keys.UP)
numModels = getNumElem('div.dropdown-menu.open ul li a')
ulog('numModels=%d'%numModels)
if numModels == oldNumModels:
break
oldNumModels = numModels
allModels = [_.text for _ in getElems('div.dropdown-menu.open ul li a')]
allModels = [_.strip() for _ in allModels if _.strip()]
allModels = [_ for _ in allModels if not _.lower().startswith('enter model ')]
ulog('len(allModels)=%d'%len(allModels))
with open('zyxel_models.txt','w',encoding='utf-8') as fout:
fout.write('\n'.join(_ for _ in allModels))
btn.click()
except Exception as ex:
ipdb.set_trace()
traceback.print_exc()
driver.save_screenshot(getScriptName()+'_'+getFuncName()+'_excep.png')
def main():
global startTrail, prevTrail,driver,conn
try:
startTrail = [int(re.search(r'\d+', _).group(0)) for _ in sys.argv[1:]]
ulog('startTrail=%s'%startTrail)
conn=sqlite3.connect('zyxel.sqlite3')
sql("CREATE TABLE IF NOT EXISTS TFiles("
"id INTEGER NOT NULL,"
"model TEXT," # NBG5715
"prod_name TEXT," # NBG5715
"fw_ver TEXT," # V1.00(AAAG.8)C0
"rel_date DATE," # 06-18-2015
"image_url TEXT," # http://www2.zyxel.com/uploads/images/img_nbg5715_p_01_380.gif
"page_url TEXT," # http://www2.zyxel.com/us/en/support/DownloadLandingSR.shtml?c=us&l=en&kbid=M-00022&md=NBG5715#searchZyxelTab4
"file_url TEXT," # ftp://ftp2.zyxel.com/NBG5715/firmware/NBG5715_V1.00(AAAG.8)C0.zip
"tree_trail TEXT," # [26, 2, 1, 0, 0]
"file_size INTEGER," #
"file_sha1 TEXT," # 5d3bc16eec2f6c34a5e46790b513093c28d8924a
"PRIMARY KEY (id)"
"UNIQUE(model,fw_ver)"
")")
driver=harvest_utils.getFirefox()
# driver.implicitly_wait(2.0)
harvest_utils.driver=driver
prevTrail=[]
goToUrl(rootUrl)
getAllModels()
modelWalker()
driver.quit()
conn.close()
except Exception as ex:
ipdb.set_trace()
traceback.print_exc()
driver.save_screenshot(getScriptName()+'_'+getFuncName()+'_excep.png')
if __name__=='__main__':
try:
main()
except Exception as ex:
ipdb.set_trace()
traceback.print_exc()
try:
driver.save_screenshot(getScriptName()+'_excep.png')
driver.quit()
except Exception:
pass