This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticketcheck.py
109 lines (90 loc) · 3.58 KB
/
ticketcheck.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
import uzbooking
import traindb
import json
import logging
import datetime
import mailcomposer
def parse_monitor_config(filename):
with open(filename, 'r', encoding='utf8') as f:
raw_monitors = json.loads(f.read())
monitors = []
for raw_monitor in raw_monitors:
monitor = {}
try:
monitor['departure_date'] = datetime.datetime.strptime(
raw_monitor['departure_date'],'%Y.%m.%d')
except ValueError:
logging.error('Invalid departure date')
continue
departure_station_id = uzbooking.get_station_id(raw_monitor['departure_station'])
if not departure_station_id:
logging.error('Wrong departure station')
continue
monitor['departure_station_id'] = departure_station_id
monitor['departure_station_name'] = raw_monitor['departure_station']
destination_station_id = uzbooking.get_station_id(raw_monitor['destination_station'])
if not destination_station_id:
logging.error('Wrong destination station')
continue
monitor['destination_station_id'] = destination_station_id
monitor['destination_station_name'] = raw_monitor['destination_station']
try:
classes = raw_monitor['classes']
except KeyError:
logging.info('No classes specified. Monitoring all.')
classes = [1,2,3]
monitor['classes'] = classes
try:
monitor['email'] = raw_monitor['email']
except KeyError:
logging.error('No email address specified.')
continue
monitors.append(monitor)
return monitors
def parse_settings(filename):
with open(filename, 'r', encoding='utf8') as f:
settings = json.loads(f.read())
return settings
rides_to_update = []
def check_for_new_tickets(monitor):
global rides_to_update
trains = uzbooking.get_trains(
departure_date = monitor['departure_date'],
departure_station_id = monitor['departure_station_id'],
destination_station_id = monitor['destination_station_id'])
trains_with_new_tickets = []
for train in trains:
# Check tis train in db
seats_in_db = traindb.get_seats(
train['number'],
monitor['departure_station_name'],
train['departure_datetime'],
monitor['destination_station_name'])
for c in monitor['classes']:
if seats_in_db[c]<train['seats'][c]:
trains_with_new_tickets.append(train)
break
# add ride to update queue
rides_to_update.append((train,monitor))
return trains_with_new_tickets
if __name__=='__main__':
logging.basicConfig(filename='ticketcheck.log', level=logging.INFO)
logging.info("Script started")
monitors = parse_monitor_config('monitors.json')
settings = parse_settings('settings.json')
for monitor in monitors:
trains = check_for_new_tickets(monitor)
logging.info('Found %s trains'%len(trains))
if trains:
composer = mailcomposer.MailComposer(recipient=monitor['email'],**settings['email'])
composer.add_trains(monitor, trains)
composer.send_mail()
# update_rides
traindb.clear_rides()
for train, monitor in rides_to_update:
traindb.update_seats(
train['number'],
monitor['departure_station_name'],
train['departure_datetime'],
monitor['destination_station_name'],
train['seats'])