-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResy_Bot.py.old
244 lines (216 loc) · 10.6 KB
/
Resy_Bot.py.old
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
import os
import time
from datetime import datetime
from playwright.sync_api import sync_playwright
from dotenv import load_dotenv
import logging
import random
import time
from playwright.sync_api import sync_playwright
from playwright_stealth import stealth_sync
import logging
import re
from bs4 import BeautifulSoup
#tesx
# from selenium import webdriver
# from selenium.webdriver.firefox.options import Options
# options = Options()
# options.headless = True # Enable headless mode for Firefox
# driver = webdriver.Firefox(options=options)
# driver.get("https://resy.com")
load_dotenv('settings.env')
email = os.getenv('RESY_EMAIL')
password = os.getenv('RESY_PASSWORD')
# PW_TEST_SCREENSHOT_NO_FONTS_READY = 1
# headless = False
headless = True
'''headers = {
'accept': 'application/json, text/plain, */*',
'accept-language': 'en-US,en;q=0.9,id;q=0.8',
'authorization': 'ResyAPI api_key="VbWk7s3L4KiK5fzlO7JD3Q5EYolJI7n5"',
'cache-control': 'no-cache',
'content-type': 'application/x-www-form-urlencoded',
'origin': 'https://resy.com',
'priority': 'u=1, i',
'referer': 'https://resy.com/',
'sec-ch-ua': '"Microsoft Edge";v="125", "Chromium";v="125", "Not.A/Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
'x-origin': 'https://resy.com',
}
payload = 'email=aseasease2005b%40gmail.com&password=6EAMGVu8mpwWb7*'
'''
# made the file mode to overwrite the log file
logging.basicConfig(filename='bot.log', filemode='w', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(filename)s - %(lineno)d - %(message)s')
def random_delay(min_seconds, max_seconds):
time.sleep(random.uniform(min_seconds, max_seconds))
def update_restaurant_link(restaurant_link, date, seats):
# convert the date from string to datetime object and then return it to the correct format to avoid url errors
date = datetime.strptime(date, "%Y-%m-%d").strftime('%Y-%m-%d')
return restaurant_link.split("?")[0] + f"?seats={seats}&date={date}"
def login_to_resy(page, email, password):
"""Login to Resy with enhanced stability and error handling."""
try:
page.wait_for_selector('.AnnouncementModal__icon-close', timeout=5000)
page.click('.AnnouncementModal__icon-close')
except Exception:
logging.info("No announcement modal to close.")
# breakpoint()
page.click("text=Log in", timeout=30000)
page.click("text=Use Email and Password instead", timeout=30000)
page.fill('input[name="email"]', email)
page.fill('input[name="password"]', password)
# print("login")
# breakpoint()
# api_request_context = context.request
# page.request.post("https://api.resy.com/3/auth/password", headers=headers, data=payload,)
page.click('[name="login_form"] button', timeout=10000)
# time.sleep(10)
# page.get_by_role("button", name=re.compile("continue", re.IGNORECASE)).click()
# page.click("text=Continue", timeout=5000)
# page.click('/html/body/div[8]/div/div/div/div/div[2]/div[2]/div/form/div/button', timeout=5000)
# breakpoint()
# page.evaluate("document.querySelector('[name=\"login_form\"] button').click()")
page.evaluate("() => document.fonts.ready")
# breakpoint()
page.screenshot(path='debugging_photos/screenshot2.png')
logging.info("Logged in and screenshot taken.")
def reserve_restaurant(page, selected_reservation):
"""Reserve the restaurant with improved error handling and explicit waits."""
try:
selected_reservation.click()
frame_element = page.wait_for_selector('iframe[title="Resy - Book Now"]', timeout=10000)
frame = frame_element.content_frame()
frame.wait_for_selector('[data-test-id="order_summary_page-button-book"]', timeout=10000).click()
breakpoint()
confirmation_message = frame.query_selector('.ConfirmationPage__header').inner_text()
logging.info(f"Reservation confirmation message: {confirmation_message}")
logging.info("Reservation confirmed.")
page.evaluate("() => document.fonts.ready")
page.screenshot(path='debugging_photos/screenshot3.png')
except Exception as e:
logging.exception("Failed to complete reservation")
def main(restaurant_link, date_wanted, seats, time_wanted, period_wanted, reservation_type):
# breakpoint()
try:
# update the restaurant link
restaurant_link = update_restaurant_link(restaurant_link, date_wanted, seats)
# Rotate user agents or use more realistic ones
user_agents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
# 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0'
# More user agents can be added here
]
user_agent = random.choice(user_agents)
# Start the bot
with sync_playwright() as p:
'''browser_args = [
'--window-size=1300,570',
'--window-position=000,000',
'--disable-dev-shm-usage',
,
'--disable-features=site-per-process',
'--disable-setuid-sandbox',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--use-gl=egl',
'--disable-blink-features=AutomationControlled',
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-component-extensions-with-background-pages',
'--disable-default-apps',
'--disable-extensions',
'--disable-features=Translate',
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
,
'--disable-prompt-on-repost',
'--disable-renderer-backgrounding',
'--disable-sync',
'--force-color-profile=srgb',
'--metrics-recording-only',
,
'--password-store=basic',
'--use-mock-keychain',
'--hide-scrollbars',
'--mute-audio'
] '''
wargs = []
# wargs.append('--enable-logging=stderr')
wargs.append('--v=1')
wargs.append('--no-sandbox')
wargs.append('--enable-features=NetworkService,NetworkServiceInProcess')
wargs.append('--enable-automation')
wargs.append('--disable-popup-blocking')
wargs.append('--disable-web-security')
browser = p.chromium.launch(headless=headless, args=wargs)
proxy_server = "http://kpeqkzlp:[email protected]:5868"
context = browser.new_context(
user_agent=user_agent,
# viewport={'width': random.randint(1200, 1920), 'height': random.randint(900, 1080)},
viewport={'width': 1920, 'height': 1080},
permissions=['geolocation', 'notifications'],
java_script_enabled=True,
# no_viewport=True,
# bypass_csp=True,
locale='US_en',
# geolocation=False,
#proxy = {
#'server': proxy_server
#}
)
page = context.new_page()
stealth_sync(page)
page.on("console", lambda msg: logging.debug(f"PAGE LOG: {msg.text}"))
page.on("pageerror", lambda msg: logging.error(f"PAGE ERROR: {msg}"))
page.on("response", lambda response: logging.debug(f"RESPONSE: {response.url} {response.status}"))
page.on("requestfailed", lambda request: logging.error(f"REQUEST FAILED: {request.url} {request.failure}"))
logging.info("Bot is running...")
# Login to Resy
# breakpoint()
page.goto("https://resy.com", timeout=60000)
# breakpoint()
login_to_resy(page, email, password)
logging.info("Logged in successfully.")
random_delay(2, 5)
# Go to restaurant page
page.goto(restaurant_link, wait_until='networkidle')
# page.goto(restaurant_link)
page.wait_for_timeout(20000)
# Take screenshot for debugging
# breakpoint()
page.evaluate("() => document.fonts.ready")
page.screenshot(path="debugging_photos/screenshot1.png", timeout=120000)
# breakpoint()
menu = page.wait_for_selector(f'//div[contains(@class,"ShiftInventory__shift")][h2[text()="{period_wanted.lower()}"]]', timeout=120000)
selected_reservation = menu.query_selector(f'//button[div[text()="{time_wanted}"]][div[text()="{reservation_type.lower().title()}"]]')
if selected_reservation:
logging.info(
f"Reservation available at {time_wanted} for {seats} people {reservation_type.lower().title()}")
reserve_restaurant(page, selected_reservation)
random_delay(3, 6)
else:
logging.info("No reservation available")
except Exception as e:
# Show all error details in log file
logging.exception("An error occurred")
return e
if __name__ == '__main__':
restaurant_link = "https://resy.com/cities/london-england/venues/granger-and-co-chelsea?date=2024-05-15&seats=2"
# restaurant_link = "https://resy.com/cities/new-york-ny/venues/don-angie?seats=2&date=2024-05-15"
date_wanted = "2024-06-1"
seats = 2
time_wanted = "10:00 PM"
period_wanted = "Dinner"
reservation_type = "Dining Room"
main(restaurant_link, date_wanted, seats, time_wanted, period_wanted, reservation_type)