import re from .. import helpers from . import getSources from random import randint from requests import get as reqget from random import choice as randchoice def getJoke(request): lang = helpers.getFromRequest(request, "lang") if not lang: return {"status": "error", "details": { "error_code": 133, "error_details": "No lang provided" }}, 400 elif lang.lower() not in ['ru']: return {"status": "error", "details": { "error_code": 133, "error_details": "Lang not supported" }}, 400 else: lang = lang.lower() source = helpers.getFromRequest(request, "source") availableSources = getSources.getSources(None) if not source or source not in availableSources[lang]: source = randchoice(availableSources[lang]) try: if source == "nekdo": site = reqget("https://nekdo.ru/random/").text joke = randchoice(re.findall('
')[2].partition('
')[0].replace('')[2].partition('
')[0].replace('", "").replace("
", "").strip() elif source == "anepedia": site = reqget("https://www.anepedia.mobi/%D0%A1%D0%BB%D1%83%D1%87%D0%B0%D0%B9%D0%BD%D1%8B%D0%B9_%D0%B0%D0%BD%D0%B5%D0%BA%D0%B4%D0%BE%D1%82").content.decode() joke = re.sub(r']*>', '', site.partition('