-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
102 lines (84 loc) · 3.25 KB
/
script.py
File metadata and controls
102 lines (84 loc) · 3.25 KB
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
import time
import os
import random
from datetime import datetime, timedelta
import requests
import pytz
from bs4 import BeautifulSoup
# get start/end time
jst = pytz.timezone("Asia/Tokyo")
start_time_str = os.environ.get("START_TIME")
end_time_str = os.environ.get("END_TIME")
start_time = jst.localize(datetime.strptime(start_time_str, "%H:%M:%S")).time()
end_time = jst.localize(datetime.strptime(end_time_str, "%H:%M:%S")).time()
webhook_url = os.environ.get("WEBHOOK_URL")
# Define the URL of the website you want to poll
website_url = "https://omakase.in/en/r/gv412216"
button_selector = (
".p-r_reserve_action_reserve" # Replace with the actual selector for your button
)
# set up session
session_cookie = os.environ.get("SESSION_COOKIE")
session = requests.Session()
session.headers.update({"Cookie": session_cookie})
# Function to check the button's disabled status
last_button_text = None
same_button_text_messages = 0
def check_button_status():
global last_button_text
global same_button_text_messages
try:
# Send an HTTP GET request to the website
response = response = session.get(website_url)
response.raise_for_status() # Raise an exception for HTTP errors
# Parse the HTML content
soup = BeautifulSoup(response.text, "html.parser")
# Find the button element by its selector
button = soup.select_one(button_selector).find_all()[0]
print(button)
new_button_text = button.get_text().strip()
if last_button_text != new_button_text:
last_button_text = new_button_text
same_button_text_messages = 0
else:
same_button_text_messages += 1
if button is not None and "disabled" in button.get("class", []):
if "login" in button.get_text():
requests.post(
webhook_url,
json={
"username": "Sawada Monitor",
"icon_emoji": ":cookie:",
"text": f"Current button text:\n{new_button_text}\nCookie expired, provide a new one ASAP *<{website_url}|CHECK HERE>*",
},
)
else:
if same_button_text_messages < 3:
requests.post(
webhook_url,
json={
"username": "Sawada Monitor",
"icon_emoji": ":sushi:",
"text": f"Current button text:\n{new_button_text}\nSlots may be available, *<{website_url}|CHECK HERE>*",
},
)
else:
print("No message sent to slack, since button text hasn't changed:")
print(new_button_text)
except Exception as e:
print(f"Error: {e}")
current_time = datetime.now(jst).time()
in_time_window = False
if start_time < end_time:
in_time_window = start_time <= current_time <= end_time
else:
in_time_window = start_time <= current_time or current_time <= end_time
if in_time_window:
print("in time window")
while True:
check_button_status()
time.sleep(random.randint(5, 20))
else:
print("out of time window, checking for cookie validity")
time.sleep(random.randint(0, 5 * 60))
check_button_status()