1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
from __future__ import unicode_literals |
4
|
|
|
from telegram.ext.dispatcher import run_async |
5
|
|
|
from telegram.ext import CommandHandler |
6
|
|
|
from modules.logging import log_command |
7
|
|
|
from telegram import ChatAction |
8
|
|
|
from datetime import datetime |
9
|
|
|
from pybooru import Moebooru |
10
|
|
|
from random import randint |
11
|
|
|
import requests |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def module_init(gd): |
15
|
|
|
global path |
16
|
|
|
path = gd.config["path"] |
17
|
|
|
commands = gd.config["commands"] |
18
|
|
|
for command in commands: |
19
|
|
|
gd.dp.add_handler(CommandHandler(command, yandere_search, pass_args=True)) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def get_anime(update, query, filename): |
23
|
|
|
update.message.chat.send_action(ChatAction.UPLOAD_PHOTO) |
24
|
|
|
client = Moebooru("yandere") |
25
|
|
|
max_posts_to_load = 200 |
26
|
|
|
posts = client.post_list(tags=query, limit=max_posts_to_load) |
27
|
|
|
post_count = len(posts) |
28
|
|
|
random = randint(0, post_count - 1) |
29
|
|
|
image_post = "https://yande.re/post/show/" + str(posts[random]["id"]) |
30
|
|
|
image_url = posts[random]["sample_url"] |
31
|
|
|
dl = requests.get(image_url) |
32
|
|
|
with open(path + filename + ".jpg", "wb") as f: |
|
|
|
|
33
|
|
|
f.write(dl.content) |
34
|
|
|
return image_post |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
@run_async |
38
|
|
|
def yandere_search(bot, update, args): |
39
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
40
|
|
|
filename = datetime.now().strftime("%d%m%y-%H%M%S%f") |
41
|
|
|
if args == []: |
42
|
|
|
input_query = "rating:s" |
43
|
|
|
else: |
44
|
|
|
input_query = " ".join(args).lower() |
45
|
|
|
try: |
46
|
|
|
cap = get_anime(update, input_query, filename) |
47
|
|
|
with open(path + filename + ".jpg", "rb") as f: |
|
|
|
|
48
|
|
|
update.message.reply_photo(f, caption=cap) |
49
|
|
|
print (current_time, "> /yandere", input_query, ">", update.message.from_user.username) |
50
|
|
|
except: |
51
|
|
|
cap = get_anime(update, "rating:s", filename) |
52
|
|
|
with open(path + filename + ".jpg", "rb") as f: |
53
|
|
|
update.message.reply_photo(f, caption="Nothing found, here's one random pic:\n" + cap) |
54
|
|
|
print (current_time,"> /yandere not found:", input_query, ", sent random", ">", update.message.from_user.username) |
55
|
|
|
log_command(bot, update, current_time, "yandere") |
56
|
|
|
|