This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
140 lines (99 loc) · 4 KB
/
main.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
import asyncio
import os
import telebot
import logging
from kinopoisk.movie import Movie
from Main.consts_answers import Constants
bot = telebot.TeleBot("")
@bot.message_handler(commands=["start", "help"])
def greeting(message):
bot.send_message(message.chat.id, Constants.greeting_msg)
def get_film_by_id(film_id):
movie = Movie(id=int(film_id))
movie.get_content("main_page")
return movie
async def load_posters(film_obj):
# СИЛЬНО ЗАТОРМАЖИВАЕТ РАБОТУ!!!!
# Пока не знаю как решить. Буду пробовать с asyncio
film_obj.get_content("posters")
return film_obj.posters[:1]
def get_full_data(id_of_film):
movie_obj = get_film_by_id(id_of_film)
movie_posters = load_posters(movie_obj)
message_text = format_text(movie_obj.title, movie_obj)
def format_text(film_name, movie_obj, format="short"):
message_text = "*Название:* _{0}_ \n".format(film_name)
if movie_obj.rating:
message_text += "*Рейтинг:* " + "⭐" * int(movie_obj.rating) + "\n\n"
if format == "short" and len(movie_obj.plot) > 100:
movie_obj.plot = movie_obj.plot[:100] + " ..."
message_text += "*Описание:* {0}".format(movie_obj.plot)
return message_text
def create_keyboard_films(film_name):
film_names = get_movies_by_name(film_name)
if film_names: # В случае, если вообще существуют такие фильмы.
movie_dict_data = get_movies_desc(*film_names)
ready_elems = []
id_in = 0
for film in movie_dict_data:
if movie_dict_data[film]:
movie_obj = movie_dict_data[film]
# Создание сообщения для краткого ответа
message_text = format_text(film_name=film, movie_obj=movie_obj)
message_content = telebot.types.InputTextMessageContent(message_text=message_text,
parse_mode="Markdown")
# Создание клавиатуры
kb = telebot.types.InlineKeyboardMarkup()
btn = telebot.types.InlineKeyboardButton(text="Открыть обсуждение",
callback_data=str(movie_obj.id))
kb.add(btn)
# Новый элемент
new_elem = telebot.types.InlineQueryResultArticle(
id=str(id_in),
title=film,
input_message_content=message_content,
reply_markup=kb
)
id_in += 1
ready_elems.append(new_elem)
else:
continue
return ready_elems
else:
return []
def get_movies_by_name(film_name):
# Получаем список фильмов по названию.
list_of_movies = Movie.objects.search(film_name)
if list_of_movies:
if len(list_of_movies) > 4:
return list_of_movies[:4]
else:
return list_of_movies
else:
return []
def get_movies_desc(*films):
# print(films)
dict_to_return = {}
for film in films:
movie = Movie(id=film.id)
movie.get_content("main_page")
dict_to_return.update(
{
movie.title: movie
}
)
return dict_to_return
@bot.inline_handler(lambda query: len(query.query) > 0)
def query_text(query):
# print(query)
result = create_keyboard_films(query.query)
bot.answer_inline_query(query.id, result)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
# Если сообщение из чата с ботом
print(call)
movie_obj = get_film_by_id(call.data)
poster = yield from load_posters(movie_obj)
print(poster)
bot.edit_message_text(inline_message_id=call.inline_message_id, text="Hello, guys!")
bot.polling(none_stop=True)