-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
364 lines (324 loc) · 18.9 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
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
import telebot
import os
from pytz import utc
from config import TOKEN, DATABASELINK
from flask import Flask, request
from parcer import get_vacancy_list, get_artickle_list
from dbdriver import cursor, connection
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.executors.pool import ProcessPoolExecutor
from navigation import navigate_menu, menu, main_menu, cities, cities_menu, pro_tech, pro_tech_menu, experience, \
experience_menu, start_menu, vacancy_add_menu, vacancy_del_menu, to_main_menu, head_menu, subscription_del_menu
from utils import create_url
jobstores = {
'default': SQLAlchemyJobStore(url=DATABASELINK,
tablename='apscheduler_jobs')
}
executors = {
'default': {'type': 'threadpool', 'max_workers': 20},
'processpool': ProcessPoolExecutor(max_workers=5)
}
job_defaults = {'coalesce': False,
'max_instances': 3
}
bot = telebot.TeleBot(TOKEN)
server = Flask(__name__)
scheduler = BackgroundScheduler(daemon=True)
scheduler.configure(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc)
scheduler.start()
def get_new_vacancy(user_id, chat_id, pro_lang, exp, city, url):
remote_vacancy_list = get_vacancy_list(url)
remote_vacancy_list = [link[2] for link in remote_vacancy_list]
cursor.execute("""SELECT vacancy_urls
FROM subscriptions
WHERE user_id = %s AND chat_id = %s AND pro_lang = %s AND exp = %s AND city = %s""",
(user_id, chat_id, pro_lang, exp, city))
vacancy_urls, = cursor.fetchone() or ([],)
list_vacancy_urls = vacancy_urls.split(',') if vacancy_urls else []
print(scheduler.get_jobs())
print(list_vacancy_urls)
if remote_vacancy_list:
for vacancy in remote_vacancy_list:
if vacancy not in list_vacancy_urls:
bot.send_message(chat_id, vacancy, parse_mode='html', reply_markup=vacancy_add_menu)
users_vacancy_links = ','.join(list_vacancy_urls) + ',' + vacancy if list_vacancy_urls else vacancy
cursor.execute("""UPDATE subscriptions
SET vacancy_urls = %s
WHERE user_id = %s AND chat_id = %s AND pro_lang = %s AND exp = %s AND city = %s""",
(users_vacancy_links, user_id, chat_id, pro_lang, exp, city))
connection.commit()
@bot.message_handler(commands=['start'])
def welcome(message):
welcome_message = f"<i>Вітаю</i> <b>{message.from_user.first_name}</b>!\n" \
f"<i>Я</i> - <b>Dou</b>!, <i>бот який допоможе тобі швидко та зручно отримати необхідну " \
f"інформацію з Dou.ua. \nНа даний час я знаю про багато актуальних вакансій, " \
f"які реалізовані в розділі «Вакансії».</i>\n\n" \
f"<i>Отож не зволікай! Мерщій клікай та обирай свою найкращу вакансію!</i>"
cursor.execute("""INSERT INTO users (chat_id, user_id, state, pro_lang, exp, city, first_name, last_name)
VALUES (%s, %s, Null, Null, Null, Null, %s, %s)
ON CONFLICT (user_id) DO UPDATE SET
chat_id = %s,
user_id = %s,
state = Null,
pro_lang = Null,
exp = Null,
city = NUll,
first_name = %s,
last_name = %s""", (message.chat.id, message.from_user.id, message.chat.first_name, message.chat.last_name,
message.chat.id, message.from_user.id, message.chat.first_name, message.chat.last_name))
connection.commit()
return bot.send_message(message.chat.id, welcome_message, parse_mode='html', reply_markup=main_menu)
@bot.message_handler(content_types=['text'])
def message_handler(message):
if message.text == 'Оновити':
return welcome(message)
cursor.execute("""SELECT *
FROM users
WHERE user_id = %s
AND chat_id = %s""", (message.from_user.id, message.chat.id))
result = cursor.fetchone()
if not result:
return bot.send_message(message.chat.id, "Інформація застаріла. Оновіть щоб розпочати", parse_mode='html',
reply_markup=start_menu)
else:
result = {'chat_id': result[0],
'user_id': result[1],
'state': result[2],
'pro_lang': result[3],
'exp': result[4],
'city': result[5]}
return message_handler_with_data(message, result)
def message_handler_with_data(message, result):
if message.text == 'Вакансії':
bot.send_message(message.chat.id, 'Виберіть напрямок', parse_mode='html', reply_markup=pro_tech_menu)
cursor.execute("""UPDATE users
SET state = %s
WHERE user_id = %s
AND chat_id = %s""", (message.text, message.from_user.id, message.chat.id))
connection.commit()
elif message.text == 'Обрані вакансії':
cursor.execute("""UPDATE users
SET state = %s
WHERE user_id = %s
AND chat_id = %s""",(message.text, message.from_user.id, message.chat.id))
connection.commit()
cursor.execute("""SELECT vacancy_url, title
FROM favorites
WHERE user_id = %s""", (message.from_user.id,))
result = cursor.fetchall()
if not result:
bot.send_message(message.chat.id, "У вас немає обраних вакансій", parse_mode='html', reply_markup=to_main_menu)
else:
for vacancy in result:
text_msg = f"<a href='{vacancy[0]}'>{vacancy[1]}</a>"
bot.send_message(message.chat.id, text_msg, parse_mode='html', reply_markup=vacancy_del_menu)
bot.send_message(message.chat.id, '', parse_mode='html', reply_markup=to_main_menu)
elif message.text == 'Стрічка':
cursor.execute("""UPDATE users
SET state = %s
WHERE user_id = %s
AND chat_id = %s""", (message.text, message.from_user.id, message.chat.id))
connection.commit()
article_list = get_artickle_list('https://dou.ua/lenta/')
if article_list:
for article in article_list:
text_msg = f"<a href='{article[0]}'>{article[1].strip()}</a>"
bot.send_message(message.chat.id, text_msg, parse_mode='html')
bot.send_message(message.chat.id, '', parse_mode='html', reply_markup=to_main_menu)
elif message.text == 'Мої підписки':
cursor.execute("""UPDATE users
SET state = %s
WHERE user_id = %s
AND chat_id = %s""", (message.text, message.from_user.id, message.chat.id))
connection.commit()
cursor.execute("""SELECT CONCAT(pro_lang, ' ', city, ' ', exp)
FROM subscriptions
WHERE user_id = %s""", (message.from_user.id,))
result = cursor.fetchall()
if not result:
bot.send_message(message.chat.id, "У вас немає активних підписок", parse_mode='html',
reply_markup=to_main_menu)
else:
for sub in result:
bot.send_message(message.chat.id, sub[0], parse_mode='markdown', reply_markup=subscription_del_menu)
bot.send_message(message.chat.id, '', parse_mode='html', reply_markup=to_main_menu)
elif message.text == 'Оформити підписку':
cursor.execute("""SELECT chat_id, user_id, pro_lang, exp, city, vacancy_urls
FROM users
WHERE user_id = %s""", (message.from_user.id,))
result = cursor.fetchone()
if result:
chat_id = result[0]
user_id = result[1]
pro_lang = result[2]
exp = result[3]
city = result[4]
vacancy_urls = result[5]
cursor.execute("""SELECT subscribe
FROM subscriptions
WHERE user_id = %s AND chat_id = %s AND pro_lang = %s AND exp = %s AND city = %s""",
(user_id, chat_id, pro_lang, exp, city))
subscribe, = cursor.fetchone() or (False,)
if not subscribe:
url = create_url(pro_lang, exp, city)
unique_cron_id = str(chat_id)+str(user_id)+pro_lang+exp+city
cursor.execute("""INSERT INTO subscriptions(user_id, chat_id, pro_lang, exp, city, vacancy_urls, url, cron_id, subscribe)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, True)
""", (user_id, chat_id, pro_lang, exp, city, vacancy_urls, url, unique_cron_id))
connection.commit()
scheduler.add_job(get_new_vacancy, 'interval', args=[chat_id, user_id, pro_lang, exp, city, url], minutes=15,
replace_existing=True, id=unique_cron_id)
bot.send_message(message.chat.id, f'Підписку оформлено', parse_mode='html',
reply_markup=head_menu)
else:
bot.send_message(message.chat.id, 'У вас вжє є активна підписка', parse_mode='html', reply_markup=head_menu)
bot.send_message(message.chat.id, '', parse_mode='html', reply_markup=head_menu)
elif message.text in ('« На головну',) and \
result.get('chat_id') == message.chat.id and \
result.get('user_id') == message.from_user.id and \
(result.get('state') in menu or result.get('state') in pro_tech or result.get('state')
in experience or result.get('state') in cities):
bot.send_message(message.chat.id, 'Виберіть розділ', parse_mode='html', reply_markup=main_menu)
cursor.execute("""UPDATE users
SET state = Null,
pro_lang = Null,
exp = Null,
city = Null
WHERE user_id = %s
AND chat_id = %s""", (message.from_user.id, message.chat.id))
connection.commit()
elif message.text in ('‹ Назад', ) and \
result.get('chat_id') == message.chat.id and \
result.get('user_id') == message.from_user.id and \
(result.get('state') in menu or result.get('state') in pro_tech or result.get('state')
in experience or result.get('state') in cities):
current_state = result.get('state')
current_pro_lang = result.get('pro_lang')
current_exp = result.get('exp')
current_cty = result.get('city')
if current_state == current_pro_lang:
bot.send_message(message.chat.id, "Виберіть напрямок", parse_mode='html', reply_markup=pro_tech_menu)
cursor.execute("""UPDATE users
SET state = %s,
pro_lang = Null
WHERE user_id = %s
AND chat_id = %s""", (menu[0], message.from_user.id, message.chat.id))
connection.commit()
elif current_state == current_exp:
bot.send_message(message.chat.id, 'Вкажіть досвід', parse_mode='html', reply_markup=experience_menu)
cursor.execute("""UPDATE users
SET state = %s,
exp = Null
WHERE user_id = %s
AND chat_id = %s""", (current_pro_lang, message.from_user.id, message.chat.id))
connection.commit()
elif current_state == current_cty:
bot.send_message(message.chat.id, 'Вкажіть місто або оберіть зі списку', parse_mode='html',
reply_markup=cities_menu)
cursor.execute("""UPDATE users
SET state = %s,
city = Null
WHERE user_id = %s
AND chat_id = %s""",(current_exp, message.from_user.id, message.chat.id))
connection.commit()
elif message.text not in pro_tech and \
result.get('chat_id') == message.chat.id and \
result.get('user_id') == message.from_user.id and \
result.get('state') in menu:
bot.send_message(message.chat.id, 'Виберіть напрямок', parse_mode='html', reply_markup=pro_tech_menu)
elif message.text in pro_tech:
bot.send_message(message.chat.id, 'Вкажіть досвід', parse_mode='html', reply_markup=experience_menu)
cursor.execute("""UPDATE users
SET state = %s,
pro_lang = %s
WHERE user_id = %s
AND chat_id = %s""", (message.text, message.text, message.from_user.id, message.chat.id))
connection.commit()
elif message.text not in experience and \
result.get('chat_id') == message.chat.id and \
result.get('user_id') == message.from_user.id and \
result.get('state') in pro_tech:
bot.send_message(message.chat.id, 'Вкажіть досвід', parse_mode='html', reply_markup=experience_menu)
elif message.text in experience:
bot.send_message(message.chat.id, 'Вкажіть місто або оберіть зі списку', parse_mode='html',
reply_markup=cities_menu)
cursor.execute("""UPDATE users
SET state = %s,
exp = %s
WHERE user_id = %s
AND chat_id = %s""", (message.text, message.text, message.from_user.id, message.chat.id))
connection.commit()
elif message.text not in cities and \
result.get('chat_id') == message.chat.id and \
result.get('user_id') == message.from_user.id and \
result.get('state') in experience:
bot.send_message(message.chat.id, "Можливо ви допустили помилку у назві міста, або даного міста немає в базі "
"даних. Будьласка спробуйте ще раз, або виберіть зі списку", parse_mode='html',
reply_markup=cities_menu)
elif message.text in cities:
city = message.text
pro_lang = result['pro_lang']
exp = result['exp']
url = create_url(pro_lang, exp, city)
vacancy_links = get_vacancy_list(url)
if not vacancy_links:
bot.send_message(message.chat.id, "Нажаль на даний час немає актуальних вакансій по заданим критеріям",
parse_mode='html', reply_markup=navigate_menu)
else:
for link in vacancy_links:
text_msg = f"<a href='{link[2]}'>{link[0]} в {link[1]}</a>"
bot.send_message(message.chat.id, text_msg, parse_mode='html',
reply_markup=vacancy_add_menu, disable_notification=True)
bot.send_message(message.chat.id, "", parse_mode='html',
reply_markup=navigate_menu)
cursor.execute("""UPDATE users
SET state = %s,
city = %s,
vacancy_urls = %s
WHERE user_id = %s
AND chat_id = %s""", (message.text, message.text, ','.join([link[2] for link in vacancy_links]), message.from_user.id, message.chat.id))
connection.commit()
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.data == 'add_vacancy':
cursor.execute("""INSERT INTO favorites (user_id, vacancy_url, title)
VALUES (%s, %s, %s)
ON CONFLICT (vacancy_url) DO NOTHING """,(call.from_user.id,
call.message.json['entities'][0]['url'],
call.message.text))
connection.commit()
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, parse_mode='html',
text=f"<a href='{call.message.json['entities'][0]['url']}'>{call.message.text}</a>")
bot.answer_callback_query(callback_query_id=call.id, show_alert=True, text="Вакансію додано до обраних")
elif call.data == 'del_vacancy':
cursor.execute("""DELETE FROM favorites
WHERE user_id = %s
AND vacancy_url = %s""",(call.from_user.id, call.message.json['entities'][0]['url']))
connection.commit()
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text="Вакансію вилучено з обраних")
elif call.data == 'delete_subscription':
cursor.execute("""SELECT cron_id
FROM subscriptions
WHERE CONCAT(pro_lang, ' ', city, ' ', exp) = %s
AND user_id = %s""", (call.message.text, call.from_user.id))
cron_id, = cursor.fetchone() or (False,)
cursor.execute("""DELETE FROM subscriptions
WHERE cron_id = %s;
DELETE FROM apscheduler_jobs
WHERE id = %s;""", (cron_id,cron_id,))
connection.commit()
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text="Підписку скасовано")
@server.route("/" + TOKEN, methods=['POST'])
def get_message():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "POST", 200
@server.route("/")
def web_hook():
bot.remove_webhook()
bot.set_webhook(url="https://doutelegrambot.herokuapp.com/" + TOKEN)
return "CONNECTED", 200
if __name__ == "__main__":
server.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))