-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
499 lines (432 loc) · 23.8 KB
/
bot.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
import requests
from bs4 import BeautifulSoup
import zipfile
import os
import telebot
from urllib.parse import quote_plus
import re
from telebot import types
import datetime
from config import config, logging
from connectors import database
from helpers.fetch_series import fetch_series
from helpers.fetch_series_names import fetch_series_names
from helpers.zip_helper import download_extract_zip
from config.logging import logger
bot = telebot.TeleBot(config.TOKEN)
logging.set_bot(bot)
database.create_table_movie()
database.create_table_tv()
@bot.message_handler(commands=['start'])
def send_welcome(message):
try:
welcome_message = "👋 *Welcome to the Sinhala Subtitle Download Bot!* 🎬\n\n"
welcome_message += ("I'm here to help you find and download Sinhala subtitles for your favorite movies. Here's "
"what I can do:\n\n")
welcome_message += ("1. 🎥 **Search for Movies**: You can search for a movie by its name, and I'll find the "
"Sinhala subtitles for it. To do this, use the `/movie` command followed by the movie name. "
"For example, `/movie Titanic`.\n\n")
welcome_message += (
"2. 📺 **Search for TV Series**: You can search for a tv series by its name, and I'll find the "
"Sinhala subtitles for it. To do this, use the `/tv` command followed by the series name. For "
"example, `/tv Breaking Bad`.\n\n")
welcome_message += (
"*Disclaimer*: _This bot merely provides a means to share subtitles found on the internet. All "
"subtitles shared by this bot are the property of their respective owners. Any credits and "
"intellectual property rights associated with the subtitles belong solely to the original "
"owners. This bot does not claim any ownership or responsibility for the subtitles shared._ 📝")
photo = open('images/icon.jpg', 'rb')
bot.send_photo(message.chat.id, photo, caption=welcome_message, parse_mode='Markdown')
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.message_handler(commands=['help'])
def send_help(message):
try:
help_message = "👋 *Welcome to the Sinhala Subtitle Download Bot!* 🎬\n\n"
help_message += ("Here's what I can do:\n\n")
help_message += ("1. 🎥 **Search for Movies**: You can search for a movie by its name, and I'll find the "
"Sinhala subtitles for it. To do this, use the `/movie` command followed by the movie name. "
"For example, `/movie Titanic`.\n\n")
help_message += (
"2. 📺 **Search for TV Series**: You can search for a tv series by its name, and I'll find the "
"Sinhala subtitles for it. To do this, use the `/tv` command followed by the series name. For "
"example, `/tv Breaking Bad`.\n\n")
help_message += (
"3. 🎬 **Inline Search**: You can also search for a movie or TV series by mentioning the bot in any chat, "
"followed by the name of the movie or TV series. For example, `@sinhalasub_bot Titanic`. This may not always "
"show results because it only shows items that have been downloaded and are in the database.\n\n")
bot.reply_to(message, help_message, parse_mode='Markdown')
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.message_handler(commands=['movie'])
def search_movie(message):
try:
logger.info("Received a request to search for a movie.")
msg = bot.send_message(message.chat.id, "🔍 Searching....")
# Check if the command is exactly '/movie'
if message.text.strip() == '/movie':
logger.info("No movie name provided. Requesting movie name.")
bot.edit_message_text("Please provide a movie name. For example, `/movie Titanic`.", msg.chat.id,
msg.message_id)
return
# Get the movie name from the message
movie_name = message.text.split('/movie ', 1)[1].strip()
# Search for the movie on the website
search_url = f'{config.HOST_URL}/?s={quote_plus(movie_name)}'
r = requests.get(search_url)
soup = BeautifulSoup(r.text, 'html.parser')
# Find all movie titles and links
movies = []
for h2 in soup.find_all('h2', class_='entry-title'):
if len(movies) >= 5: # Stop after finding 5 movies
logger.info("Found 5 movies. Breaking the loop.")
break
a = h2.find('a', href=True)
text = a.text
match = re.search(r'\(\d{4}\)', text)
if match:
# This is a movie, add it to the list
title = text[:match.end()].strip()
search_title = title.split(' (')[0].strip()
link = a['href']
# get the movie details
tmdb_url = f'https://api.themoviedb.org/3/search/movie?api_key={config.TMDB_API_KEY}&query={search_title}'
tmdb_response = requests.get(tmdb_url).json()
if tmdb_response['results']:
movie_data = tmdb_response['results'][0]
movies.append((title, link, movie_data))
logger.info(f"Added movie to the list: {title}")
if not movies:
logger.info("No movies found matching the search.")
bot.edit_message_text("I'm sorry, but I couldn't find any movies matching your search.", msg.chat.id,
msg.message_id)
return
movie_message = ""
for title, link, movie_data in movies:
movie_id = movie_data['id']
title = movie_data['title']
overview = movie_data['overview']
poster = movie_data['poster_path']
year = re.search(r'\d{4}', movie_data['release_date']).group()
database.insert_details(movie_id, title, year, link, overview, poster)
logger.info(f"Inserted movie details into the database: {title}, Year: {year}")
movie_message += f"🎬 *{title}* ({year})\n"
movie_message += f"_Download:_ /dl\_{movie_id}\n\n"
# Send the message
logger.info("Sending the message with the found movies.")
bot.edit_message_text(movie_message, msg.chat.id, msg.message_id, parse_mode='Markdown')
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.message_handler(regexp='^/dl')
def download_subtitle(message):
try:
logger.info("Received a request to download subtitles.")
movie_id = message.text.split('/dl_', 1)[1].strip()
result = database.get_link(movie_id)
if result is None:
logger.error("Invalid command. No link found for the provided movie ID.")
bot.reply_to(message, "This command is incorrect. Please provide a valid command.")
return
chat_dir = f'subtitles/movies/{movie_id}'
movie_message = ""
movie_message += f"🎬 *{result[1]}* ({result[2]})\n\n"
movie_message += f"{result[4]}\n\n"
bot.send_photo(message.chat.id, f"https://image.tmdb.org/t/p/original{result[3]}",
caption=movie_message, parse_mode='Markdown')
msg = bot.send_message(message.chat.id, "⏫ Uploading the subtitles...")
# check if directory exists
if os.path.isdir(chat_dir):
logger.info("Directory exists. Sending all .srt files.")
# Send all .srt files
for file in os.listdir(chat_dir):
if file.endswith('.srt'):
with open(os.path.join(chat_dir, file), 'rb') as f:
bot.send_document(message.chat.id, f)
bot.edit_message_text(f"✅ {result[1]} ({result[2]}) Subtitle Uploaded", msg.chat.id, msg.message_id,
parse_mode='Markdown')
else:
logger.info("Directory does not exist. Creating directory and downloading subtitles.")
bot.edit_message_text("⏳ Almost Done...", msg.chat.id, msg.message_id, parse_mode='Markdown')
# Create a unique directory for this chat
os.makedirs(chat_dir, exist_ok=True)
download_extract_zip(result[0], chat_dir, bot, msg)
# Send all .srt files
for file in os.listdir(chat_dir):
if file.endswith(('.srt', '.ass', '.ssa', '.vtt', '.stl', '.scc', '.ttml', '.sbv', '.idx', '.sub')):
with open(os.path.join(chat_dir, file), 'rb') as f:
bot.send_document(message.chat.id, f)
bot.edit_message_text(f"✅ {result[1]} ({result[2]}) Subtitle Uploaded", msg.chat.id, msg.message_id,
parse_mode='Markdown')
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.message_handler(commands=['tv'])
def search_tv(message):
try:
logger.info("Received a request to search for a TV series.")
msg = bot.send_message(message.chat.id, "🔍 Searching for the tv series...")
# Check if the command is exactly '/tv'
if message.text.strip() == '/tv':
logger.info("No series name provided. Requesting series name.")
bot.edit_message_text("Please provide a tv series name. For example, `/tv breaking bad`.", msg.chat.id,
msg.message_id)
return
# Get the series name from the message
series_name = message.text.split('/tv ', 1)[1].strip()
logger.info(f"Series name: {series_name}")
# Find if tv series already on the database
series = database.search_tv_series(series_name)
if not series:
# Fetch series names from your website
series_names = fetch_series_names(config.HOST_URL, series_name)
if not series_names:
logger.info("No series found matching the search.")
bot.edit_message_text("I'm sorry, but I couldn't find any series matching your search", msg.chat.id,
msg.message_id)
return
tv_message = ""
for name in series_names:
# Get series details from TMDB
tmdb_url = f'https://api.themoviedb.org/3/search/tv?api_key={config.TMDB_API_KEY}&query={quote_plus(name)}'
tmdb_response = requests.get(tmdb_url).json()
if tmdb_response['results']:
series_data = tmdb_response['results'][0]
series_id = series_data['id']
year = re.search(r'\d{4}', series_data['first_air_date']).group()
logger.info(f"Found series: {series_data['name']} ({year})")
tv_message += f"🎬 *{series_data['name']}* ({year})\n"
tv_message += f"_Select:_ /s\_{series_id}\n\n"
# Send the message
logger.info("Sending the message with the found series.")
bot.edit_message_text(tv_message, msg.chat.id, msg.message_id, parse_mode='Markdown')
else:
bot.edit_message_text("found", msg.chat.id, msg.message_id, parse_mode='Markdown')
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.message_handler(regexp='^/s')
def download_subtitle(message):
try:
logger.info("Received a request to download subtitles.")
msg = bot.send_message(message.chat.id, "🔍 Searching for the subtitles...")
series_id = message.text.split('/s_', 1)[1].strip()
logger.info(f"Series ID: {series_id}")
tmdb_url = f'https://api.themoviedb.org/3/tv/{series_id}?api_key={config.TMDB_API_KEY}'
response = requests.get(tmdb_url)
if response.status_code == 200:
logger.info("Successfully retrieved series information from TMDB.")
tmdb_response = response.json()
series_name = tmdb_response['name']
year = re.search(r'\d{4}', tmdb_response['first_air_date']).group()
overview = tmdb_response['overview']
poster_path = tmdb_response['poster_path']
results = database.check_series_available(series_id)
current_datetime = datetime.datetime.now().strftime('%Y-%m-%d')
if results and current_datetime[:10] == results[0][6]:
logger.info("Series is available in the database & its up to date")
row = []
tv_message = ""
# Create a message with a keyboard of seasons
seasons = sorted(set(season for series_name, year, season, episode, link, overview, updated in results))
keyboard = types.InlineKeyboardMarkup()
tv_message += f"🎬 *{results[0][0]}* ({results[0][1]})\n\n"
tv_message += f"{results[0][5]}\n\n"
tv_message += f"Please select a season:"
for season in seasons:
button = types.InlineKeyboardButton(text=f"Season {season}",
callback_data=f"series_{series_id}_season_{season}")
row.append(button)
if len(row) == 3:
keyboard.row(*row)
row = []
if row:
keyboard.row(*row)
bot.send_photo(msg.chat.id, f"https://image.tmdb.org/t/p/original{tmdb_response['poster_path']}",
caption=tv_message, reply_markup=keyboard, parse_mode='Markdown')
bot.delete_message(msg.chat.id, msg.message_id, timeout=None)
else:
logger.info("Series is not available in the database or not uptodate. Fetching series.")
series = fetch_series(config.HOST_URL, series_name, series_id, year, overview, poster_path)
if not series:
bot.edit_message_text("I'm sorry, but I couldn't find any series matching your search.",
msg.chat.id,
msg.message_id)
return
row = []
tv_message = ""
# Create a message with a keyboard of seasons
seasons = sorted(set(season for title, season, episode, link in series))
keyboard = types.InlineKeyboardMarkup()
tv_message += f"🎬 *{series_name}* ({year})\n\n"
tv_message += f"{overview}\n\n"
tv_message += f"Please select a season:"
for season in seasons:
button = types.InlineKeyboardButton(text=f"Season {season}",
callback_data=f"series_{series_id}_season_{season}")
row.append(button)
if len(row) == 3:
keyboard.row(*row)
row = []
if row:
keyboard.row(*row)
bot.send_photo(msg.chat.id, f"https://image.tmdb.org/t/p/original{poster_path}",
caption=tv_message, reply_markup=keyboard, parse_mode='Markdown')
bot.delete_message(msg.chat.id, msg.message_id, timeout=None)
elif response.status_code == 404:
logger.error("Could not find a series with the provided ID.")
bot.send_message(message.chat.id, "I'm sorry, but I couldn't find a series with that ID.")
else:
logger.error("An error occurred while trying to retrieve the series information.")
bot.send_message(message.chat.id, "Something went wrong. Please try again later.")
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.callback_query_handler(func=lambda call: call.data.startswith('series_'))
def handle_season_button(call):
try:
# Extract the series ID and season number from the callback data
_, series_id, _, season = call.data.split('_')
series_id = int(series_id)
season = int(season)
logger.info(f"Handling season button for series_id {series_id} and season {season}")
msg = bot.send_message(call.message.chat.id, "🔍 Searching for the subtitles...")
# Define the directory of the season
season_dir = f'subtitles/series/{series_id}/{season}'
# Retrieve the episode numbers and links from the database
episodes = database.get_series_links(series_id, season)
# Check if the season directory exists
if not os.path.isdir(season_dir):
# Download all subtitles and save them
for season, episode, link, updated, series_name in episodes:
chat_dir = f'subtitles/series/{series_id}/{season}/{episode}'
os.makedirs(chat_dir, exist_ok=True)
# download & extract zip
download_extract_zip(link, chat_dir, bot, msg)
else:
logger.info(f"Subtitles for series_id {series_id} and season {season} already exist")
# Create a message with a keyboard of episodes
keyboard = types.InlineKeyboardMarkup()
row = []
for season, episode, link, updated, series_name in episodes:
# Check if subtitles exist for the episode
subtitle_exists = os.path.isdir(f'subtitles/series/{series_id}/{season}/{episode}')
# Add a check or uncheck emoji based on whether subtitles exist
episode_text = f"✅ E{episode}" if subtitle_exists else f"❌ E{episode}"
callback_data = f"episode_{episode}_season_{season}_series_id_{series_id}"
button = types.InlineKeyboardButton(text=episode_text, callback_data=callback_data)
row.append(button)
if len(row) == 4:
keyboard.row(*row)
row = []
if row:
keyboard.row(*row)
download_all_button = types.InlineKeyboardButton(text="📥 Download All (.zip)",
callback_data=f"zip_{series_id}_season_{season}")
keyboard.row(download_all_button)
message_season = f"*Download {episodes[0][4]} : Season {season}*\n _(Last updated: {episodes[0][3]})_"
bot.edit_message_text(message_season, msg.chat.id, msg.message_id, reply_markup=keyboard, parse_mode='Markdown')
logger.info(f"Sent message for series_id {series_id} and season {season}")
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.callback_query_handler(func=lambda call: call.data.startswith('episode_'))
def download_subtitle(call):
try:
# Extract the series ID, season number, and episode number from the callback data
_, episode, _, season, _, series_id = call.data.split('_', 5)
series_id = int(series_id.split('_')[1])
season = int(season)
episode = int(episode)
logger.info(f"Uploading subtitles for series_id {series_id}, season {season}, episode {episode}")
msg = bot.send_message(call.message.chat.id, "⏫ Uploading the subtitles...")
name = database.get_series_name(series_id)
exists = f'subtitles/series/{series_id}/{season}/{episode}'
# check if directory exists
if os.path.isdir(exists):
for file in os.listdir(exists):
if file.endswith(('.srt', '.ass', '.ssa', '.vtt', '.stl', '.scc', '.ttml', '.sbv', '.idx', '.sub')):
with open(os.path.join(exists, file), 'rb') as f:
bot.send_document(call.message.chat.id, f)
bot.edit_message_text(f"✅ {name} S{season}:E{episode} Subtitle Uploaded", msg.chat.id, msg.message_id,
parse_mode='Markdown')
else:
bot.reply_to(call.message, "The subtitles for this episode are not available. Please try another episode.")
logger.error(f"Subtitles not found for series_id {series_id}, season {season}, episode {episode}")
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.callback_query_handler(func=lambda call: call.data.startswith('zip_'))
def zip_download(call):
try:
# Extract the series ID and season number from the callback data
_, series_id, _, season = call.data.split('_')
series_id = int(series_id)
season = int(season)
logger.info(f"Downloading & Compressing subtitles for series_id {series_id} and season {season}")
msg = bot.send_message(call.message.chat.id, "🗜️ Compressing the subtitles...")
# Get the series name from the database
series_name = database.get_series_name(series_id)
# Define the directory of the season
season_dir = f'subtitles/series/{series_id}/{season}'
# Define the zip file name
zip_file_name = os.path.join(season_dir, f'{series_name} - Season {season}.zip')
# Check if the zip file already exists
if not os.path.exists(zip_file_name):
# Create a ZipFile object in write mode
with zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
# Walk the directory tree and add files to the ZipFile object
for root, dirs, files in os.walk(season_dir):
for file in files:
# Skip .zip files
if not file.endswith('.zip'):
file_path = os.path.join(root, file)
zipf.write(file_path, arcname=os.path.relpath(file_path, start=season_dir))
# Send the zip file to the user
with open(zip_file_name, 'rb') as zipf:
bot.send_document(call.message.chat.id, zipf)
bot.edit_message_text(f'✅ {series_name} - Season {season} zip file uploaded', msg.chat.id, msg.message_id,
parse_mode='Markdown')
logger.info(f'Sent zip file for series_id {series_id} and season {season}')
except Exception as e:
logger.error(f"An error occurred: {e}")
@bot.inline_handler(lambda query: len(query.query) > 0)
def query_text(inline_query):
try:
# Find series names that match the query
series_rows = database.find_series_name_search(inline_query)
# Find movie names that match the query
movie_rows = database.find_movie_name_search(inline_query)
# Create an InlineQueryResultArticle for each matching row
results = []
for i, row in enumerate(series_rows):
r = types.InlineQueryResultArticle(
id=str(i),
thumbnail_url=f"https://image.tmdb.org/t/p/original{row[3]}",
title=row[0] + ' ' + '(' + row[2] + ') [Tv]',
description=row[4],
input_message_content=types.InputTextMessageContent(
message_text="/s_" + row[1]
)
)
results.append(r)
for i, row in enumerate(movie_rows):
r = types.InlineQueryResultArticle(
id=str(i),
thumbnail_url=f"https://image.tmdb.org/t/p/original{row[3]}",
title=row[0] + ' ' + '(' + row[2] + ') [Movie]',
description=row[4],
input_message_content=types.InputTextMessageContent(
message_text="/dl_" + row[1]
)
)
results.append(r)
# If no results were found, add a custom message
if not results:
r = types.InlineQueryResultArticle(
id='no_results',
title='No results, Try /tv or /movie commands',
input_message_content=types.InputTextMessageContent(
message_text="/help"
)
)
results.append(r)
# Send the results
bot.answer_inline_query(inline_query.id, results)
except Exception as e:
print(e)
bot.polling()