-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
103 lines (85 loc) · 3.87 KB
/
bot.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
import logging
import sys
import time
from telegram import Bot
from google_client import get_sheet_data
from db import add_news, create_db, get_last_news_from_db, update_table
from settings import CHANNEL_ID, SECRET_TOKEN, RETRY_PERIOD, SPREADSHEET_ID
from utils import string_to_date
handler = logging.FileHandler(filename='main.log', encoding='utf-8')
logging.basicConfig(
handlers=(handler,),
format='%(asctime)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
def get_new_news(last_news_from_db, values):
"""Находит новости, которые недавно добавили в google-таблицу.
Возвращает из в виде списка"""
last_news_list = []
for row in values[::-1]:
if string_to_date(row[0]) < string_to_date(last_news_from_db[0]):
break
if row[0] != last_news_from_db[0]:
if len(row) == 6:
last_news_list.append(row)
logger.info(f'Неопубликованная новость: {row}')
else:
break
return last_news_list[::-1]
def update_news_in_bd(old_date_time, last_news_list):
"""Обновляет последнюю новость в бд"""
if last_news_list:
last_news = last_news_list[-1]
update_table(old_date_time, last_news[0], last_news[1],
last_news[2], last_news[3], last_news[4], last_news[5])
logger.info('Обновлена последняя новость в бд')
def list_messages(last_news_list):
"""Получает список неопубликованных новостей
и возвращает сообщения, которые должен отправть бот"""
messages = []
for news in last_news_list:
message = f'<b>{news[1]}</b>\n{news[2]}\n{news[4]}'
logger.info(f'Сообщение к отправке: {message}')
messages.append(message)
return messages
def send_news(bot, message):
"""Оnправляет сообщение в бот"""
bot.send_message(chat_id=CHANNEL_ID, text=message, parse_mode='HTML')
logger.info('Сообщение отправлено')
def main():
"""Основная логика работы бота."""
while True:
try:
values = get_sheet_data(SPREADSHEET_ID)
create_db()
last_news_from_db = get_last_news_from_db()
# если в базе не записана последняя новость, то записываем
if not last_news_from_db:
last_news_list = []
last_news = values[-1]
add_news(last_news[0], last_news[1], last_news[2],
last_news[3], last_news[4], last_news[5])
# если записана, то получаем список новых новостей
else:
last_news_list = get_new_news(last_news_from_db, values)
# если они есть, то бот отправит их в канал и обновит в бд
if last_news_list:
bot = Bot(token=SECRET_TOKEN)
messages = list_messages(last_news_list)
for message in messages:
send_news(bot, message)
time.sleep(RETRY_PERIOD)
update_news_in_bd(last_news_from_db[0], last_news_list)
except Exception as error:
message = f'Сбой в работе программы: {error}'
logger.error(message)
finally:
time.sleep(RETRY_PERIOD)
if __name__ == '__main__':
main()