-
Notifications
You must be signed in to change notification settings - Fork 0
/
tennis_session.rb
99 lines (89 loc) · 2.86 KB
/
tennis_session.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
require 'rubygems'
require 'highline/import'
require 'unirest'
require 'io/console'
require 'logger'
## A class to store the session token and some url
class TennisSession
def initialize
@logger = Logger.new(STDOUT)
@index_address = 'https://teleservices.paris.fr/srtm/jsp/web/index.jsp'
@auth_address = 'https://teleservices.paris.fr/srtm/authentificationConnexion.action'
@init_query_address = 'https://teleservices.paris.fr/srtm/reservationCreneauInit.action'
@query_address = 'https://teleservices.paris.fr/srtm/reservationCreneauListe.action'
@reservation_address = 'https://teleservices.paris.fr/srtm/reservationCreneauReserver.action'
@reservation_validation = 'https://teleservices.paris.fr/srtm/ReservationCreneauValidationForm.action'
@cookie = auth_cookie
end
def auth_cookie
@logger.info('get Auth Cookie')
response = Unirest.get(@index_address)
@cookie = response.headers[:set_cookie][0]
end
def credentials
login = ask 'login'
puts 'password'
password = STDIN.noecho(&:gets).chomp
[login, password]
end
def connect
@logger.info("Connecting to user #{@login} ...")
headers = { 'Accept' => 'text/html', 'Cookie' => @cookie }
login, password = credentials
parameters = { login: login, password: password }
Unirest.post @auth_address,
headers: headers,
parameters: parameters
end
def search_query(query_parameters)
@logger.info('Searching for a tennis court...')
@logger.info('Initializing the search...')
headers = {
'Accept' => 'text/html',
'Cookie' => @cookie
}
Unirest.get @init_query_address, headers: headers
@logger.info('Requesting the tennis courts page 1')
Unirest.post @query_address,
headers: headers,
parameters: query_parameters
end
def get_search_page(link)
headers = {
'Accept' => 'text/html',
'Cookie' => @cookie
}
page = link.split('&')
.select { |param| param.include?('d-41734-p') }[0]
.split('=')[-1].to_i
@logger.info("Getting page #{page} ")
Unirest.get link, headers: headers
end
def reservation_page(key)
headers = {
'Accept' => 'text/html',
'Cookie' => @cookie
}
Unirest.post @reservation_address,
headers: headers,
parameters: { cle: key }
end
def captcha_getter
headers = {
'Accept' => 'image/jpeg',
'Cookie' => @cookie
}
@logger.info('Getting a new captcha')
captcha_url = 'https://teleservices.paris.fr/srtm/ImageCaptcha'
Unirest.get captcha_url, headers: headers
end
def validate_reservation(captcha)
headers = {
Accept: 'text/html',
Cookie: @cookie,
jcaptcha_reponse: captcha,
valider: 'ENREGISTRER'
}
Unirest.post @reservation_validation, headers: headers
end
end