-
Notifications
You must be signed in to change notification settings - Fork 1
/
seerrbridge.py
612 lines (508 loc) · 29.2 KB
/
seerrbridge.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# =============================================================================
# Soluify.com | Your #1 IT Problem Solver | {SeerrBridge v0.1.2}
# =============================================================================
# __ _
# (_ _ | .(_
# __)(_)||_||| \/
# /
# © 2024
# -----------------------------------------------------------------------------
import discord
import asyncio
import time
import os
import logging
import sys
import urllib.parse
import re
import inflect
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from dotenv import load_dotenv
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from asyncio import Queue
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s:%(name)s: %(message)s',
handlers=[
logging.FileHandler("seerbridge.log", encoding='utf-8'), # Use utf-8 encoding for log file
logging.StreamHandler(sys.stdout) # Ensure stdout can handle Unicode
]
)
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
CHANNEL_ID = os.getenv('DISCORD_CHANNEL_ID')
try:
CHANNEL_ID = int(CHANNEL_ID)
except ValueError:
logger.error("DISCORD_CHANNEL_ID must be an integer.")
exit(1)
# Securely load credentials from environment variables
REAL_DEBRID_USERNAME = os.getenv('REAL_DEBRID_USERNAME')
REAL_DEBRID_PASSWORD = os.getenv('REAL_DEBRID_PASSWORD')
# Discord bot intents
intents = discord.Intents.default()
intents.messages = True # Enable message intent
intents.guilds = True
intents.message_content = True # Ensure message content intent is enabled
client = discord.Client(intents=intents)
# Global driver variable to hold the Selenium WebDriver
driver = None
# Initialize a global queue with a maximum size of 500
request_queue = Queue(maxsize=500)
processing_task = None # To track the current processing task
### Helper function to handle login
def login(driver):
logger.info("Initiating login process.")
try:
# Click the "Login with Real Debrid" button
login_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Login with Real Debrid')]"))
)
login_button.click()
logger.info("Clicked on 'Login with Real Debrid' button.")
# Click the "Authorize Debrid Media Manager" button
authorize_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Authorize Debrid Media Manager')]"))
)
authorize_button.click()
logger.info("Clicked on 'Authorize Debrid Media Manager' button.")
# Switch to the new tab for Real-Debrid login
driver.switch_to.window(driver.window_handles[-1])
# Fill in the username and password fields
username_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "u"))
)
username_field.send_keys(REAL_DEBRID_USERNAME)
logger.info("Entered username.")
password_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "p"))
)
password_field.send_keys(REAL_DEBRID_PASSWORD)
logger.info("Entered password.")
# Submit the login form
submit_button = driver.find_element(By.XPATH, "//input[@type='submit' and @value='Login']")
time.sleep(2)
submit_button.click()
logger.info("Submitted the login form.")
# Wait for the "Application allowed" message
WebDriverWait(driver, 30).until(
EC.text_to_be_present_in_element((By.XPATH, "//body"), "Application allowed, you can close this page")
)
logger.info("Login successful, authorization granted.")
time.sleep(2)
# Close the authorization tab and switch back to the debrid media manager tab
driver.switch_to.window(driver.window_handles[0])
except (TimeoutException, NoSuchElementException) as ex:
logger.error(f"Error during login process: {ex}")
### Browser Initialization and Persistent Session
async def initialize_browser():
global driver
if driver is None:
logger.info("Starting persistent browser session.")
options = Options()
options.add_argument('--headless') # Run browser in headless mode
options.add_argument('--disable-gpu') # Disable GPU to save resources
chromedriver_path = os.getenv('CHROMEDRIVER_PATH')
service = ChromeService(executable_path=chromedriver_path)
driver = webdriver.Chrome(service=service, options=options)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
logger.info("Initialized Selenium WebDriver.")
# Perform login once during browser initialization
driver.get("https://debridmediamanager.com")
logger.info("Navigated to Debrid Media Manager start page.")
login(driver)
logger.info("Logged into Debrid Media Manager.")
# After successful login, click on "⚙️ Settings" to open the settings popup
try:
logger.info("Attempting to click the '⚙️ Settings' link.")
settings_link = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'⚙️ Settings')]"))
)
settings_link.click()
logger.info("Clicked on '⚙️ Settings' link.")
# Wait for the settings popup to appear
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//h2[contains(text(),'⚙️ Settings')]"))
)
logger.info("Settings popup appeared.")
# Locate the "Default torrents filter" input box and insert the regex
logger.info("Attempting to insert regex into 'Default torrents filter' box.")
default_filter_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "dmm-default-torrents-filter"))
)
default_filter_input.clear() # Clear any existing filter
# Add custom Regex here
default_filter_input.send_keys("^(?!.*【.*?】)(?!.*[\u0400-\u04FF])(?!.*\[esp\]).*")
logger.info("Inserted regex into 'Default torrents filter' input box.")
# Confirm the changes by clicking the 'OK' button on the popup
ok_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[@class='swal2-confirm swal2-styled']"))
)
ok_button.click()
logger.info("Clicked 'OK' to save settings.")
except (TimeoutException, NoSuchElementException) as ex:
logger.error(f"Error while interacting with the settings popup: {ex}")
# Navigate to the library section
logger.info("Navigating to the library section.")
driver.get("https://debridmediamanager.com/library")
# Wait for 2 seconds on the library page before further processing
try:
# Ensure the library page has loaded correctly (e.g., wait for a specific element on the library page)
library_element = WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.XPATH, "//div[@id='library-content']")) # Adjust the XPath as necessary
)
logger.info("Library section loaded successfully.")
except TimeoutException:
logger.error("Library section did not load within the expected time.")
# Wait for at least 2 seconds on the library page
logger.info("Waiting for 2 seconds on the library page.")
time.sleep(2)
logger.info("Completed waiting on the library page.")
async def shutdown_browser():
global driver
if driver:
driver.quit()
logger.info("Selenium WebDriver closed.")
driver = None
### Function to process requests from the queue
async def process_requests():
while True:
movie_title = await request_queue.get() # Wait for the next request in the queue
logger.info(f"Processing movie request: {movie_title}")
try:
await asyncio.to_thread(search_on_debrid, movie_title, driver) # Process the request
except Exception as ex:
logger.error(f"Error processing movie request {movie_title}: {ex}")
finally:
request_queue.task_done() # Mark the request as done
### Function to add requests to the queue
async def add_request_to_queue(movie_title):
if request_queue.full():
logger.warning(f"Request queue is full. Cannot add movie: {movie_title}")
return False
await request_queue.put(movie_title)
logger.info(f"Added movie request to queue: {movie_title}")
return True
### Helper function to extract year from a string
def extract_year(text):
match = re.search(r'\d{4}', text)
if match:
return int(match.group(0))
return None
# Initialize the inflect engine for number-word conversion
p = inflect.engine()
def clean_title(title):
"""
Cleans the movie title by removing commas, hyphens, colons, semicolons, and apostrophes,
and converting to lowercase. Also replaces multiple spaces with a single space.
"""
# Remove commas, hyphens, colons, semicolons, and apostrophes
cleaned_title = re.sub(r"[,:;'-]", '', title)
# Replace multiple spaces with a single dot
cleaned_title = re.sub(r'\s+', '.', cleaned_title)
# Convert to lowercase for comparison
return cleaned_title.lower()
def normalize_title(title):
"""
Normalizes the title by ensuring there are no unnecessary spaces or dots,
and converts to lowercase (unclean version).
"""
# Replace multiple spaces with a single space and dots with spaces
normalized_title = re.sub(r'\s+', ' ', title)
normalized_title = normalized_title.replace('.', ' ')
# Convert to lowercase
return normalized_title.lower()
def replace_numbers_with_words(title):
"""
Replaces digits with their word equivalents (e.g., "3" to "three").
"""
return re.sub(r'\b\d+\b', lambda x: p.number_to_words(x.group()), title)
def replace_words_with_numbers(title):
"""
Replaces number words with their digit equivalents (e.g., "three" to "3").
"""
words_to_numbers = {
"zero": "0", "one": "1", "two": "2", "three": "3", "four": "4",
"five": "5", "six": "6", "seven": "7", "eight": "8", "nine": "9",
"ten": "10", "eleven": "11", "twelve": "12", "thirteen": "13",
"fourteen": "14", "fifteen": "15", "sixteen": "16", "seventeen": "17",
"eighteen": "18", "nineteen": "19", "twenty": "20"
# Add more mappings as needed
}
# Replace word numbers with digits
for word, digit in words_to_numbers.items():
title = re.sub(rf'\b{word}\b', digit, title, flags=re.IGNORECASE)
return title
### Search Function to Reuse Browser
def search_on_debrid(movie_title, driver):
logger.info(f"Starting Selenium automation for movie: {movie_title}")
# Check if the driver is None before proceeding to avoid NoneType errors
if not driver:
logger.error("Selenium WebDriver is not initialized. Attempting to reinitialize.")
driver = initialize_browser()
debrid_media_manager_base_url = "https://debridmediamanager.com/search?query="
# Use urllib to encode the movie title safely, handling all special characters including '&', ':', '(', ')'
encoded_movie_title = urllib.parse.quote(movie_title)
url = debrid_media_manager_base_url + encoded_movie_title
logger.info(f"Search URL: {url}")
try:
# Directly jump to the search results page after login
driver.get(url)
logger.info(f"Navigated to search results page for {movie_title}.")
time.sleep(3) # Allow time for search results to load
# Clean the movie title (remove year in parentheses)
movie_title_cleaned = movie_title.split('(')[0].strip()
logger.info(f"Searching for movie title: {movie_title_cleaned}")
# Find the movie result elements
try:
movie_elements = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, f"//a[contains(@href, '/movie/')]"))
)
# Iterate over the movie elements to find the correct one
for movie_element in movie_elements:
movie_title_element = movie_element.find_element(By.XPATH, ".//h3")
movie_year_element = movie_element.find_element(By.XPATH, ".//div[contains(@class, 'text-gray-600')]")
search_title_cleaned = movie_title_element.text.strip()
search_year = extract_year(movie_year_element.text)
# Extract the expected year from the provided movie title (if it exists)
expected_year = extract_year(movie_title)
# Check if the titles match and if the years are within ±1 year range
if search_title_cleaned.lower() == movie_title_cleaned.lower() and \
(expected_year is None or abs(search_year - expected_year) <= 1):
logger.info(f"Found matching movie: {search_title_cleaned} ({search_year})")
# Click on the parent <a> tag (which is the clickable link)
parent_link = movie_element
parent_link.click()
logger.info(f"Clicked on the movie link for {search_title_cleaned}")
break
else:
logger.error(f"No matching movie found for {movie_title_cleaned} ({expected_year})")
return
except (TimeoutException, NoSuchElementException) as e:
logger.error(f"Failed to find or click on the search result: {movie_title}")
return
# Wait for the movie's details page to load by listening for the status message
try:
# Step 1: Wait for the "Checking RD availability..." message to appear
logger.info("Waiting for 'Checking RD availability...' to appear.")
WebDriverWait(driver, 60).until(
EC.text_to_be_present_in_element(
(By.XPATH, "//div[@role='status' and contains(@aria-live, 'polite')]"),
"Checking RD availability"
)
)
logger.info("'Checking RD availability...' has appeared. Waiting for it to disappear & Checking if already available.")
# Step 2: Check if any red button (RD 100%) exists immediately, and skip if found
try:
red_button = driver.find_element(By.XPATH, "//button[contains(@class, 'bg-red-500')]")
logger.info("Red button (100% RD) detected. Skipping this entry.")
return # Skip this entry if the red button is detected
except NoSuchElementException:
logger.info("No red button (100% RD) detected. Proceeding with 'Checking RD availability...'.")
# Step 3: Wait for the "Checking RD availability..." message to disappear
try:
WebDriverWait(driver, 60).until_not(
EC.text_to_be_present_in_element(
(By.XPATH, "//div[@role='status' and contains(@aria-live, 'polite')]"),
"Checking RD availability"
)
)
logger.info("'Checking RD availability...' has disappeared. Now waiting for RD results.")
except TimeoutException:
logger.warning("'Checking RD availability...' did not disappear within 60 seconds. Proceeding to the next steps.")
# Step 4: Wait for the "Found X available torrents in RD" message
try:
status_element = WebDriverWait(driver, 60).until(
EC.presence_of_element_located(
(By.XPATH, "//div[@role='status' and contains(@aria-live, 'polite') and contains(text(), 'available torrents in RD')]")
)
)
status_text = status_element.text
logger.info(f"Status message: {status_text}")
except TimeoutException:
logger.warning("Timeout waiting for the RD status message. Proceeding with the next steps.")
status_text = None # No status message found, but continue
# Step 5: Extract the number of available torrents from the status message (look for the number)
if status_text:
torrents_match = re.search(r"Found (\d+) available torrents in RD", status_text)
if torrents_match:
torrents_count = int(torrents_match.group(1))
logger.info(f"Found {torrents_count} available torrents in RD.")
else:
logger.warning("Could not find the expected 'Found X available torrents in RD' message. Proceeding to check for Instant RD.")
torrents_count = 0 # Default to 0 torrents if no match found
else:
logger.warning("No status text available. Proceeding to check for Instant RD.")
torrents_count = 0 # Default to 0 torrents if no status text
# Step 6: If the status says "0 torrents", check if there's still an Instant RD button
if torrents_count == 0:
logger.info("No torrents found in RD according to status, but checking for Instant RD buttons.")
else:
logger.info(f"{torrents_count} torrents found in RD. Proceeding with RD checks.")
# Step 7: Check if any red button (RD 100%) exists again before continuing
try:
red_button = driver.find_element(By.XPATH, "//button[contains(@class, 'bg-red-500')]")
logger.info("Red button (100% RD) detected after RD check. Skipping this entry.")
return # Skip this entry if the red button is detected
except NoSuchElementException:
logger.info("No red button (100% RD) detected. Proceeding with Instant RD check.")
# After clicking the matched movie title, we now check the popup boxes for Instant RD buttons
# Step 8: Check the result boxes with the specified class for "Instant RD"
try:
result_boxes = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, "//div[contains(@class, 'border-black')]"))
)
for i, result_box in enumerate(result_boxes, start=1):
try:
# Extract the title from the result box
title_element = result_box.find_element(By.XPATH, ".//h2")
title_text = title_element.text.strip()
logger.info(f"Box {i} title: {title_text}")
# Extract the year from the title
box_year = extract_year(title_text)
if box_year is None:
logger.warning(f"Could not extract year from '{title_text}'. Skipping box {i}.")
continue
# Clean both the movie title and the box title for comparison
movie_title_cleaned = clean_title(movie_title.split('(')[0].strip())
title_text_cleaned = clean_title(title_text.split(str(box_year))[0].strip())
# Normalize both titles for unclean comparison
movie_title_normalized = normalize_title(movie_title.split('(')[0].strip())
title_text_normalized = normalize_title(title_text.split(str(box_year))[0].strip())
# Convert digits to words for comparison
movie_title_cleaned_word = replace_numbers_with_words(movie_title_cleaned)
title_text_cleaned_word = replace_numbers_with_words(title_text_cleaned)
movie_title_normalized_word = replace_numbers_with_words(movie_title_normalized)
title_text_normalized_word = replace_numbers_with_words(title_text_normalized)
# Convert words to digits for comparison
movie_title_cleaned_digit = replace_words_with_numbers(movie_title_cleaned)
title_text_cleaned_digit = replace_words_with_numbers(title_text_cleaned)
movie_title_normalized_digit = replace_words_with_numbers(movie_title_normalized)
title_text_normalized_digit = replace_words_with_numbers(title_text_normalized)
# Log all variations for debugging
logger.info(f"Cleaned movie title: {movie_title_cleaned}, Cleaned box title: {title_text_cleaned}")
logger.info(f"Normalized movie title: {movie_title_normalized}, Normalized box title: {title_text_normalized}")
logger.info(f"Movie title (digits to words): {movie_title_cleaned_word}, Box title (digits to words): {title_text_cleaned_word}")
logger.info(f"Movie title (words to digits): {movie_title_cleaned_digit}, Box title (words to digits): {title_text_cleaned_digit}")
# Compare the title in all variations
if not (
title_text_cleaned.startswith(movie_title_cleaned) or
title_text_normalized.startswith(movie_title_normalized) or
title_text_cleaned_word.startswith(movie_title_cleaned_word) or
title_text_normalized_word.startswith(movie_title_normalized_word) or
title_text_cleaned_digit.startswith(movie_title_cleaned_digit) or
title_text_normalized_digit.startswith(movie_title_normalized_digit)
):
logger.info(f"Title mismatch for box {i}: {title_text_cleaned} or {title_text_normalized} (Expected: {movie_title_cleaned} or {movie_title_normalized}). Skipping.")
continue # Skip this box if none of the variations match
# Compare the year with the expected year (allow ±1 year)
expected_year = extract_year(movie_title)
if expected_year is not None and abs(box_year - expected_year) > 1:
logger.info(f"Year mismatch for box {i}: {box_year} (Expected: {expected_year}). Skipping.")
continue # Skip this box if the year doesn't match
# Check the file count (ensure it's 1 folder)
file_info_element = result_box.find_element(By.XPATH, ".//div[contains(@class, 'text-gray-300')]")
file_info_text = file_info_element.text
file_count_match = re.search(r"\((\d+) 📂\)", file_info_text)
if file_count_match:
file_count = int(file_count_match.group(1))
if file_count > 1:
logger.info(f"Box {i} has {file_count} files (Expected: 1). Skipping.")
continue # Skip this box if there is more than 1 file
else:
logger.warning(f"Could not extract file count from '{file_info_text}' in box {i}. Skipping.")
continue
# Try to locate the Instant RD button using the button's class
instant_rd_button = result_box.find_element(By.XPATH, ".//button[contains(@class, 'bg-green-600')]")
instant_rd_button.click()
logger.info(f"Clicked 'Instant RD' in box {i} for {title_text} ({box_year}).")
time.sleep(2)
# After clicking, check if the button has changed to "RD (0%)" or "RD (100%)"
try:
rd_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, ".//button[contains(text(), 'RD (')]"))
)
rd_button_text = rd_button.text
logger.info(f"RD button text after clicking: {rd_button_text}")
# If the button is now "RD (0%)", undo the click and retry with the next box
if "RD (0%)" in rd_button_text:
logger.warning(f"RD (0%) button detected after clicking Instant RD in box {i} {title_text}. Undoing the click and moving to the next box.")
rd_button.click() # Undo the click by clicking the RD (0%) button
continue # Move to the next box
# If it's "RD (100%)", we are done with this entry
if "RD (100%)" in rd_button_text:
logger.info(f"RD (100%) button detected. {i} {title_text}. This entry is complete.")
break # Break out of the loop since the task is complete
except TimeoutException:
logger.warning(f"Timeout waiting for RD button status change in box {i}.")
continue # Move to the next box if a timeout occurs
except NoSuchElementException as e:
logger.warning(f"Could not find 'Instant RD' button in box {i}: {e}")
except TimeoutException as e:
logger.warning(f"Timeout when processing box {i}: {e}")
except TimeoutException:
logger.warning("Timeout waiting for result boxes to appear.")
except TimeoutException:
logger.warning("Timeout waiting for the RD status message.")
return
except Exception as ex:
logger.error(f"Error during Selenium automation: {ex}")
### Discord Bot Logic
@client.event
async def on_ready():
global processing_task
logger.info(f'Bot logged in as {client.user}')
await initialize_browser() # Initialize the browser when the bot is ready
if processing_task is None: # Start the request processing task if not already started
processing_task = asyncio.create_task(process_requests())
logger.info("Started request processing task.")
@client.event
async def on_message(message):
logger.info(f"Message received in channel {message.channel.id}")
if message.author == client.user:
return
if message.channel.id == CHANNEL_ID:
logger.info("Message is in the correct channel.")
if message.embeds:
logger.info("Message contains embeds. Processing embeds...")
for embed in message.embeds:
logger.info(f"Embed title: {embed.title}")
logger.info(f"Embed description: {embed.description}")
if embed.author and embed.author.name == "Movie Request Automatically Approved":
logger.info(f"Detected a movie request from author: {embed.author.name}")
movie_title = embed.title.strip() if embed.title else None
if movie_title:
logger.info(f"New movie request detected: {movie_title}")
success = await add_request_to_queue(movie_title) # Add request to the queue
if not success:
await message.channel.send("Request queue is full. Please try again later.")
else:
logger.warning("Embed title is missing. Skipping.")
@client.event
async def on_disconnect():
logger.warning("Bot disconnected, attempting to reconnect...")
@client.event
async def on_close():
await shutdown_browser() # Ensure browser is closed when the bot closes
# Run the Discord client
if __name__ == "__main__":
try:
logger.info("Starting Discord bot...")
client.run(TOKEN)
except Exception as e:
logger.critical(f"Failed to run the Discord bot: {e}")
asyncio.run(shutdown_browser()) # Ensure browser is closed on critical failure