| Total Complexity | 8 |
| Total Lines | 44 |
| Duplicated Lines | 34.09 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 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") |
|
|
|
|||
| 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 |