diff --git a/.gitignore b/.gitignore index 81498d6..efd4571 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ animeworld.egg-info/ build/ tests/ *.pyc +.idea +.vscode \ No newline at end of file diff --git a/animeworld/utility.py b/animeworld/utility.py index 786bf28..9d1dc28 100644 --- a/animeworld/utility.py +++ b/animeworld/utility.py @@ -2,18 +2,16 @@ Modulo per delle funzioni di utilità. """ import requests -from bs4 import BeautifulSoup import inspect from typing import * -import re +import requests_html from datetime import datetime -import time import locale from .exceptions import DeprecatedLibrary -class MySession(requests.Session): +class MySession(requests_html.HTMLSession): """ Sessione requests. """ @@ -23,20 +21,32 @@ def __init__(self) -> None: self.fixCookie() def fixCookie(self): - AWCookieVerify = re.compile(br'document\.cookie="AWCookieVerify=(.+) ;') - csrf_token = re.compile(br'') - for _ in range(2): # numero di tentativi - res = self.get("https://www.animeworld.tv") - - m = AWCookieVerify.search(res.content) - if m: - self.cookies.update({'AWCookieVerify': m.group(1).decode('utf-8')}) - continue - - m = csrf_token.search(res.content) - if m: - self.headers.update({'csrf-token': m.group(1).decode('utf-8')}) + + r = self.get('https://www.animeworld.tv') + + script = """ + () => { + function getCookie(name) { + const value = `; ${document.cookie}`; + const parts = value.split(`; ${name}=`); + if (parts.length === 2) return parts.pop().split(';').shift(); + } + + return { + SecurityAW: getCookie('SecurityAW'), + csfrToken: window.csrfToken + } + } + """ + # TODO: controllare il timeout + result = r.html.render(script=script, timeout=100) + + if 'SecurityAW' in result.keys(): + self.cookies.update({'SecurityAW': result['SecurityAW']}) + + if 'csfrToken' in result.keys(): + self.headers.update({'csrf-token': result['csfrToken']}) break else: frame = inspect.getframeinfo(inspect.currentframe()) diff --git a/setup.py b/setup.py index cad96f6..c8f2bef 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ long_description_content_type="text/markdown", url="https://github.com/MainKronos/AnimeWorld-API", packages=setuptools.find_packages(), - install_requires=['requests', 'youtube_dl', 'beautifulsoup4'], + install_requires=['requests', 'youtube_dl', 'beautifulsoup4', 'requests-html'], license='MIT', classifiers=[ "Programming Language :: Python :: 3", diff --git a/test.py b/test.py new file mode 100644 index 0000000..f24a0b6 --- /dev/null +++ b/test.py @@ -0,0 +1,34 @@ +import unittest + +import animeworld as aw + +anime_link = "https://www.animeworld.tv/play/summertime-render.GDU38" + + + + +class TestAnimeWorld(unittest.TestCase): + def setUp(self): + self.anime = aw.Anime(link=anime_link) + + def test_Anime(self): + + with self.subTest(msg="Anime"): + self.assertIsInstance(self.anime, aw.Anime) + + with self.subTest(msg="getName"): + self.assertIsInstance(self.anime.getName(), str) + + with self.subTest(msg="getTrama"): + self.assertIsInstance(self.anime.getTrama(), str) + + with self.subTest(msg="getInfo"): + self.assertIsInstance(self.anime.getInfo(), dict) + + # def test_Episodio(self): + # episodi = self.anime.getEpisodes() + + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file