-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrends.py
65 lines (50 loc) · 2.45 KB
/
trends.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
import random
import asyncio
from telegram import InlineKeyboardMarkup, InlineKeyboardButton
from constants import TMDB_BEARER_TOKEN
from utils import clean_movie_name_for_api
from messengers import send_message_to_all_users
from dal import suggest_trending_movies, clear_trending_movie, mark_trending_movie, get_trending_movies
async def send_trending_movies() -> None:
"""Send a message with trending movies and buttons for download links."""
trending_movies = suggest_trending_movies()
random_trend_movies = random.sample(trending_movies, min(len(trending_movies), 5))
message = "<b>🔥 فیلم های پیشنهادی این هفته 🔥</b>"
keyboard = []
for movie in random_trend_movies:
movie_id = movie[0]
movie_name = movie[1]
button = InlineKeyboardButton(text=movie_name, callback_data=f"trending_links:{movie_id}")
keyboard.append([button])
reply_markup = InlineKeyboardMarkup(keyboard)
await send_message_to_all_users(message, reply_markup, parse_mode='HTML')
async def send_suggested_movie(movie_id: int, movie_name: str) -> None:
"""Send a message with a specific movie and button for download links."""
message = "<b>🔥 فیلم پیشنهادی ما 🔥</b>"
keyboard = [[InlineKeyboardButton(text=movie_name, callback_data=f"trending_links:{movie_id}")]]
reply_markup = InlineKeyboardMarkup(keyboard)
await send_message_to_all_users(message, reply_markup, parse_mode='HTML')
def send_automatic_trending():
"""Automatic: Clear previous movies, fetch new ones, and send trending movies."""
clear_trending_movie()
trend_movies = get_trending_movies(authorization=TMDB_BEARER_TOKEN)
for element in trend_movies:
movie_name = element['title']
cleaned_movie_name = clean_movie_name_for_api(movie_name)
mark_trending_movie(cleaned_movie_name)
asyncio.run(send_trending_movies())
def send_manual_movie(movie_id: int, movie_name: str):
"""Manual: Send a single specific movie."""
asyncio.run(send_suggested_movie(movie_id, movie_name))
if __name__ == '__main__':
import sys
if len(sys.argv) == 1:
# Default to automatic mode
send_automatic_trending()
elif len(sys.argv) == 3:
# Manual mode with provided movie details
movie_id = int(sys.argv[1])
movie_name = sys.argv[2]
send_manual_movie(movie_id, movie_name)
else:
print("Usage: python trends.py [movie_id movie_name]")