-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
119 lines (96 loc) · 5.35 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
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
import asyncio
import json
import os
import random
import telegram
from dotenv import load_dotenv
from core.artwork_analysis import Artwork, ArtworkRetriever, ArtworkAnalyser
load_dotenv()
TELEGRAM_API_KEY = os.getenv('TG_TOKEN')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
channel_id = '@science_art_at_least_once_a_week'
MAX_POST_LENGTH = 4096
MAX_CAPTION_LENGTH = 1024
def get_message_text(artwork: dict, message_length: int = 0, to_cut=False):
if to_cut:
return f'*{artwork["name"]}*\n\n{artwork["authors"]}\n\n_{artwork["award"]}, {artwork["year"]}_\n' \
f'_{artwork["category"]}_\n\n{cut(artwork["description_ru"], message_length)}\n\n{artwork["url"]}\n\n' \
f'{to_hashtag(artwork["award"])} {to_hashtag(artwork["category"])} {to_hashtag(artwork["year"]) + "year"}'
return f'*{artwork["name"]}*\n\n{artwork["authors"]}\n\n_{artwork["award"]}, {artwork["year"]}_\n' \
f'_{artwork["category"]}_\n\n{artwork["description_ru"]}\n\n{artwork["url"]}\n\n' \
f'{to_hashtag(artwork["award"])} {to_hashtag(artwork["category"])} {to_hashtag(artwork["year"]) + "year"}'
def get_caption_text(artwork: dict):
return f'*{artwork["name"]}*\n\n_{artwork["authors"]} ({artwork["year"]})_\n\n{artwork["url"]}\n\n' \
f'{to_hashtag(artwork["award"])} {to_hashtag(artwork["category"])} {to_hashtag(artwork["year"]) + "year"}'
def cut(text: str, length: int):
cut_length = MAX_POST_LENGTH - length
return text[:cut_length - 3] + '...'
def to_hashtag(text: str) -> str:
return '#' + text.lower().replace('-', '').replace('–', '')\
.replace('(', '').replace(')', '').replace('/', ' ').replace('&', '')\
.replace(' ', ' ').replace(' ', ' ').replace(' ', '\_')
def remove_markdown(text: str) -> str:
# removes all markdown symbols from text
return text.replace('*', '').replace('_', '')
def update_posted(path: str, key: str):
keys = list(open(path, 'r').readline().split(','))
new_keys = [old_key for old_key in keys if old_key != key]
new_text = ','.join([new_key for new_key in new_keys])
with open(path, 'w') as f:
f.write(new_text)
f.close()
async def run_bot():
"""
If the description is too long, it will be cut. Not more than 5 photos will be posted in the next message.
If the description is too long for caption, <= 5 photos will be posted in the next message.
If the description is short, it will be posted with one photo as caption.
"""
bot = telegram.Bot(TELEGRAM_API_KEY)
async with bot:
message = get_message_text(main_artwork_data)
if len(message) >= MAX_POST_LENGTH:
len_wo_desc = len(message) - len(main_artwork_data['description_ru'])
message = get_message_text(main_artwork_data, len_wo_desc, to_cut=True)
print(await bot.send_message(chat_id=channel_id, text=message, parse_mode='markdown', read_timeout=60))
images = [telegram.InputMediaPhoto(photo) for photo in main_artwork_data['img_list'][:5]]
if len(images) > 0:
caption = get_caption_text(main_artwork_data)
print(await bot.send_media_group(chat_id=channel_id, media=images, caption=caption,
parse_mode='markdown', read_timeout=60))
elif MAX_CAPTION_LENGTH <= len(message) < MAX_POST_LENGTH:
print(await bot.send_message(chat_id=channel_id, text=message, parse_mode='markdown', read_timeout=60))
images = [telegram.InputMediaPhoto(photo) for photo in main_artwork_data['img_list'][:5]]
if len(images) > 0:
caption = get_caption_text(main_artwork_data)
print(await bot.send_media_group(chat_id=channel_id, media=images, caption=caption,
parse_mode='markdown', read_timeout=60))
elif len(message) < MAX_CAPTION_LENGTH:
if len(main_artwork_data['img_list']) > 0:
print(await bot.send_photo(chat_id=channel_id, photo=main_artwork_data['img_list'][0],
caption=message, parse_mode='markdown', read_timeout=60))
else:
print(await bot.send_message(chat_id=channel_id, text=message, parse_mode='markdown', read_timeout=60))
if main_artwork_data['category'] != 'Visionary Pioneers of Media Art':
print(await bot.send_message(chat_id=channel_id, text=review_ru, parse_mode='markdown', read_timeout=60))
async def main():
try:
await run_bot()
except Exception:
data[key]['description_ru'] = remove_markdown(data[key]['description_ru'])
await run_bot()
if __name__ == '__main__':
source = 'ars_electronica_prizewinners_ru.json'
data = json.load(open(source, 'r', encoding='utf-8'))
path = 'not_posted.txt'
not_posted = open(path, 'r').readline().split(',')
key = random.choice(not_posted)
print(f'Key is {key}')
main_artwork_data = data[key]
main_artwork = Artwork(main_artwork_data)
artwork_retriever = ArtworkRetriever(source)
related_artworks = artwork_retriever.get_related_artworks(main_artwork)
analysis = ArtworkAnalyser(OPENAI_API_KEY)
analysis_result = analysis.analyze_artworks(main_artwork, related_artworks)
review_ru = analysis_result + '\n\n_Рецензия GPT-4_'
asyncio.run(main())
update_posted(path, key)