-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrainline_search.rb
155 lines (136 loc) · 4.78 KB
/
trainline_search.rb
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
require './config/environment.rb'
class TrainlineSearch
def search_loop
while 1 do
trips_to_search.each do |trip_to_search|
next unless trip_is_valid?(trip_to_search)
next if trip_is_outdated?(trip_to_search)
puts Time.now.strftime("%H:%M") + " Recherche d'un train de #{trip_to_search["departure_station"]} a #{trip_to_search["arrival_station"]} entre le #{trip_to_search["from_date"]} et #{trip_to_search["to_date"]}"
csv_result = trainline_query(trip_to_search)
desactivate_searching(trip_to_search) && next if csv_result.nil?
search_results = csv_to_array(csv_result)
if search_results === false
p "Error in parsing trainline for this trip", trip_to_search
desactivate_searching(trip_to_search)
elsif tgvmax_train = tgvmax_inside?(search_results)
new_tgvmax_available(trip_to_search, tgvmax_train)
else
puts Time.now.strftime("%H:%M") + " Aucun train dispo de #{trip_to_search["departure_station"]} a #{trip_to_search["arrival_station"]} entre le #{trip_to_search["from_date"]} et #{trip_to_search["to_date"]}"
end
sleep(5)
end
end
end
def trainline_query(requested_trip)
birthdate = format_birthdate(requested_trip.user.birthdate)
tgvmax_key = requested_trip.user.tgvmax_key
departure_station = requested_trip['departure_station']
arrival_station = requested_trip['arrival_station']
from_date = format_departure_date(requested_trip['from_date'])
to_date = format_departure_date(requested_trip['to_date'])
query = "python3 trainline_parser.py \'#{birthdate}\' \'#{tgvmax_key}\' \'#{departure_station}\' \'#{arrival_station}\' \'#{from_date}\' \'#{to_date}\'"
puts query
begin
`#{query}`
rescue Errno::ENOENT, Errno::EACCES
puts 'Error in Python script'
return false
end
end
def desactivate_searching(trip)
trip.searching = false
trip.save(validate: false)
end
def trip_is_valid?(trip)
return false unless (trip.departure_station.is_a? String || trip.departure_station.to_s.strip.empty?)
return false unless (trip.arrival_station.is_a? String || trip.arrival_station.to_s.strip.empty?)
return false if trip.from_date.nil?
return false if trip.to_date.nil?
return false if trip.user.tgvmax_key.nil?
return false if trip.user.birthdate.nil?
# Notify the user if false
true
end
private
def trainline_date_generator(departure_date)
temp_time = Time.parse(departure_date)
temp_time.strftime("%Y-%m-%d-%H:%M")
end
def trainline_url_generator(trip, train)
departure_station = trip.departure_station
arrival_station = trip.arrival_station
tl_departure_date = trainline_date_generator(train['departure_date'])
url = "https://www.trainline.fr/search/#{departure_station}/#{arrival_station}/#{tl_departure_date}"
ShortURL.shorten(url)
end
def notify_user(trip, train)
Mailjet.configure do |config|
config.api_key = ENV['MJ_APIKEY_PUBLIC']
config.secret_key = ENV['MJ_APIKEY_PRIVATE']
config.api_version = "v3.1"
end
variable = Mailjet::Send.create(messages: [{
'From'=> {
'Email'=> "[email protected]",
'Name'=> "TGVMAX"
},
'To'=> [
{
'Email'=> trip.user.email,
'Name'=> "Passager TGVMAX"
}
],
'TemplateID'=> 1092935,
'TemplateLanguage'=> true,
'Subject'=> "TGVMAX Disponible ",
'Variables'=> {
"departure_station" => trip.departure_station,
"arrival_station" => trip.arrival_station,
"departure_date" => train['departure_date'],
"url" => trainline_url_generator(trip, train)
}
}])
p variable.attributes['Messages']
end
def trip_is_outdated?(trip)
if Date.today > trip['to_date']
# Notify the user
puts trip, "is outdated"
desactivate_searching(trip)
return true
end
false
end
def new_tgvmax_available(trip, train)
puts 'Le train suivant est disponible : ', train
notify_user(trip, train)
desactivate_searching(trip)
end
def trips_to_search
Trip.all.where(searching: true)
end
def format_birthdate(birthdate)
birthdate.strftime('%d/%m/%Y')
end
def format_departure_date(date)
date.strftime('%d/%m/%Y %H:%M')
end
def csv_to_array(string)
return false if string.empty?
string = string.chomp
string = string.gsub(';', ',')
csv = CSV::parse(string)
fields = csv.shift
fields = fields.map {|f| f.downcase.gsub(" ", "_")}
csv.collect { |record| Hash[*fields.zip(record).flatten] }
end
def tgvmax_inside?(trips)
trips.each do |trip|
return trip if trip["price"] == "0"
end
false
end
end
search = TrainlineSearch.new
puts "La recherche va commencer"
search.search_loop