forked from xeruvimov/admins_pain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
158 lines (124 loc) · 4.77 KB
/
main.py
File metadata and controls
158 lines (124 loc) · 4.77 KB
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
import threading
import traceback
from datetime import date
from datetime import datetime
import telebot
import vk_api
from telebot.types import InputMediaPhoto
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import data
import replacer
start_msg = "Start listen just now"
message_breakers = [':', ' ', '\n']
max_message_length = 4091
bot = telebot.TeleBot(data.BOT_TOKEN)
vk_session = None
vk_long_poll = None
repost_image_urls = None
original_post_img_urls = None
def get_session():
global vk_session
if vk_session is None:
vk_session = vk_api.VkApi(token=data.GROUP_TOKEN)
return vk_session
else:
return vk_session
def get_longpoll():
global vk_long_poll
if vk_long_poll is None:
vk_long_poll = VkBotLongPoll(get_session(), data.GROUP_ID, 60)
return vk_long_poll
else:
return vk_long_poll
def check_posts_vk(chat_id):
global bot
global original_post_img_urls
longpoll = get_longpoll()
print(start_msg)
bot.send_message(data.MY_CHAT_ID, start_msg)
for event in longpoll.listen():
if event.type == VkBotEventType.WALL_POST_NEW:
post = event.obj
print('------------------------------------------------------------------------------------------------')
print(post)
text = post['text']
images = []
links = []
doc = []
attachments = []
if 'attachments' in post:
attach = post['attachments']
for add in attach:
if add['type'] == 'photo':
img = add['photo']
images.append(img)
elif add['type'] == 'video':
video = add['video']
if 'player' in video:
links.append(video['player'])
elif add['type'] == 'doc':
docs = add['doc']
if 'url' in docs:
doc.append(docs['title'])
doc.append(docs['url'])
else:
for (key, value) in add.items():
if key != 'type' and 'url' in value:
attachments.append(value['url'])
print(doc, '\n')
if len(doc) != 0:
text += '\n'
text += '\n'.join(doc)
send_posts_text(replacer.prepare_text(text, replacer.TELEGRAM, None), chat_id)
if len(images) > 0:
original_post_img_urls = list(
map(lambda img: max(img["sizes"], key=lambda size: size["type"])["url"], images))
print(original_post_img_urls)
bot.send_media_group(chat_id, map(lambda url: InputMediaPhoto(url), original_post_img_urls))
bot.send_message(data.MY_CHAT_ID, "News posted on telegram")
create_site_post(text)
bot.send_message(data.MY_CHAT_ID, "News posted on site")
def create_site_post(text):
site_text = replacer.prepare_text(text, replacer.SITE, original_post_img_urls)
print(site_text)
file_path = data.PATH_TO_SITE_PAGES + str(date.today().strftime("%Y-%m-%d")) + str(datetime.now().hour) + str(
datetime.now().minute) + str(datetime.now().second) + ".md"
print(file_path)
page_file = open(file_path, 'w+', encoding='utf-8')
page_file.write(site_text)
page_file.close()
def send_posts_text(text, chat_id):
global bot
if text == '':
print('no text')
else:
for msg in split(text):
bot.send_message(chat_id, msg)
def split(text):
global message_breakers
global max_message_length
if len(text) >= max_message_length:
last_index = max(
map(lambda separator: text.rfind(separator, 0, max_message_length), message_breakers))
good_part = text[:last_index]
bad_part = text[last_index + 1:]
return [good_part] + split(bad_part)
else:
return [text]
@bot.message_handler(commands=['test'])
def test(message):
bot.send_message(message.chat.id, "I`m still work")
if __name__ == '__main__':
bot_polling_thread = threading.Thread(target=bot.polling, args=())
bot_polling_thread.start()
try:
check_posts_vk(data.CHANEL_ID)
except Exception:
bot.send_message(data.MY_CHAT_ID, traceback.format_exc())
bot.send_message(data.MY_CHAT_ID, "Stop check new posts")
bot.send_message(data.MY_CHAT_ID, "Attempt to restore work")
try:
check_posts_vk(data.CHANEL_ID)
except Exception:
bot.send_message(data.MY_CHAT_ID, traceback.format_exc())
bot.send_message(data.MY_CHAT_ID, "Stop check new posts\nRestart me")