-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (48 loc) · 1.81 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
import logging
import os
import sys
import time
from datetime import datetime
import pytz
import telebot
from dotenv import load_dotenv
from parse import get_media_names, get_post_content, remove_files
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
streamHandler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
load_dotenv()
bot = telebot.TeleBot(os.getenv('TOKEN_TELEGRAM'))
time_skip: int = 1800
operations: dict = {'sell': 1, 'rent': 3}
channels: dict = {'sell': '@kypitkvsrtirykiev', 'rent': '@kkkarenda'}
while True:
for i in operations:
tz = pytz.timezone('Europe/Kiev')
current_datetime = datetime.now(tz)
if 9 > current_datetime.hour > 22:
time.sleep(time_skip*2)
logger.info('Too late/early for posts')
else:
message_text = str(get_post_content(type=operations[i]))
media_list = []
file_names = get_media_names()
for a in range(0, len(file_names)):
file = file_names[a]
media_list.append(
telebot.types.InputMediaPhoto(open(file, 'rb')))
try:
recipient = channels[i]
bot.send_media_group(recipient, media_list)
bot.send_message(recipient, message_text)
logger.info(f'Message to {recipient} has been sent succesfully')
except (TypeError, NameError, AttributeError, Exception) as error:
message = f'Message sending error: {error}'
logger.error(message)
remove_files()
time.sleep(time_skip/120)
time.sleep(time_skip)
if __name__ == '__main__':
bot.infinity_polling()