-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateData.py
55 lines (49 loc) · 1.87 KB
/
updateData.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
import concurrent.futures
import time
import os
from src.spider import spider
l = []
cnt = 0
spi = spider()
lastUpdateTime = ''
def updateData():
curr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f'[INFO] {curr}: Start to update data, it may takes a few minutes and every hour update.')
while True:
t = time.time()
global lastUpdateTime
lastIdx = spi.getLastIndexFromSinyi()
crawler([i for i in range(1, lastIdx + 1)])
lastUpdateTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f'[INFO] {lastUpdateTime}: Data updated.')
curr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f'[INFO] {curr}: It took {int(time.time() - t)} seconds to update this time.')
writeToFile()
pushToHeroku()
time.sleep(60 * 60)
def crawler(page):
global l
# multithread
with concurrent.futures.ThreadPoolExecutor(max_workers = 50) as executor:
tmp = executor.map(spi.getDataFromSinyi, page)
l = [y for x in tmp if x for y in x]
def writeToFile():
global cnt
try:
with open('houseData.txt', 'r', encoding = 'UTF-8') as f:
fileUpdateTime = f.readline()
if lastUpdateTime > fileUpdateTime:
with open('houseData.txt', 'w', encoding = 'UTF-8') as f:
f.write(lastUpdateTime)
f.writelines(['\n' + x.string() for x in l])
cnt += 1
curr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f'[INFO] {curr}: "houseData.txt" already updated {cnt} ' + ('times.' if cnt > 1 else 'time.'))
except Exception as e:
print(e)
def pushToHeroku():
os.system('push.bat>pushLog.txt')
curr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f'[INFO] {curr}: "houseData.txt" pushed to heroku.')
if __name__ == '__main__':
updateData()