-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
85 lines (76 loc) · 2.83 KB
/
main.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
import getPrices
from getPrices import getProductsFromWeb as getProds
from datetime import datetime
import database as db
from subscriptions import checkSubs
import time
from threading import Thread
import sys
test_urls = {
"kabum":{
"vga":['https://www.kabum.com.br/hardware/placa-de-video-vga?string=&pagina=1&ordem=5&limite=100'],
"ram":['https://www.kabum.com.br/hardware/memoria-ram?ordem=5&limite=100&pagina=1&string=']
},
"pichau":{
"vga":['https://www.pichau.com.br/hardware/placa-de-video?p=1&product_list_limit=48'],
"ram":['https://www.pichau.com.br/hardware/memorias?p=1&product_list_limit=48']
},
"terabyte":{
"vga":['https://www.terabyteshop.com.br/hardware/placas-de-video/nvidia-geforce'],
"ram":['https://www.terabyteshop.com.br/hardware/memorias/ddr4']
}
}
def readProducts(test=False):
while 1:
lastRead = db.getLastRead()
now = datetime.now().timestamp()
deltaInHours = int((now - lastRead)/(3600))
## Só faz a leitura se se passaram pelo menos 20 horas desde a ultima leitura.
if(deltaInHours >= 20 or test):
if(not test):
urls = db.getUrlsAsDict()
else:
urls = test_urls
startTime = datetime.now().timestamp()
data = getProds(urls)
endTime = datetime.now().timestamp()
getProdsTime = endTime - startTime
startTime = datetime.now().timestamp()
db.storeProducts(data)
endTime = datetime.now().timestamp()
saveSqliteTime = endTime - startTime
print("Captura dos preços completa!")
print("Tempo decorrido: " + str(getProdsTime + saveSqliteTime))
print("Tempo para obter os dados: " + str(getProdsTime))
print("Tempo para salvar em Sqlite: " + str(saveSqliteTime))
else:
print("Going to sleep")
nextRead = lastRead + 3600*8
sleepTime = nextRead - datetime.now().timestamp()
## Dorme até o momento da proxima leitura.
time.sleep(sleepTime)
def makeReports():
while 1:
checkSubs()
print('Make Reports Indo Dormir')
time.sleep(60*60)
def readTest():
readProducts(test=True)
def main():
print('Número de argumentos: ' + str(len(sys.argv)))
if(len(sys.argv) == 1):
getProductsThread = Thread(target=readProducts)
getProductsThread.start()
makeReportsThread = Thread(target=makeReports)
makeReportsThread.start()
getProductsThread.join()
makeReportsThread.join()
elif(len(sys.argv) == 2):
if(sys.argv[1] == 'readTest'):
readTest()
else:
print('Argumento incorreto!')
else:
print('Número incorreto de argumentos')
if __name__ == '__main__':
main()