|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
from modules.logging import logging_decorator |
|
4
|
|
|
from telegram.ext.dispatcher import run_async |
|
5
|
|
|
from telegram.ext import PrefixHandler |
|
6
|
|
|
from telegram import ChatAction |
|
7
|
|
|
import requests |
|
8
|
|
|
import random |
|
9
|
|
|
import urllib.parse |
|
10
|
|
|
|
|
11
|
|
|
yandere_request_link = "https://yande.re/post.json?limit=100" |
|
12
|
|
|
yandere_post_link = "https://yande.re/post/show/" |
|
13
|
|
|
gelbooru_request_link = "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1" |
|
14
|
|
|
gelbooru_post_link = "https://gelbooru.com/index.php?page=post&s=view&id=" |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def module_init(gd): |
|
18
|
|
|
global proxies |
|
19
|
|
|
commands_gelbooru = gd.config["commands_gelbooru"] |
|
20
|
|
|
commands_yandere = gd.config["commands_yandere"] |
|
21
|
|
|
proxyuser = gd.config["proxy"]["user"] |
|
22
|
|
|
proxypassword = gd.config["proxy"]["password"] |
|
23
|
|
|
proxyserver = gd.config["proxy"]["server"] |
|
24
|
|
|
proxylink = "socks5://"+proxyuser+":"+proxypassword+"@"+proxyserver |
|
25
|
|
|
proxies = {"https" : proxylink} |
|
26
|
|
|
for command in commands_gelbooru: |
|
27
|
|
|
gd.dp.add_handler(PrefixHandler("/", command, gelbooru_search)) |
|
28
|
|
|
for command in commands_yandere: |
|
29
|
|
|
gd.dp.add_handler(PrefixHandler("/", command, yandere_search)) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
@run_async |
|
33
|
|
|
@logging_decorator("yandere") |
|
34
|
|
|
def yandere_search(update, context): |
|
35
|
|
|
query = search(update, context, yandere_request_link, yandere_post_link) |
|
36
|
|
|
return query |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
@run_async |
|
40
|
|
|
@logging_decorator("gelbooru") |
|
41
|
|
|
def gelbooru_search(update, context): |
|
42
|
|
|
query = search(update, context, gelbooru_request_link, gelbooru_post_link, 'gelbooru') |
|
43
|
|
|
return query |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def search(update, context, request_link, image_link, search_place='yandere'): |
|
47
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
|
48
|
|
|
query = " ".join(context.args) |
|
49
|
|
|
try: |
|
50
|
|
|
if search_place == 'gelbooru': |
|
51
|
|
|
direct_link, page_link, sample_link = get_gelbooru_image(query, request_link, image_link) |
|
52
|
|
|
else: |
|
53
|
|
|
direct_link, page_link, sample_link = get_image(query, request_link, image_link) |
|
54
|
|
|
|
|
55
|
|
|
except: |
|
56
|
|
|
update.message.reply_text("Sorry, something went wrong!") |
|
57
|
|
|
return |
|
58
|
|
|
if direct_link is None: |
|
59
|
|
|
update.message.reply_text("Nothing found!") |
|
60
|
|
|
return |
|
61
|
|
|
msg_text = "[Image]({})".format(direct_link) + "\n" + "[View post]({})".format(page_link) |
|
62
|
|
|
update.message.reply_text(msg_text, parse_mode="Markdown") |
|
63
|
|
|
return query |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def get_gelbooru_image(query, request_link, image_link): |
|
67
|
|
|
params = {"tags": query.replace(' ', '+')} # replace space for plus symbol |
|
68
|
|
|
params_str = urllib.parse.urlencode(params, safe=':+') # prevent percenting symbols in request |
|
69
|
|
|
response = requests.get(request_link, params=params, proxies=proxies) |
|
|
|
|
|
|
70
|
|
|
result_obj = response.json() |
|
71
|
|
|
if not response.text: |
|
72
|
|
|
return None, None |
|
73
|
|
|
if not result_obj: |
|
74
|
|
|
return None, None |
|
75
|
|
|
if result_obj['@attributes']['count'] == 0: # check if nothing found |
|
76
|
|
|
return None, None |
|
77
|
|
|
post = random.choice(result_obj['post']) # get random post from list |
|
78
|
|
|
direct_link, page_link, sample_link = post.get("file_url"), image_link+str(post.get("id")), post.get("sample_url") |
|
79
|
|
|
return direct_link, page_link, sample_link |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def get_image(query, request_link, image_link): |
|
83
|
|
|
params = {"tags": query} |
|
84
|
|
|
response = requests.get(request_link, params=params, proxies=proxies) |
|
|
|
|
|
|
85
|
|
|
result_list = response.json() |
|
86
|
|
|
if not response.text: |
|
87
|
|
|
return None, None |
|
88
|
|
|
if not result_list: |
|
89
|
|
|
return None, None |
|
90
|
|
|
post = random.choice(result_list) |
|
91
|
|
|
direct_link, page_link, sample_link = post.get("file_url"), image_link+str(post.get("id")), post.get("sample_url") |
|
92
|
|
|
return direct_link, page_link, sample_link |
|
93
|
|
|
|