-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcuriosity.py
459 lines (428 loc) · 19.6 KB
/
curiosity.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# -*- coding: utf-8 -*-
import re
import time
import requests
import vk_api
from PIL import Image, ImageFont, ImageDraw, ImageFilter, ImageOps
from bs4 import BeautifulSoup
import CuriosityTopicparser
import CuriosityTrendingparser
# TODO: 2. Прописать рекурсивную подгузку и доработку поста
# TODO: 2.1. ссылки на магазин
# TODO: 2.2. ссылки на журнал
# TODO: 2.1. апач с файлами HTML
# TODO: 3. Добавить логотип школы изобылия
# TODO: 4. Запустить асинхронного бота
# TODO: 4.1 Настроить репост
class Curiosity:
# РЕГУЛЯРНЫЕ ВЫРАЖЕНИЯ
re_zero_img = re.compile(r"https://curiosity-data\.s3\.amazonaws\.com/images/content/meme/standard/(.*?)\.png")
# СПИСКИ С АНГЛИЙСКИМ ТЕКТОМ
topic_href = []
topic_channel = []
topic_tags = []
topic_title = []
topic_img_0_href = []
topic_img_0_hrefs = []
topic_img_0_scr = []
topic_img_0_alt = []
topic_text_1 = []
topic_img_1_href = []
topic_img_1_scr = []
topic_img_2_href = []
topic_img_2_src = []
topic_img_3_href = []
topic_img_3_src = []
topic_video_1_title = []
topic_video_1_data_scr = []
# СПИСКИ С РУССКИМ ТЕКСТОМ
topic_channel_ru = []
topic_title_ru = []
topic_img_0_alt_ru = []
topic_text_1_ru = []
topic_tags_ru = []
def __init__(self, *args):
self.args = args
# ПОМОШНИК АНАЛИЗАТОРА
def img_0_alt_parser():
respon = requests.get("http://curiosity.com/trending/day/")
html = respon.text
soup = BeautifulSoup(html, "lxml")
trending_grid = soup.find("div", {"class": "js-trending-grid"})
all_a = trending_grid.find_all("img")
for item in all_a:
alt = item["alt"]
Curiosity.topic_img_0_alt.append(str(alt))
print("Анализатор получил текст заманух")
# ГРУЗЧИК
def img_0_downloader():
respon = requests.get("http://curiosity.com/trending/day/")
html = respon.text
zero_img_srcs = re.findall(Curiosity.re_zero_img, html)
for x in zero_img_srcs[::3]:
Curiosity.topic_img_0_hrefs.append(x)
count = 0
max_ind = len(Curiosity.topic_channel) - 1
while count <= max_ind:
Curiosity.topic_img_0_href.append(
"http://curiosity-data.s3.amazonaws.com/images/content/meme/standard/" + Curiosity.topic_img_0_hrefs[
count] + ".png")
res = requests.get(Curiosity.topic_img_0_href[count])
with open('C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/0-img-' + str(count) + '.png',
'wb') as zero:
zero.write(res.content)
Curiosity.topic_img_0_scr.append(
'/home/ubuntu/workspace/curiosity-to-v/topics/0-img-' + str(count) + '.png')
count += 1
print("Скачены изображениея обложек")
return Curiosity.topic_img_0_scr
# ПЕРЕВОДЧИК
def translater():
# ВРЕМЕННЫЕ ПЕРЕМЕННЫЕ ДЛЯ ЦИКЛОВ
count = 0
max_index = len(Curiosity.topic_channel) - 1
# ПЕРЕВОДИМ СПИСКИ
while count <= max_index:
# КАНАЛ
channel = {
"key": "trnsl.1.1.20170730T114755Z.994753b77b648f24.f3ed7d2f59fcb232c089a1a3328c0e0b900d4925",
"text": f"{Curiosity.topic_channel[count]}.",
'lang': 'en-ru',
'format': 'plain'
}
# ЗАГОЛОВОК
title = {
"key": "trnsl.1.1.20170514T220842Z.5b2c14ecd7990670.3ccb355751262f1359f3c3ff0b9b7d5447ce39a1",
"text": f"{Curiosity.topic_title[count]}.",
'lang': 'en-ru',
'format': 'plain'
}
# ЗАМАНУХА
alt = {
"key": "trnsl.1.1.20170514T220842Z.5b2c14ecd7990670.3ccb355751262f1359f3c3ff0b9b7d5447ce39a1",
"text": f"{Curiosity.topic_img_0_alt[count]}.",
'lang': 'en-ru',
'format': 'plain'
}
# ТЕКСТ
text_1 = {
"key": "trnsl.1.1.20170514T220842Z.5b2c14ecd7990670.3ccb355751262f1359f3c3ff0b9b7d5447ce39a1",
"text": f"{Curiosity.topic_text_1[count]}.",
'lang': 'en-ru',
'format': 'plain'
}
# ТЕГИ
'''
tags = {
"key": "trnsl.1.1.20170514T220842Z.5b2c14ecd7990670.3ccb355751262f1359f3c3ff0b9b7d5447ce39a1",
"text": f"{Curiosity.topic_tags[count]}.",
'lang': 'en-ru',
'format': 'plain'
}
'''
# ДЕЛАЕМ ЗАПРОС К ЯНДЕКС ПЕРЕВОДЧИКУ И СОХРАНЯЕМ ОТВЕТ
channel_ru = requests.get('https://translate.yandex.net/api/v1.5/tr.json/translate', params=channel).json()
# КАНАЛ
title_ru = requests.get('https://translate.yandex.net/api/v1.5/tr.json/translate', params=title).json()
# ЗАГОЛОВОК
text_1_ru = requests.get('https://translate.yandex.net/api/v1.5/tr.json/translate', params=text_1).json()
# ЗАМАНУХА
img_0_alt_ru = requests.get('https://translate.yandex.net/api/v1.5/tr.json/translate', params=alt).json()
# ТЕГИ
# tags_ru = requests.get('https://translate.yandex.net/api/v1.5/tr.json/translate', params=tags).json()
# ЗАПОЛНЯЕМ СПИСКИ С РУССКИМ ТЕКСТОМ
# ЗАГОЛОВОК
Curiosity.topic_title_ru.append(title_ru['text'][0])
# ЗАГОЛОВОК
Curiosity.topic_channel_ru.append(channel_ru['text'][0])
# КАНАЛ
Curiosity.topic_text_1_ru.append(text_1_ru['text'][0])
# ЗАМАНУХА
Curiosity.topic_img_0_alt_ru.append(img_0_alt_ru["text"][0])
# ТЕГИ
# Curiosity.topic_tags_ru.append((tags_ru["text"][0]))
count += 1
print("Переводчик выполнил свою работу")
# КИСТЬ
def draw(count):
# =========================
# #######НАСТРОЙКА КИСТИ##########
# =========================
# НАЗВАНИЕ КАНАЛА
try:
channel = Curiosity.topic_channel_ru[count].upper()
# ЗАГОЛОВОК
if len(Curiosity.topic_title_ru[count]) <= 40:
title = Curiosity.topic_title_ru[count]
else:
titlelist = list(Curiosity.topic_title_ru[count])
titlelist.insert(40, '-\n')
title = ''.join(titlelist)
except AttributeError:
channel = "УДИВИТЕЛЬНАЯ ПЛАНЕТА"
title = "Ну что вы тут ещё не видели люди на этой удивительной планете?"
if len(title) <= 41:
title = title
else:
titlelist = list(title)
titlelist.insert(41, '\n')
title = ''.join(titlelist)
# ОБЛОЖКА ТОПИКА НА АНГЛ.
img_composit = Image.open(
"C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/0-img-" + str(count) + ".png").convert("RGBA")
# БАЗА, ОНА ЖЕ - ЛОГО_ПАИНТЕР
logo_painter = Image.open('C:/Users/Елена/PycharmProjects/curiosity-to-vk/desing/logo-playload.png').convert("RGBA")
# ШРИФТЫ
channel_font = ImageFont.truetype(
"C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/Roboto-Fonts/Roboto-Bold.ttf", 42)
title_font = ImageFont.truetype("C:/Users/Елена/PycharmProjects/curiosity-to-vk/Roboto-Fonts/Roboto-Bold.ttf", 42)
# РАЗМЕР БЛОКА ТЕКСТА С НАЗВАН
channel_size = channel_font.getsize(str(channel))
# РАЗМЕР БЛОКА ТЕКСТА С НАЗВАНИЕМ КАНАЛА В КОРТЕЖЕ
_size = (channel_size[0] + 20, channel_size[1] + 40)
# НОВОЕ ИЗОБРАЖЕНИЕ ДЛЯ НАНЕСЕНИЯ ТЕКСТА С НАЗВАНИЕМ КАНАЛА
channel_im = Image.open('./Button.png').convert("RGBA")
# ИЗМЕНЯЕМ РАЗМЕР ИЗОБРАЖЕНИЯ
channel_img = channel_im.resize(_size, resample=0)
# КИСТЬ для пустое ИЗОБРАЖЕНИЕ для нанесения текста с названием КАНАЛА
channel_draw = ImageDraw.Draw(channel_img)
# МЕТОД ПРОРИСОВКИ мультистрокового ТЕКСТА с названием канала на изобрание
x = (_size[0] - channel_size[0]) / 2
y = (_size[1] - channel_size[1]) / 2
channel_draw.multiline_text((x, y), channel, font=channel_font, align="center")
# ========================
# #########МОДИФИКАЦИЯ ИЗОБРАЖЕНИЯ#######
# =========================
# МОДИФИКАЦИЯ нижней части ОСНОВНОГО ИЗОБРАЖЕНИЯ
box = (1, 875, 999, 999)
# ВЫРЕЗАЕМ
text = img_composit.crop(box)
# СОЗДАЕМ ФИЛТР
gaus = ImageFilter.GaussianBlur(radius=20)
# ПРИМЕНЯЕМ ФИЛТР К ВЫРЕЗКЕ
textarea = text.filter(gaus)
# АВТОКОНТРАСТ
# ImageOps.autocontrast(textarea, cutoff=0, ignore=None)
# УДАЛЯЕМ ГРАНИЦЫ
ImageOps.crop(textarea)
# ВСТАВЛЯЕМ ВЫРЕЗКУ НАЗАД
img_composit.paste(textarea, (1, 875))
img_composit.save("./topics/0-img-" + str(
count) + ".png")
# КИСТЬ для ЗАГРУЗЧИКА
logo_painter_draw = ImageDraw.Draw(logo_painter)
# ПРОРИСОВКА канал загрузчик
logo_painter.paste(channel_img, (27, 800))
# ПРОРИСОВКА заголовок на ЗАГРУЗЧИК ЛОГОТИПОВ
logo_painter_draw.multiline_text((27, 890), title, font=title_font, align="left") # fill=(255,0,255,255)
# МОДИФИКАЦИЯ нижней части ОСНОВНОГО ИЗОБРАЖЕНИЯ
# ВЫРЕЗАЕМ
img_composits = Image.open(
"C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/0-img-" + str(count) + ".png").convert("RGBA")
logo_box = (1, 70, 999, 70)
logob = img_composits.crop(logo_box)
gaus = ImageFilter.GaussianBlur(radius=2)
logoArea = logob.filter(gaus)
img_composits.paste(logoArea, (1, 1))
# ЗАКАТЫВАЕМ ПОЛУЧЕНЫЙ КОМПОТ
img_composits.save("./topics/0-img-" + str(count) + ".png")
# СВЕДЕНИЕ СЛОЕВ обложки и загрузчика логотипов
img_composite = Image.open("./topics/0-img-" + str(count) + ".png", mode='r').convert("RGBA")
img_composite = Image.alpha_composite(img_composite, logo_painter)
# СОХРАНЯЕМ РЕЗУЛЬТАТ - ГОТОВУЮ ОБЛОЖКУ ПОСТА в файл
img_composite.save("./topics/0-img-" + str(count) + "-composite.png")
# ГЛАВНЫЙ АНАЛИЗАТОР
def topicsparser():
# ПОЛУЧАЕМ СГРУПИРОВАННЫЕ ССЛЫКИ НА ПОСТЫ ОТ АНАЛИЗАТОРА ТРЕНДОВ
in_db, new, to_post = CuriosityTrendingparser.TrendingParser.change_href()
for href in new:
try:
img_1_href, channel, title, text_1, img_2_href, img_3_href, video_1_title, video_1_data_scr = CuriosityTopicparser.topic_parser(
href)
# ЗАПОЛНЯЕМ СПИСКИ
# каналы
Curiosity.topic_channel.append(str(channel))
# заголовки
Curiosity.topic_title.append(str(title))
# ссылок на 1 изображения
Curiosity.topic_img_1_href.append(
"http://curiosity-data.s3.amazonaws.com/images/content/hero/standard/" + img_1_href[0] + ".png")
# текты первых блоков
Curiosity.topic_text_1.append(str(text_1))
# ссылки на 2 изображения
Curiosity.topic_img_2_href.append(str(img_2_href))
# ссылки на 3 изображения
Curiosity.topic_img_3_href.append(str(img_3_href))
# заголовки видеороликов
Curiosity.topic_video_1_title.append(str(video_1_title))
# ID видеороликов
Curiosity.topic_video_1_data_scr.append(str(video_1_data_scr))
# ТЕГИ
#Curiosity.topic_tags.append(str(topic_tag))
except ArithmeticError:
print("Ошибочка выскочила")
# ЖУРНАЛИСТ
def post():
# Аутинтификация
login, password = '89045155434', '778899'
vk_session = vk_api.VkApi(login, password)
# проверка сессиии
try:
vk_session.auth()
except vk_api.AuthError as error_msg:
print(error_msg)
return
# получаем объект API
vk = vk_session.get_api()
# получаем файловый объект сессии используемый для загрузки
upload = vk_api.VkUpload(vk_session)
# ЦИКЛ ПОСТОВ
count = 0
max_index = 8
while count <= max_index:
# получаем объект конкретной фотографии
try:
photo = upload.photo(
'C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/0-img-' + str(count) + '-composite.png',
album_id=248018572)
except:
photo = None
print("Фото 1 не загруженно")
try:
photo2 = upload.photo(
'C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/1-img-' + str(count) + '.png',
album_id=248018572)
except:
photo2 = None
print("Фото 2 не загруженно")
try:
photo3 = upload.photo(
'C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/2-img-' \
+ str(count) + '.png',
album_id=248018572)
except:
photo3 = None
print("Фото 2 не загруженно")
try:
photo4 = upload.photo(
'C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/3-img-' + str(count) + '.png',
album_id=248018572)
except:
photo4 = None
print("Фото 3 не загруженно")
# ССЫЛКА НА САЙТ
# ☀☀☀☀☀☀☀ 🌈🌈🌈🌈🌈🌈 ✨✨✨✨✨🇷🇺 💡 🇷🇺
# ПОДГОТОВКА ПЕРЕМЕННЫХ ДЛЯ ПОСТА
# tag = Curiosity.topic_tags_ru[count].replace("\n", "#")
# tag = tag.replace(".", "")
# tag = tag.replace('#', "🇷🇺#")
# channel_tag = Curiosity.topic_channel_ru[count].replace(".", "")
topic_text = Curiosity.topic_text_1_ru[count].replace("\n\n\n", "\n", 1)
# ТЕКСТ СТАТЬИ ДЛЯ ПОСТА # {tag}{channel_tag}🇷🇺\n\n
post_message = f"✨✨✨Любопытство делает Вас умнее✨✨✨\n💡💡💡{Curiosity.topic_img_0_alt_ru[count].replace('n', ' ').upper()}💡💡💡\n{topic_text}"
# ВИДОС
link = f"https://www.youtube.com/watch?v={Curiosity.topic_video_1_data_scr[count]}"
if photo and photo2 and photo3 and photo4 is None:
vk.wall.post(
owner_id=279286486,
friends_only=0,
from_group=0,
message=str(post_message[:]),
attachments=f'photo{photo[0]["owner_id"]}_{photo[0]["id"]},photo{photo2[0]["owner_id"]}_{photo2[0]["id"]},photo{photo3[0]["owner_id"]}_{photo3[0]["id"]},photo{photo4[0]["owner_id"]}_{photo4[0]["id"]}, {link}')
elif photo and photo2 and photo3 is not None:
vk.wall.post(
owner_id=279286486,
friends_only=0,
from_group=0,
message=str(post_message[:]),
attachments=f'photo{photo[0]["owner_id"]}_{photo[0]["id"]},photo{photo2[0]["owner_id"]}_{photo2[0]["id"]},photo{photo3[0]["owner_id"]}_{photo3[0]["id"]}, {link}')
elif photo and photo2 is not None:
vk.wall.post(
owner_id=279286486,
friends_only=0,
from_group=0,
message=str(post_message[:]),
attachments=f'photo{photo[0]["owner_id"]}_{photo[0]["id"]},photo{photo2[0]["owner_id"]}_{photo2[0]["id"]}, {link}')
else:
vk.wall.post(
owner_id=279286486,
friends_only=0,
from_group=0,
message=str(post_message[:]),
attachments=f'photo{photo[0]["owner_id"]}_{photo[0]["id"]}, {link}')
print(f"Пост № {str(count)} выполнен")
# time.sleep(600)
count += 1
# ГРУЗЧИК
def img_3_downloader():
count = 0
max_index = len(Curiosity.topic_img_3_href) - 1
while count <= max_index:
try:
res = requests.get(Curiosity.topic_img_3_href[count])
with open('C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/3-img-' + str(count) + '.png',
'wb') as zero:
zero.write(res.content)
# Curiosity.topic_img_3_scr.append('/home/ubuntu/workspac/curiosity-to-v/topics/two-img-' + str(count) + '.png')
except:
with open('C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/3-img-' + str(count) + '.png',
'wb') as zero:
zero.close()
count += 1
print("Скачены третьи изображениея")
# return Curiosity.topic_img_3_scr
# ГРУЗЧИК
def img_2_downloader():
count = 0
max_index = len(Curiosity.topic_img_2_href) - 1
while count <= max_index:
try:
res = requests.get(Curiosity.topic_img_2_href[count])
with open('C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/2-img-' + str(count) + '.png',
'wb') as zero:
zero.write(res.content)
except:
with open('C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/2-img-' + str(count) + '.png',
'wb') as zero:
zero.close()
count += 1
print("Скачены вторые изображениея")
# ГРУЗЧИК
def img_1_downloader():
count = 0
max_index = len(Curiosity.topic_img_1_href) - 1
while count <= max_index:
res = requests.get(Curiosity.topic_img_1_href[count])
with open('C:/Users/Елена/PycharmProjects/curiosity-to-vk/topics/1-img-' + str(count) + '.png',
'wb') as zero:
zero.write(res.content)
Curiosity.topic_img_1_scr.append(
'/home/ubuntu/workspac/curiosity-to-v/topics/1-img-' + str(count) + '.png')
count += 1
print("Скачены первые изображениея")
return Curiosity.topic_img_1_scr
# ХУДОЖНИК
def painters() -> object:
# ЦИКЛ ПРОХОДА ИЗОБРАЖЕНИЙ ДЛЯ ХУДОЖНИКА
count = 0
max_index = len(Curiosity.topic_title) - 1
while count <= max_index:
draw(count)
count += 1
print(f"ХУДОЖНИК УСПЕШНО ОТРИСОВАЛ {max_index} ИЗОБРАЖЕНИЙ")
return
# ======ТЕСТЫ==========ТЕСТЫ===============ТЕСТЫ=============ТЕСТЫ============== #
CuriosityTrendingparser.TrendingParser.change_href()
img_0_alt_parser()
topicsparser()
img_0_downloader()
translater()
painters()
img_1_downloader()
img_3_downloader()
img_2_downloader()
post()
x = 0
# ======КОД ВЫПОЛНЯЕМЫЙ ПРИ ИМПОРТЕ============== #
if __name__ == "__main__":
print("Любопытcтво делает вас умнее")