forked from KeyZenD/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYandexRS.py
55 lines (53 loc) · 2.12 KB
/
YandexRS.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
from .. import loader, utils
import json
import io
import requests
from PIL import Image
import random
import string
@loader.tds
class YandexReverseSearchMod(loader.Module):
"""Reverse image search via Yandex (he is the best, imho)"""
strings = {"name": "YandexReverseSearch",
"search": "⚪⚪⚪\n⚪❓⚪\n⚪⚪⚪",
"no_reply": "<b>Reply to image or sticker!</b>",
"result": '<a href="{}"><b>🔴⚪🔴|See</b>\n<b>⚪🔴⚪|Search</b>\n<b>⚪🔴⚪|Results</b></a>',
"error": '<b>Something went wrong...</b>'}
@loader.owner
async def yarscmd(self, message):
""".yars <repy to image>"""
reply = await message.get_reply_message()
data = await check_media(message, reply)
if not data:
await utils.answer(message, self.strings("no_reply", message))
return
await utils.answer(message, self.strings("search", message))
searchUrl = 'https://yandex.ru/images/search'
files = {'upfile': ('blob', data, 'image/jpeg')}
params = {'rpt': 'imageview', 'format': 'json', 'request': '{"blocks":[{"block":"b-page_type_search-by-image__link"}]}'}
response = requests.post(searchUrl, params=params, files=files)
if response.ok:
query_string = json.loads(response.content)['blocks'][0]['params']['url']
link = searchUrl + '?' + query_string
text = self.strings("result", message).format(link)
await utils.answer(message, text)
else:
await utils.answer(message, self.strings("error", message))
async def check_media(message, reply):
if reply and reply.media:
if reply.photo:
data = reply.photo
elif reply.document:
if reply.gif or reply.video or reply.audio or reply.voice:
return None
data = reply.media.document
else:
return None
else:
return None
if not data or data is None:
return None
else:
data = await message.client.download_file(data, bytes)
img = io.BytesIO(data)
return img