-
Notifications
You must be signed in to change notification settings - Fork 1
/
augcrossnews.py
122 lines (105 loc) · 5.1 KB
/
augcrossnews.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
import requests as req
import json
import time
from objects import *
from utils import *
import vk
def make_object(obj, url=None):
obj['chat_id'] = CONFIG['target_channel']
obj['parse_mode'] = 'HTML'
obj['disable_web_page_preview'] = True
if url != None:
obj['reply_markup'] = json.dumps({
'inline_keyboard':[[{
'text': 'Открыть пост',
'url': url
}]]
})
return obj
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def main():
while True:
try:
log_print('Получаю новости...')
posts = list()
posts += vk.get_news()
if len(posts) > 0:
log_print('Новости получены. Начинаю постинг...')
for post in posts:
# Only text
if len(post.attachments) == 0:
for is_last_element, chunk in signal_last(chunks(post.text, 4096)):
body = {
'text': chunk
}
tg_api('sendMessage', make_object(body, post.url) if is_last_element else make_object(body))
# Text length less 1024 and one attachment
elif (len(post.text) <= 1024) and (len(post.attachments) == 1):
if isinstance(post.attachments[0], PhotoAttachment):
body = {
'photo': post.attachments[0].url,
'caption': post.text
}
tg_api('sendPhoto', make_object(body, post.url))
if isinstance(post.attachments[0], VideoAttachment):
body = {
'video': post.attachments[0].url,
'supports_streaming': True,
'caption': post.text
}
tg_api('sendVideo', make_object(body, post.url))
if isinstance(post.attachments[0], DocAttachment):
body = {
'document': post.attachments[0].url,
'caption': post.text
}
tg_api('sendDocument', make_object(body, post.url))
# Text length more then 1024 or attachments more then one
elif (len(post.text) > 1024) or (len(post.attachments) > 1):
for group in chunks(post.attachments, 10):
attachments = list()
for attach in group:
if isinstance(attach, PhotoAttachment):
InputMediaPhoto = {
'type': 'photo',
'media': attach.url,
'caption': attach.caption,
'parse_mode': 'HTML'
}
attachments.append(InputMediaPhoto)
if isinstance(attach, VideoAttachment):
InputMediaPhoto = {
'type': 'video',
'media': attach.url,
'caption': attach.caption,
'supports_streaming': True,
'parse_mode': 'HTML'
}
attachments.append(InputMediaPhoto)
if isinstance(attach, DocAttachment):
body = {
'document': attach.url,
'caption': attach.caption
}
tg_api('sendDocument', make_object(body))
body = {
'media': json.dumps(attachments)
}
tg_api('sendMediaGroup', make_object(body, post.url))
for is_last_element, chunk in signal_last(chunks(post.text, 4096)):
body = {
'text': chunk
}
tg_api('sendMessage', make_object(body, post.url) if is_last_element else make_object(body))
time.sleep(1)
log_print('Постинг закончен.')
else:
log_print('Новых постов нет.')
except e:
print(e)
time.sleep(CONFIG['cooldown'])
if __name__ == "__main__":
main()