import re def animepahe_key_creator(c: int, a: int): from ...scraping.utils import encode_base_n if c < a: val_a = "" else: val_a = animepahe_key_creator(int(c / a), a) c = c % a if c > 35: val_b = chr(c + 29) else: val_b = encode_base_n(c, 36) return val_a + val_b def animepahe_embed_decoder( encoded_js_p: str, base_a: int, no_of_keys_c: int, values_to_replace_with_k: list, ): decode_mapper_d: dict = {} for i in range(no_of_keys_c): key = animepahe_key_creator(i, base_a) val = values_to_replace_with_k[i] or key decode_mapper_d[key] = val return re.sub( r"\b\w+\b", lambda match: decode_mapper_d[match.group(0)], encoded_js_p ) PARAMETERS_REGEX = re.compile(r"eval\(function\(p,a,c,k,e,d\)\{.*\}\((.*?)\)\)$") ENCODE_JS_REGEX = re.compile(r"'(.*?);',(\d+),(\d+),'(.*)'\.split") def process_animepahe_embed_page(embed_page: str): from ...scraping.html_parser import get_element_text_and_html_by_tag encoded_js_string = "" embed_page_content = embed_page for _ in range(8): text, html = get_element_text_and_html_by_tag("script", embed_page_content) if not text and html: embed_page_content = re.sub(html, "", embed_page_content) continue if text: encoded_js_string = text.strip() break if not encoded_js_string: return obsfucated_js_parameter_match = PARAMETERS_REGEX.search(encoded_js_string) if not obsfucated_js_parameter_match: return parameter_string = obsfucated_js_parameter_match.group(1) encoded_js_parameter_string = ENCODE_JS_REGEX.search(parameter_string) if not encoded_js_parameter_string: return p: str = encoded_js_parameter_string.group(1) a: int = int(encoded_js_parameter_string.group(2)) c: int = int(encoded_js_parameter_string.group(3)) k: list = encoded_js_parameter_string.group(4).split("|") return animepahe_embed_decoder(p, a, c, k).replace("\\", "") if __name__ == "__main__": # Testing time filepath = input("Enter file name: ") if filepath: with open(filepath) as file: data = file.read() else: data = """""" print(process_animepahe_embed_page(data))