This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
outsource_faceapp.py
executable file
·146 lines (110 loc) · 5.54 KB
/
outsource_faceapp.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
from handler.base_plugin import CommandPlugin
from utils import upload_photo
import aiohttp, random, string
BASE_API_URL = 'https://node-01.faceapp.io/api/v2.3/photos' # Ensure no slash at the end.
BASE_HEADERS = {'User-agent': "FaceApp/1.0.229 (Linux; Android 4.4)"}
DEVICE_ID_LENGTH = 8
KNOWN_FILTERS = ('smile', 'smile_2', 'hot', 'old', 'young', 'female', 'male',
'hollywood', 'fun_glasses', 'hitman', 'mustache_free', 'pan', 'heisenberg')
# Thanks to https://github.com/vasilysinitsin/Faces
class FacePlugin(CommandPlugin):
__slots__ = ("filters", "command")
def __init__(self, *commands, prefixes=None, strict=False):
"""Plugin using FaceApp for changing photo."""
if not commands:
commands = ("сделай", "фильтр")
super().__init__(*commands, prefixes=prefixes, strict=strict)
self.filters = {
'улыбка2': 'smile_2',
'весёлой': 'smile',
'весёлым': 'smile',
'весело': 'smile',
'старым': 'old',
'старой': 'old',
'молодым': 'young',
'молодой': 'young',
'мужчиной': 'male',
'мужиком': 'male',
'парнем': 'male',
'поцем': 'male',
'женщиной': 'female',
'тёлкой': 'female',
'тётей': 'female',
'кисой': 'female',
# Вы можете давать свои именя для фильтров из KNOWN_FILTERS здесь
}
self.command = self.command_example()
self.description = [f"FaceApp Фильтр",
f"{self.command} - показать помощь.",
f"{self.command} <фильтр> - использовать фильтр."]
@staticmethod
def _generate_device_id():
device_id = ''.join(random.choice(string.ascii_letters) for _ in range(DEVICE_ID_LENGTH))
return device_id
@staticmethod
def _generate_headers(device_id):
BASE_HEADERS.update({'X-FaceApp-DeviceID': device_id})
return BASE_HEADERS
async def process_message(self, msg):
command, text = self.parse_message(msg)
if not text or text not in self.filters.keys():
return await msg.answer('🙋♂️ Список доступных фильтров:\n' + \
", ".join(self.filters) + '\nВведите ' + self.command + \
' <фильтр> <прикрепленная фотография>')
if not any(k.endswith('_type') and v == "photo"
for k, v in msg.brief_attaches.items()):
return await msg.answer('Вы не прислали фото!\nВведите ' + \
self.command + ' <фильтр> <прикрепленная фотография>')
photo_url = None
for a in await msg.get_full_attaches():
if a.type == "photo" and a.url:
photo_url = a.url
break
else:
return await msg.answer('Произошла какая-то ошибка. Попробуйте другу фотографию.')
await msg.answer("Одну секундочку...")
image = None
async with aiohttp.ClientSession() as sess:
async with sess.get(photo_url) as resp:
image = await resp.read()
if image is None:
return await msg.answer("Ерунда какая-то! Ошибка...")
device_id = self._generate_device_id()
headers = self._generate_headers(device_id)
code = None
async with aiohttp.ClientSession() as sess:
async with sess.post(BASE_API_URL, headers=headers, data={'file': image}) as resp:
try:
response = await resp.json()
except ValueError:
response = None
code = response.get('code')
if code is None:
error = resp.headers.get('X-FaceApp-ErrorCode')
if error == 'photo_bad_type':
return await msg.answer("Плохая у тебя картинка, пф")
elif error == 'photo_no_faces':
return await msg.answer("Не вижу лиц \\_C:_/")
return await msg.answer("Хм... Ошибка...")
filter_name = text.strip().lower()
filter_name = self.filters.get(filter_name, filter_name)
if filter_name in ('male', 'female'):
cropped = 1
else:
cropped = 0
async with aiohttp.ClientSession() as sess:
async with sess.get(
'{0}/{1}/filters/{2}?cropped={3}'.format(
BASE_API_URL, code, filter_name, cropped
), headers=headers) as resp:
image = await resp.read()
error = resp.headers.get('X-FaceApp-ErrorCode')
if error:
if error == 'bad_filter_id':
return await msg.answer("Какой-то фильтр у тебя неправильный очень...")
else:
return await msg.answer("Чо... Я сломался :(")
at = await upload_photo(self.api, image)
if not at:
return await msg.answer("Не удалось отправить картинку!")
return await msg.answer(";)", attachment=at)