-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
70 lines (52 loc) · 1.68 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import os
import time
from threading import Thread
# pip install python-telegram-bot
from telegram.ext import Updater, Defaults
import common
from bot import commands, db
from config import TOKEN, DIR_COMICS
from common import log, log_backup
from bot.db_utils import do_backup
from bot.parsers import (
download_random_quotes,
download_main_page_quotes,
download_seq_page_quotes,
run_parser_health_check,
)
def main():
log.debug("Start")
cpu_count = os.cpu_count()
workers = cpu_count
log.debug(f"System: CPU_COUNT={cpu_count}, WORKERS={workers}")
updater = Updater(
TOKEN,
workers=workers,
defaults=Defaults(run_async=True),
)
bot = updater.bot
log.debug(f"Bot name {bot.first_name!r} ({bot.name})")
common.BOT = bot
commands.setup(updater)
updater.start_polling()
updater.idle()
log.debug("Finish")
if __name__ == "__main__":
# TODO: Вернуть, если https://bash.im станет доступен
# Thread(target=download_main_page_quotes, args=[log, DIR_COMICS]).start()
# Thread(target=download_seq_page_quotes, args=[log, DIR_COMICS]).start()
# Thread(target=download_random_quotes, args=[log, DIR_COMICS]).start()
# Thread(target=run_parser_health_check, args=[log]).start()
Thread(target=do_backup, args=[log_backup]).start()
while True:
try:
main()
except Exception as e:
log.exception("")
db.Error.create_from(main, e)
timeout = 15
log.info(f"Restarting the bot after {timeout} seconds")
time.sleep(timeout)