-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
191 lines (158 loc) · 6.85 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
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
import json
import random
import telebot
from telethon.errors.rpcbaseerrors import FloodError, InvalidDCError
from UI import ui_messages, keyboards
from data_managing.Shelver import Shelver
from channel_parser.ChannelDataCollector import DataCollector
JSON_PATH = 'data_managing/secret_data.json'
POST_LIMIT = 10_000
with open(JSON_PATH, 'r') as json_file:
API_TOKEN = json.load(json_file)['token']
bot = telebot.TeleBot(API_TOKEN, threaded=False)
collector = DataCollector.from_config(JSON_PATH, limit=POST_LIMIT)
database = Shelver('data_managing/user_info.db')
print('Initializing finished')
# Interaction with a user
@bot.message_handler(commands=['start'])
def handle_start(message):
lang = database.get_language(message.chat.id)
if lang == 'en':
bot.send_message(message.chat.id, ui_messages.WELCOME_EN)
elif lang == 'ru':
bot.send_message(message.chat.id, ui_messages.WELCOME_RU)
else:
keyboards.send_language_markup(bot, message.chat.id)
@bot.callback_query_handler(func=lambda call: call.data == 'en')
def set_english(call):
lang = database.get_language(call.from_user.id)
if lang is None:
bot.send_message(call.from_user.id, ui_messages.WELCOME_EN)
else:
bot.send_message(call.from_user.id, ui_messages.LANGUAGE_SET_EN)
database.set_language(call.from_user.id, 'en')
@bot.callback_query_handler(func=lambda call: call.data == 'ru')
def set_russian(call):
lang = database.get_language(call.from_user.id)
if lang is None:
bot.send_message(call.from_user.id, ui_messages.WELCOME_RU)
else:
bot.send_message(call.from_user.id, ui_messages.LANGUAGE_SET_RU)
database.set_language(call.from_user.id, 'ru')
@bot.message_handler(commands=['help'])
def send_help(message):
lang = database.get_language(message.chat.id)
if lang == 'en':
bot.send_message(message.chat.id, ui_messages.HELP_EN, parse_mode='markdown')
elif lang == 'ru':
bot.send_message(message.chat.id, ui_messages.HELP_RU, parse_mode='markdown')
else:
keyboards.send_language_markup(bot, message.chat.id)
@bot.message_handler(commands=['setlanguage'])
def handle_setlanguage(message):
keyboards.send_language_markup(bot, message.chat.id)
@bot.message_handler(regexp='^https://t.me/')
def handle_wrong_query(message):
language = database.get_language(message.chat.id)
if language is None:
keyboards.send_language_markup(bot, message.chat.id)
elif language == 'en':
bot.send_message(message.chat.id, ui_messages.FORGOT_ANALYZE_EN, parse_mode='markdown')
else:
bot.send_message(message.chat.id, ui_messages.FORGOT_ANALYZE_RU, parse_mode='markdown')
# Queries handling
@bot.message_handler(regexp='^/analyze')
def handle_analyze_query(message):
lang = database.get_language(message.chat.id)
if lang is None:
keyboards.send_language_markup(bot, message.chat.id)
elif message.text.strip() == '/analyze': # if user just typed /analyze without the link
if lang == 'en':
bot.send_message(message.chat.id, ui_messages.FORGOT_LINK_EN, parse_mode='markdown')
elif lang == 'ru':
bot.send_message(message.chat.id, ui_messages.FORGOT_LINK_RU, parse_mode='markdown')
else:
channel_username = message.text.strip().split(' ')[-1]
try:
path = database.get_path(message.chat.id)
collector.load_data(channel_username, path=path)
database.set_analyzer(message.chat.id)
if lang == 'en':
bot.reply_to(message, ui_messages.CHANNEL_ANALYZED_EN)
elif lang == 'ru':
bot.reply_to(message, ui_messages.CHANNEL_ANALYZED_RU)
except ValueError:
if lang == 'en':
bot.reply_to(message, ui_messages.CHANNEL_NOT_FOUND_EN)
elif lang == 'ru':
bot.reply_to(message, ui_messages.CHANNEL_NOT_FOUND_RU)
except (ConnectionError, FloodError, InvalidDCError):
if lang == 'en':
bot.reply_to(message, ui_messages.SOMETHING_WENT_WRONG_EN)
elif lang == 'ru':
bot.reply_to(message, ui_messages.SOMETHING_WENT_WRONG_RU)
@bot.message_handler(commands=['stats'])
def handle_stats(message):
print('handle_stats entered')
language = database.get_language(message.chat.id)
if language == 'ru':
keyboards.send_stats_markup_ru(bot, message.chat.id)
elif language == 'en':
keyboards.send_stats_markup_en(bot, message.chat.id)
else:
keyboards.send_language_markup(bot, message.chat.id)
def is_ready_to_get_stats(chat_id) -> bool:
language = database.get_language(chat_id)
if language is None:
keyboards.send_language_markup(bot, chat_id)
return False
if not database.is_analyzer_set(chat_id):
if language == 'en':
bot.send_message(chat_id, ui_messages.SEND_QUERY_EN, parse_mode='markdown')
elif language == 'ru':
bot.send_message(chat_id, ui_messages.SEND_QUERY_RU, parse_mode='markdown')
return False
return True
@bot.callback_query_handler(func=lambda call: call.data.split('_')[0] == 'post')
def send_post_stats(call):
print('send_post_stats entered')
if is_ready_to_get_stats(call.from_user.id):
analyzer = database.get_analyzer(call.from_user.id)
language = database.get_language(call.from_user.id)
analyzer.send_post_stats(bot, language)
@bot.callback_query_handler(func=lambda call: call.data.split('_')[0] == 'views')
def send_views_stats(call):
print('send_views_stats entered')
if is_ready_to_get_stats(call.from_user.id):
analyzer = database.get_analyzer(call.from_user.id)
language = database.get_language(call.from_user.id)
analyzer.send_views_stats(bot, language)
@bot.callback_query_handler(func=lambda call: call.data.split('_')[0] == 'text')
def send_text_stats(call):
print('send_text_stats entered')
if is_ready_to_get_stats(call.from_user.id):
language = database.get_language(call.from_user.id)
if language == 'en':
bot.send_message(call.from_user.id, ui_messages.WAIT_EN)
else:
bot.send_message(call.from_user.id, ui_messages.WAIT_RU)
analyzer = database.get_analyzer(call.from_user.id)
analyzer.send_text_stats(bot, language)
# Unknown commands handling
@bot.message_handler(func=lambda message: True)
def handle_unknown_commands(message):
lang = database.get_language(message.chat.id)
if lang == 'en':
bot.send_message(
message.chat.id,
*random.sample(ui_messages.UNKNOWN_COMMAND_EN, 1)
)
elif lang == 'ru':
bot.send_message(
message.chat.id,
*random.sample(ui_messages.UNKNOWN_COMMAND_RU, 1)
)
else:
keyboards.send_language_markup(bot, message.chat.id)
if __name__ == '__main__':
bot.polling()