-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
146 lines (138 loc) · 4.8 KB
/
core.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
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.common.exceptions import NoSuchElementException
from mytelegram import bot
from selenium.webdriver.common.by import By
import re
import warnings
from datetime import datetime
from param import *
warnings.filterwarnings("ignore", category=DeprecationWarning)
hless = True
restTime=0 #seconds
cycleTime=60 #seconds
notification=60 #minutes
cnt=0
def init():
print("Initializing...")
options = webdriver.FirefoxOptions()
#options = webdriver.ChromeOptions()
if hless:
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
#options.add_argument('window-size=1200x600')
return driver
def calculateMonthDif():
if datetime.now().year==year:
return month-datetime.now().month
if datetime.now().year<year:
return 12-datetime.now().month+month
def searchTrain(driver):
driver.get("https://www.trenitalia.com/")
assert "Trenitalia" in driver.title
depStat = driver.find_element_by_name("departureStation")
print("Departure Station found")
depStat.clear()
depStat.send_keys(DepartureStation)
depStat.send_keys(Keys.RETURN)
arrStat = driver.find_element_by_name("arrivalStation")
print("Arrival Station found")
arrStat.clear()
arrStat.send_keys(ArrivalStation)
arrStat.send_keys(Keys.RETURN)
calendar = driver.find_element_by_name("departureDateVISIBLE")
print("Date found")
calendar.click()
dif=calculateMonthDif()
for i in range(0,dif-1):
nxtMonthButt = driver.find_element(By.ID,"ui-datepicker-div").find_element(By.CLASS_NAME,"ui-datepicker-group-last").find_element(By.CLASS_NAME,"ui-datepicker-header").find_element(By.CLASS_NAME,"ui-datepicker-next")
print("Next month found")
nxtMonthButt.click()
date = driver.find_element(By.ID,"ui-datepicker-div").find_element(By.CLASS_NAME,"ui-datepicker-group-last").find_element(By.CLASS_NAME,"ui-datepicker-calendar").find_elements(By.TAG_NAME,"td")
for dat in date:
if int(dat.text)==day:
print("Date found")
dat.click()
break
depTime = driver.find_element_by_id("biglietti_ora_p")
print("Time found")
Select(depTime).select_by_value(hr)
passenger = driver.find_element_by_id("biglietti_adulti")
print("Passengers found")
passenger.click()
plus = driver.find_element_by_id('addAdult')
print("Number of passengers found")
plus.click()
go = driver.find_element_by_xpath("/html/body/div[5]/form/div/div[3]/div[2]/div[6]/button")
print("Search found")
go.click()
return 0
def getTrainList(driver):
l =[]
i=0
while True:
try:
l.append(driver.find_element(By.ID, 'travelSolution'+str(i)))
i+=1
except NoSuchElementException:
print("Solutions extracted: "+str(i-1))
break
if i!=0:
return l
else:
return -1
def getTimes(solutions):
d=[]
for train in solutions:
index = str(solutions.index(train))
try:
print("Searching times for solution "+index)
hr=train.find_element(By.ID,"deptTime_"+index).get_attribute("value")
ch=train.find_element(By.CLASS_NAME,"table-solution-hover").find_element(By.CLASS_NAME,"solutionRow").find_element(By.CLASS_NAME,"trainOffer").find_element(By.CLASS_NAME,"descr").text
d.append(str(hr)+"_"+str(ch))
except NoSuchElementException:
print("Error in getting times or changes")
return -1
for dd in d:
if "*" in dd:
d.remove(dd)
return d
def inform(mybot,message):
mybot.send(message)
if __name__ == "__main__":
mybot = bot("AntonioMarangi"+id)
lastTimes=0
while True:
try:
t = time.time()
driver = init()
print("Inizialized")
res = searchTrain(driver)
if res!=0:
raise Exception
Trains = getTrainList(driver)
if Trains==-1:
raise Exception
Times = getTimes(Trains)
if Times==-1:
raise Exception
if len(Times)!=lastTimes:
lastTimes=len(Times)
inform(mybot,str(Times))
print("Informing")
if cnt==notification:
inform(mybot,str(Times))
cnt=0
print("Informing")
driver.close()
elapsed = time.time()-t
print("Elapsed: "+str(elapsed))
cnt+=1
print("Iteration: "+str(cnt))
time.sleep(cycleTime-elapsed)
except Exception:
print("error: END")
time.sleep(restTime)