-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
158 lines (125 loc) · 4.61 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import requests
import time
import json
import urllib.parse
from datetime import datetime
from log_utils import log_error, log_success, log_info
from utils import get_headers, format_slot
# Bot Configuration
INTERVAL = 9 # seconds
NO_RESERVATION = True
# Venue Configuration
VENUE_ID = 2567
PARTY_SIZE = 2
DAY = "2024-05-25"
def check_availability():
headers = get_headers()
params = {
'lat': '0',
'long': '0',
'day': DAY,
'party_size': PARTY_SIZE,
'venue_id': VENUE_ID
}
url = f'https://api.resy.com/4/find'
encoded_params = urllib.parse.urlencode(params)
full_url = f"{url}?{encoded_params}"
response = requests.get(full_url, headers=headers)
if response.status_code != 200:
log_error(f"Failed to fetch data: {response.status_code}")
log_error(f"Response Body: {response.text}")
return None
data = response.json()
matching_slots = []
for venue in data['results']['venues']:
if venue['venue']['id']['resy'] == VENUE_ID:
for slot in venue['slots']:
if (slot['availability']['id'] == 3 and
'20:00:00' >= slot['date']['start'][-8:] >= '18:00:00' and
slot['payment']['deposit_fee'] is None):
matching_slots.append(slot)
# Sort the list of matching slots by start time
matching_slots.sort(key=lambda x: x['date']['start'])
# Filter for preferred time range
preferred_slots = [slot for slot in matching_slots if '19:00:00' <=
slot['date']['start'][-8:] <= '19:30:00']
# Return the best available slot based on the time preference
if preferred_slots:
log_success("Best slot within preferred time range found:",
format_slot(preferred_slots[0]))
return book_reservation(preferred_slots[0])
elif matching_slots:
log_success("No preferred slots found, earliest matching slot:",
format_slot(matching_slots[0]))
return book_reservation(matching_slots[0])
else:
log_error("No matching slots found.")
return None
def get_reservation_details(config_id):
url = "https://api.resy.com/3/details"
headers = get_headers({
'X-Origin': 'https://widgets.resy.com',
'Origin': 'https://widgets.resy.com',
'Referer': 'https://widgets.resy.com/',
"Content-Type": "application/json",
})
body = {
"config_id": config_id,
"day": DAY,
"party_size": PARTY_SIZE
}
encoded_params = urllib.parse.urlencode(body)
full_url = f"{url}?{encoded_params}"
response = requests.get(full_url, headers=headers)
if response.status_code == 200:
return response.json()
else:
log_error(
f"Failed to fetch reservation details: {response.status_code}")
return None
def book_reservation(slot):
details = get_reservation_details(slot['config']['token'])
if not details:
return "Failed to get reservation details"
payment_method_id = details['user']['payment_methods'][0]['id']
book_token = details['book_token']['value']
# URL and headers setup for booking reservation
url = "https://api.resy.com/3/book"
headers = get_headers({
"Content-Type": "application/x-www-form-urlencoded",
"X-Origin": "https://widgets.resy.com",
'Origin': 'https://widgets.resy.com',
"Referer": "https://widgets.resy.com/",
})
# Form data for the POST request
data = {
"book_token": book_token,
"struct_payment_method": f'{{"id":{payment_method_id}}}',
"source_id": "resy.com-venue-details"
}
# Use urllib to encode the form data
encoded_data = urllib.parse.urlencode(data)
log_info("Attempting to snipe reservation...")
response = requests.post(url, headers=headers, data=encoded_data)
if response.status_code == 201:
log_success(f"Reservation successfully booked!")
NO_RESERVATION = False
return response.json()
else:
log_error(
f"Failed to book reservation: {response.status_code}, Reason: {response.text}")
return response.text
def main_loop():
global NO_RESERVATION
while NO_RESERVATION:
current_date = datetime.now().strftime('%Y-%m-%d')
if current_date >= DAY:
log_info("Date has passed, stopping the search.")
break
try:
check_availability()
except Exception as e:
log_error(f"An error occurred while checking availability: {e}")
time.sleep(INTERVAL)
if __name__ == '__main__':
main_loop()