Passed
Push — master ( 1eef4e...280449 )
by Anas
02:26
created

modules.yandere_search.get_anime()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nop 3
dl 0
loc 13
rs 9.75
c 0
b 0
f 0
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from modules.logging import logging_decorator
4
from telegram.ext import CommandHandler
5
from telegram import ChatAction
6
import requests
7
import random
8
9
10
def module_init(gd):
11
    commands = gd.config["commands"]
12
    for command in commands:
13
        gd.dp.add_handler(CommandHandler(command, yandere_search, pass_args=True))
14
15
16 View Code Duplication
@logging_decorator("yandere")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
17
def yandere_search(bot, update, args):
18
    update.message.chat.send_action(ChatAction.UPLOAD_PHOTO)
19
    query = " ".join(args)
20
    try:
21
        direct_link, page_link = get_image(query)
22
    except:
23
        update.message.reply_text("Sorry, something went wrong!")
24
        return
25
    if direct_link is None:
26
        update.message.reply_text("Nothing found!")
27
        return
28
    msg_text = "[Image]({})".format(direct_link) + "\n" + "[View post]({})".format(page_link)
29
    update.message.reply_text(msg_text, parse_mode="Markdown")
30
    return query
31
32
    
33
def get_image(query):
34
    params = {"tags": query}
35
    response = requests.get("https://yande.re/post.json?", params=params)
36
    result_list = response.json()
37
    if not response.text:
38
        return None, None
39
    if not result_list:
40
        return None, None
41
    post = random.choice(result_list)
42
    direct_link, page_link = post.get("file_url"), "https://yande.re/post/show/"+str(post.get("id"))
43
    return direct_link, page_link