-
Notifications
You must be signed in to change notification settings - Fork 2
/
movies.py
142 lines (126 loc) · 5.3 KB
/
movies.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
import imdb
import telegram
import logging
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from utils import build_menu
import re
logger = logging.getLogger(__name__)
ia = imdb.IMDb()
def search_movie(movie):
movies = ia.search_movie(movie)
return movies
def search_person(person):
movies = ia.search_person(person)
return movies
def get_movie(id):
movie_detail = ia.get_movie(id)
return movie_detail
def get_person(id):
person_detail = ia.get_person(id)
return person_detail
def movie_search_menu(update, context):
search = update.message.text.split('/movie ')[1]
movie = search_movie(search)
buttons = list()
for m in movie:
buttons.append(InlineKeyboardButton(f'{m.data.get("title")} - {m.data.get("year")}',
callback_data=f'movie,{m.movieID}'))
reply_markup = InlineKeyboardMarkup(build_menu(buttons, n_cols=2))
update.message.reply_text('Which one do you mean?', reply_markup=reply_markup)
def person_search_menu(update, context):
search = update.message.text.split('/person ')[1]
movie = search_person(search)
buttons = list()
for m in movie:
buttons.append(InlineKeyboardButton(f'{m.data.get("title")} - {m.data.get("year")}',
callback_data=f'person,{m.movieID}'))
reply_markup = InlineKeyboardMarkup(build_menu(buttons, n_cols=2))
update.message.reply_text('Which one do you mean?', reply_markup=reply_markup)
def movie_info(update, context):
query = update.callback_query
mid = query.message.message_id
cid = query.message.chat_id
context.bot.delete_message(chat_id=cid, message_id=mid)
movieid = query.data.split(',')[1]
try:
md = get_movie(movieid)
if md.data.get('directors'):
directors = [i.data.get('name') for i in md.data.get('directors')]
else:
directors = [i.data.get('name') for i in md.data.get('director', [])]
cast = [i.data.get('name') for i in md.data.get('cast', [])[:4]]
countries = [i for i in md.data.get('countries', [])]
air_date = md.data.get('original air date')
rating = md.data.get('rating')
votes = md.data.get('votes')
cover = md.data.get('cover url')
cover = re.sub(r'._V1_.*', '._V1_.jpg', cover)
plot = md.data.get('plot outline', md.data.get('plot')[0])
title = md.data.get('title')
year = md.data.get('year')
genres = md.data.get('genres', [])
runtimes = md.data.get('runtimes', [])
languages = md.data.get('languages', [])
box_office = md.data.get('box office')
message = f'*{title} - {year} - {", ".join(countries)}*\n'
message += f'*Rating:* {rating}, *Votes:* {votes}\n'
message += f"*Directors:* {', '.join(directors)}\n"
message += f"*Cast:* {', '.join(cast)}\n"
message += f"*Air Date:* {air_date}\n"
if box_office:
budget = box_office.get('Budget', '?')
gross = box_office.get('Cumulative Worldwide Gross', '?')
message += f"*Budget:* {budget}\n*Gross:* {gross},\n"
message += "*Plot:*\n\n"
message += f"{plot}\n\n"
# message += f'{cover}\n\n'
logger.info(f'Sending {title}')
except Exception as e:
logger.exception(e)
message = str(e)
context.bot.send_message(chat_id=cid,
text=message, parse_mode=telegram.ParseMode.MARKDOWN,
timeout=150)
context.bot.send_photo(chat_id=cid, photo=cover)
def person_info(update, context):
query = update.callback_query
mid = query.message.message_id
cid = query.message.chat_id
context.bot.delete_message(chat_id=cid, message_id=mid)
person_id = query.data.split(',')[1]
try:
pd = get_person(person_id)
print(pd)
if pd.data.get('directors'):
directors = [i.data.get('name') for i in pd.data.get('directors')]
else:
directors = [i.data.get('name') for i in pd.data.get('director')]
cast = [i.data.get('name') for i in pd.data.get('cast')[:4]]
countries = [i for i in pd.data.get('countries')]
air_date = pd.data.get('original air date')
rating = pd.data.get('rating')
votes = pd.data.get('votes')
cover = pd.data.get('cover url')
plot = pd.data.get('plot')[0]
title = pd.data.get('title')
year = pd.data.get('year')
box_office = pd.data.get('box office')
message = f'*{title} - {year} - {", ".join(countries)} *\n'
# message += f'*Rating:* {rating}, *Votes:* {votes}\n'
message += f"*Directors:* {', '.join(directors)}\n"
message += f"*Cast:* {', '.join(cast)}\n"
message += f"*Air Date:* {air_date}\n"
if box_office:
budget = box_office.get('Budget')
gross = box_office.get('Cumulative Worldwide Gross')
message += f"*Budget:* {budget}\n*Gross:* {gross},\n"
message += "*Plot:*\n\n"
message += f"{plot}\n\n"
message += f'{cover}\n\n'
logger.info(f'Sending {title}')
except Exception as e:
logger.exception(e)
message = str(e)
context.bot.send_message(chat_id=cid,
text=message, parse_mode=telegram.ParseMode.MARKDOWN,
timeout=150)