-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.py
71 lines (58 loc) · 2.43 KB
/
images.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
# -*- coding: utf-8 -*-
try:
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
except:
pass
import json
import pickle
import os.path
import random
short_names = ["Левитан", "Айвазовский", "Рубенс", "Серов", "Шишкин", "Кандинский", "Брюллов", "Аргунов", "Васнецов",
"Верещагин", "Пукирев", "Суриков", "Репин", "Рублев", "Крамской", "Водкин", "Врубель", "Йорданс", "Иорданс", "Ван Гог",
"Гойен", "Гоген", "Джордано", "Матисс", "Грёз", "Сезанн", "Якобс", "Ренуар", "Моне", "Мане", "Пикассо", "Рембрандт",
"Вос", "Рафаэль", "Кранах", "Делакруа", "Иорданс", "Кранах", "Тинторетто", "Тициан", "Рибера", "Карраччи",
"Ван Дейк", "Браувер"]
def load_images():
json_file = open("static/images_db", "r")
images = json.load(json_file)
return images
images = load_images()
def get_image_link(image):
return image['data']['general']['mainImage']['preview']
def get_image_name(image):
return image['data']['general']['name']
def get_image_author(image):
return image['data']['general']['author']
def get_authors():
authors = set()
for x in map(get_image_author, images):
if x:
authors.add(x)
return authors
def get_pic():
image = random.choice(images)
return {'author': get_image_author(image),
'title': get_image_name(image),
'link': get_image_link(image)}
def find_short_name(author):
for name in short_names:
if name.lower() in author.lower():
return name
def find_long_name(short_name):
images_list = list(filter(lambda x: short_name.lower() in x.lower(), get_authors()))
if images_list:
return random.choice(images_list)
return short_name
def get_rand_names_except_author(author):
short_name = find_short_name(author)
if not short_name:
return "Ван Гог", "Босхе"
name_1 = random.choice(short_names)
while name_1 == short_name:
name_1 = random.choice(short_names)
name_2 = random.choice(short_names)
while name_2 == short_name or name_2 == name_1:
name_2 = random.choice(short_names)
return find_long_name(name_1), find_long_name(name_2)