import requests import help_methods supported_templates = sorted( list( map( lambda x: x["id"], requests.get("https://api.memegen.link/templates/").json(), ) ) ) def parse_message(template, top, bottom): escaped_chars = { "_": "__", " ": "_", "-": "--", "?": "~q", "%": "~p", "#": "~h", "/": "~s", "''": '"', } # Escape special characters in top/bottom text for char, escaped in escaped_chars.items(): top = top.replace(char, escaped) bottom = bottom.replace(char, escaped) usesTemplate = True if template.startswith("http"): usesTemplate = False elif template not in supported_templates: return "Template not supported!" return get_meme_url(template, top, bottom, usesTemplate) def get_meme_url(template, top, bottom, usesTemplate): # TODO: Implement format as a parameter? base_URL = "https://memegen.link" custom_URL = "{}/custom".format(base_URL) default_format = "jpg" # Generate meme url meme = "{}/{}/{}.{}?alt={}".format( custom_URL, top, bottom, default_format, template ) if usesTemplate: meme = "{}/{}/{}/{}.{}".format(base_URL, template, top, bottom, default_format) return meme def get_meme_help(): return "{}\nYou must supply a valid template for the meme. Templates are one of the following: \n```{}```".format( help_methods.get_help_message("meme"), ", ".join(supported_templates) )