-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotapi.rb
278 lines (216 loc) · 14.5 KB
/
otapi.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
require 'sinatra'
require 'mechanize'
require 'time'
require 'chronic'
require 'active_support/core_ext'
require 'tzinfo'
get '/otapi_v3.ashx/*' do
m = Mechanize.new { |mech| mech.user_agent = 'iPhone' }
url = "https://secure.opentable.com/api/otapi_v3.ashx/#{params[:splat].first}"
if request.query_string != ""
url += "?" + request.query_string
end
logger.info "Url #{url}"
m.get(url).body
end
get '/otcredentials' do
m = Mechanize.new { |mech| mech.user_agent = 'iPhone' }
token_page = m.get "https://mobile-api.opentable.com/oauth/consumer/token?client_id=ot-anonymous-apps&client_secret=0pentab1e&grant_type=implicit"
token = token_page.body
end
get '/availability' do
m = Mechanize.new
m.request_headers["Authorization"] = "Bearer #{params[:token]}"
m.user_agent = "com.contextoptional.OpenTable/8.6.1.1; iPhone; iOS/8.1; 3; #{SecureRandom.uuid}"
res = m.put("https://mobile-api.opentable.com/api/v2/restaurant/availability",
"{\"allowPop\":false,\"partySize\":#{params[:party]},\"includeOffers\":true,\"requestDateMessages\":true,\"rids\":[\"#{params[:rid]}\"],\"dateTime\":\"#{params[:time]}\",\"attribution\":{\"partnerId\":\"84\"}}", {'Content-Type' => 'application/json; charset=utf-8'})
res.body
end
get '/creditcard' do
m = Mechanize.new { |mech| mech.user_agent = 'iPhone' }
url = params[:url]
result = ""
credit_card_page = m.get url + "?" + params[:qs]
form = credit_card_page.forms.first
if not form
result = ""
else
form.field_with(:name => "customer[payment_method][number]").value = params[:ccn]
form.field_with(:name => "customer[payment_method][expiration_month]").value = params[:ccm]
form.field_with(:name => "customer[payment_method][expiration_year]").value = params[:ccy]
next_page = form.submit
result = next_page.body.to_s
end
result
end
post '/otapi_v3.ashx/*' do
m = Mechanize.new { |mech| mech.user_agent = 'iPhone' }
url = "https://secure.opentable.com/api/otapi_v3.ashx/#{params[:splat].first}"
if request.query_string != ""
url += "?" + request.query_string
end
logger.info "Url #{url}"
body = request.body.read
logger.info "Body #{body}"
result = m.post(url, body, { "Content-Type" => "application/x-www-form-urlencoded" }).body
logger.info "RESULT !!! #{result}"
result
end
get '/ip' do
m = Mechanize.new
b = m.get("http://www.whatismyipaddress.com").body
b.match(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/).to_s
end
get '/progress' do
desired_at = Time.parse(params[:desired_at])
m = Mechanize.new { |mech| mech.user_agent = 'iPhone' }
search_page = m.get("http://www.opentable.com/restaurant-search.aspx?startDate=#{CGI.escape(desired_at.strftime("%m/%d/%Y").strip)}&ResTime=#{CGI.escape(desired_at.strftime("%l:%M %p").strip)}&PartySize=#{params[:people]}&PartySizeFake=#{params[:people]}+People&RestaurantID=158713&rid=158713&GeoID=58&txtDateFormat=MM%2Fdd%2Fyyyy&RestaurantReferralID=158713", [], "http://theprogress-sf.com/")
return nil if not search_page.forms.first
return nil if not search_page.forms.first.ClientTimeStamp
client_timestamp = search_page.forms.first.ClientTimeStamp
search_results = m.get("http://m.opentable.com/search/results?PartySize=#{params[:people]}&DateInvariantCulture=#{desired_at.to_date.iso8601}T00%3A00%3A00&TimeInvariantCulture=0001-01-01T#{CGI.escape(desired_at.strftime("%T").strip)}&SearchName=State+Bird+Provisions&MetroAreaID=&RegionID=&Latitude=&Longitude=&RestaurantID=158713&PromoID=&PromoType=&PartnerReferralID=&ConfirmationNumber=&ClientTimeStamp=#{client_timestamp}&OfferConfirmNumber=0&ChosenOfferId=0&ReservationStatus=")
avail_times = search_results.links.select { |v| v.uri.to_s =~ /slotlock/ }
return nil if avail_times.empty?
good_times = cleanup_web_times(avail_times,desired_at,params[:tolerance].to_f,params[:tolerance_sign].to_i)
if good_times.empty?
return nil
end
details_page = good_times.first.click
if not details_page.forms.first
return nil
end
details_page_form = details_page.forms.first
if details_page_form.field_with(:name => "CreditCardNumber")
# do CC stuff
# => ["AMEX", "Diners Club", "Discover", "MasterCard", "Visa"]
ccn = params[:ccn]
ccm = params[:ccm]
ccy = params[:ccy]
return "needcc" if ccn.to_s.strip.empty?
ccform = details_page.forms.last
ccform.add_field!("hash", details_page_form.field_with(name: "hash").value)
ccform.add_field!("key_id", details_page_form.key_id)
ccform.add_field!("time", details_page_form.time)
ccform.add_field!("redirect", details_page_form.redirect)
ccform.add_field!("customer[payment_method][type]", "CreditCard")
ccform.add_field!("customer[payment_method][number]", ccn)
ccform.add_field!("customer[payment_method][expiration_month]", ccm)
ccform.add_field!("customer[payment_method][expiration_year]", ccy)
ccform.add_field!("customer[first_name]", params[:first_name])
ccform.add_field!("customer[last_name]", params[:last_name])
result = ccform.submit
dpq = CGI::parse(details_page.uri.query) # details_page.uri.query
vq = CGI::parse(result.uri.to_s)
constructlink = "https://m.opentable.com/reservation/details?RestaurantID=#{dpq["RestaurantID"].join}&" +
"Points=#{dpq["Points"].join}&PointsType=#{dpq["PointsType"].join}&SlotHash=#{dpq["SlotHash"].join}&SecurityID=#{dpq["SecurityID"].join}&" +
"DateTime=#{URI.encode(dpq["DateTime"].join)}&PartySize=#{dpq["PartySize"].join}&SlotLockID=#{dpq["SlotLockID"].join}&OfferConfirmNumber=#{dpq["OfferConfirmNumber"].join}&" +
"ChosenOfferId=#{dpq["ChosenOfferId"].join}&IsCreditCard=#{dpq["IsCreditCard"].join}&IsMiddleSlot=#{dpq["IsMiddleSlot"].join}&ArePopPoints=#{dpq["ArePopPoints"].join}&ExpireMonth=#{ccm}&" +
"ExpireYear=#{ccy}&Last4=#{ccn[-4..-1]}&PaymentUuid=#{vq["customer[payment_method][uuid]"].join}&" +
"CustomerUuid=#{vq["customer[uuid]"].join}&" +
"ResponseHash=#{vq["hash"].join}&HttpStatus=#{vq["http_status"].join}&" +
"ResponseTime=#{vq["time"].join}"
details_page = m.get constructlink
end
details_page_form = details_page.forms.first
details_page_form.FirstName = "#{params[:first_name]}"
details_page_form.LastName = "#{params[:last_name]}"
details_page_form.Email = "#{params[:email]}"
details_page_form.PhoneNumber = "#{params[:phone]}"
confirm_page = details_page_form.submit details_page_form.buttons.detect { |v| v.value =~ /Confirm/ }
return confirm_page.body
end
get '/progresstimes' do
desired_at = Time.parse(params[:desired_at])
m = Mechanize.new { |mech| mech.user_agent = 'iPhone' }
search_page = m.get("http://www.opentable.com/restaurant-search.aspx?startDate=#{CGI.escape(desired_at.strftime("%m/%d/%Y").strip)}&ResTime=#{CGI.escape(desired_at.strftime("%l:%M %p").strip)}&PartySize=#{params[:people]}&PartySizeFake=#{params[:people]}+People&RestaurantID=158713&rid=158713&GeoID=58&txtDateFormat=MM%2Fdd%2Fyyyy&RestaurantReferralID=158713", [], "http://theprogress-sf.com/")
return nil if not search_page.forms.first
return nil if not search_page.forms.first.ClientTimeStamp
client_timestamp = search_page.forms.first.ClientTimeStamp
search_results = m.get("http://m.opentable.com/search/results?PartySize=#{params[:people]}&DateInvariantCulture=#{desired_at.to_date.iso8601}T00%3A00%3A00&TimeInvariantCulture=0001-01-01T#{CGI.escape(desired_at.strftime("%T").strip)}&SearchName=State+Bird+Provisions&MetroAreaID=&RegionID=&Latitude=&Longitude=&RestaurantID=158713&PromoID=&PromoType=&PartnerReferralID=&ConfirmationNumber=&ClientTimeStamp=#{client_timestamp}&OfferConfirmNumber=0&ChosenOfferId=0&ReservationStatus=")
avail_times = search_results.links.select { |v| v.uri.to_s =~ /slotlock/ }
return JSON.dump(avail_times.map { |v| Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join('')) })
end
get '/statebirdtimes' do
desired_at = Time.parse(params[:desired_at])
m = Mechanize.new { |mech| mech.user_agent = 'iPhone' }
search_page = m.get("http://www.opentable.com/restaurant-search.aspx?startDate=#{CGI.escape(desired_at.strftime("%m/%d/%Y").strip)}&ResTime=#{CGI.escape(desired_at.strftime("%l:%M %p").strip)}&PartySize=#{params[:people]}&PartySizeFake=#{params[:people]}+People&RestaurantID=139246&rid=139246&GeoID=58&txtDateFormat=MM%2Fdd%2Fyyyy&RestaurantReferralID=139246", [], "http://mobile.dudamobile.com/site/statebirdsfcom?url=http%3A%2F%2Fstatebirdsf.com%2Freservations%2F")
return nil if not search_page.forms.first
return nil if not search_page.forms.first.ClientTimeStamp
client_timestamp = search_page.forms.first.ClientTimeStamp
search_results = m.get("http://m.opentable.com/search/results?PartySize=#{params[:people]}&DateInvariantCulture=#{desired_at.to_date.iso8601}T00%3A00%3A00&TimeInvariantCulture=0001-01-01T#{CGI.escape(desired_at.strftime("%T").strip)}&SearchName=State+Bird+Provisions&MetroAreaID=&RegionID=&Latitude=&Longitude=&RestaurantID=139246&PromoID=&PromoType=&PartnerReferralID=&ConfirmationNumber=&ClientTimeStamp=#{client_timestamp}&OfferConfirmNumber=0&ChosenOfferId=0&ReservationStatus=")
avail_times = search_results.links.select { |v| v.uri.to_s =~ /slotlock/ }
return JSON.dump(avail_times.map { |v| Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join('')) })
end
get '/statebird' do
desired_at = Time.parse(params[:desired_at])
m = Mechanize.new { |mech| mech.user_agent = 'iPhone' }
search_page = m.get("http://www.opentable.com/restaurant-search.aspx?startDate=#{CGI.escape(desired_at.strftime("%m/%d/%Y").strip)}&ResTime=#{CGI.escape(desired_at.strftime("%l:%M %p").strip)}&PartySize=#{params[:people]}&PartySizeFake=#{params[:people]}+People&RestaurantID=139246&rid=139246&GeoID=58&txtDateFormat=MM%2Fdd%2Fyyyy&RestaurantReferralID=139246", [], "http://mobile.dudamobile.com/site/statebirdsfcom?url=http%3A%2F%2Fstatebirdsf.com%2Freservations%2F")
return nil if not search_page.forms.first
return nil if not search_page.forms.first.ClientTimeStamp
client_timestamp = search_page.forms.first.ClientTimeStamp
search_results = m.get("http://m.opentable.com/search/results?PartySize=#{params[:people]}&DateInvariantCulture=#{desired_at.to_date.iso8601}T00%3A00%3A00&TimeInvariantCulture=0001-01-01T#{CGI.escape(desired_at.strftime("%T").strip)}&SearchName=State+Bird+Provisions&MetroAreaID=&RegionID=&Latitude=&Longitude=&RestaurantID=139246&PromoID=&PromoType=&PartnerReferralID=&ConfirmationNumber=&ClientTimeStamp=#{client_timestamp}&OfferConfirmNumber=0&ChosenOfferId=0&ReservationStatus=")
avail_times = search_results.links.select { |v| v.uri.to_s =~ /slotlock/ }
return nil if avail_times.empty?
good_times = cleanup_web_times(avail_times,desired_at,params[:tolerance].to_f,params[:tolerance_sign].to_i)
if good_times.empty?
return nil
end
details_page = good_times.first.click
if not details_page.forms.first
return nil
end
details_page_form = details_page.forms.first
if details_page_form.field_with(:name => "CreditCardNumber")
# do CC stuff
# => ["AMEX", "Diners Club", "Discover", "MasterCard", "Visa"]
ccn = params[:ccn]
ccm = params[:ccm]
ccy = params[:ccy]
return "needcc" if ccn.to_s.strip.empty?
ccform = details_page.forms.last
ccform.add_field!("hash", details_page_form.field_with(name: "hash").value)
ccform.add_field!("key_id", details_page_form.key_id)
ccform.add_field!("time", details_page_form.time)
ccform.add_field!("redirect", details_page_form.redirect)
ccform.add_field!("customer[payment_method][type]", "CreditCard")
ccform.add_field!("customer[payment_method][number]", ccn)
ccform.add_field!("customer[payment_method][expiration_month]", ccm)
ccform.add_field!("customer[payment_method][expiration_year]", ccy)
ccform.add_field!("customer[first_name]", params[:first_name])
ccform.add_field!("customer[last_name]", params[:last_name])
result = ccform.submit
dpq = CGI::parse(details_page.uri.query) # details_page.uri.query
vq = CGI::parse(result.uri.to_s)
constructlink = "https://m.opentable.com/reservation/details?RestaurantID=#{dpq["RestaurantID"].join}&" +
"Points=#{dpq["Points"].join}&PointsType=#{dpq["PointsType"].join}&SlotHash=#{dpq["SlotHash"].join}&SecurityID=#{dpq["SecurityID"].join}&" +
"DateTime=#{URI.encode(dpq["DateTime"].join)}&PartySize=#{dpq["PartySize"].join}&SlotLockID=#{dpq["SlotLockID"].join}&OfferConfirmNumber=#{dpq["OfferConfirmNumber"].join}&" +
"ChosenOfferId=#{dpq["ChosenOfferId"].join}&IsCreditCard=#{dpq["IsCreditCard"].join}&IsMiddleSlot=#{dpq["IsMiddleSlot"].join}&ArePopPoints=#{dpq["ArePopPoints"].join}&ExpireMonth=#{ccm}&" +
"ExpireYear=#{ccy}&Last4=#{ccn[-4..-1]}&PaymentUuid=#{vq["customer[payment_method][uuid]"].join}&" +
"CustomerUuid=#{vq["customer[uuid]"].join}&" +
"ResponseHash=#{vq["hash"].join}&HttpStatus=#{vq["http_status"].join}&" +
"ResponseTime=#{vq["time"].join}"
details_page = m.get constructlink
end
details_page_form = details_page.forms.first
details_page_form.FirstName = "#{params[:first_name]}"
details_page_form.LastName = "#{params[:last_name]}"
details_page_form.Email = "#{params[:email]}"
details_page_form.PhoneNumber = "#{params[:phone]}"
confirm_page = details_page_form.submit details_page_form.buttons.detect { |v| v.value =~ /Confirm/ }
return confirm_page.body
end
def cleanup_web_times(links,desired_at,tolerance,tolerance_sign)
Time.zone = "UTC"
Chronic.time_class = Time.zone
return [] if links.empty?
times = links
times = times - times.select { |v|
((tolerance_sign == -1 and Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join('')) < (desired_at - tolerance.to_f.hours)) ||
(tolerance_sign == -1 and Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join('')) > desired_at) ||
(tolerance_sign == 0 and Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join('')) > (desired_at + tolerance.to_f.hours)) ||
(tolerance_sign == 0 and Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join('')) < (desired_at - tolerance.to_f.hours)) ||
(tolerance_sign == 1 and Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join('')) > (desired_at + tolerance.to_f.hours)) ||
(tolerance_sign == 1 and Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join('')) < desired_at))
}
return times.sort_by { |v| (desired_at - Chronic.parse(CGI.parse(URI.parse(v.uri.to_s).query)["DateTime"].join(''))).abs }
end