From bd3ecb7c85beebbe38c9bfca4b752bbce4e555de Mon Sep 17 00:00:00 2001 From: Simone Date: Tue, 19 Jul 2022 18:00:39 +0200 Subject: [PATCH 1/4] Add .idea folder to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 81498d6..3740b04 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ animeworld.egg-info/ build/ tests/ *.pyc +.idea \ No newline at end of file From f4deeb0766de66afa79db8622cd78289f4c77b37 Mon Sep 17 00:00:00 2001 From: Simone Date: Tue, 19 Jul 2022 18:02:47 +0200 Subject: [PATCH 2/4] fix(cookie): get cookie and csrf-token --- animeworld/utility.py | 45 ++++++++++++++++++++++++++++--------------- setup.py | 2 +- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/animeworld/utility.py b/animeworld/utility.py index 786bf28..1b89177 100644 --- a/animeworld/utility.py +++ b/animeworld/utility.py @@ -2,13 +2,11 @@ Modulo per delle funzioni di utilità. """ import requests -from bs4 import BeautifulSoup import inspect from typing import * -import re +from requests_html import HTMLSession from datetime import datetime -import time import locale from .exceptions import DeprecatedLibrary @@ -23,20 +21,35 @@ 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')}) + + session = HTMLSession() + r = session.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 + } + } + """ + + result = r.html.render(script=script) + + if 'SecurityAW' in result.keys(): + print(result['SecurityAW']) + self.cookies.update({'SecurityAW': result['SecurityAW']}) + + if 'csfrToken' in result.keys(): + print(result['csfrToken']) + 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", From 5dcca2ed28ae52d009d1e2a603a490774fab4980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=B3nos?= Date: Tue, 19 Jul 2022 18:40:57 +0200 Subject: [PATCH 3/4] Aggiornamento coerenza classe MySession --- animeworld/utility.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/animeworld/utility.py b/animeworld/utility.py index 1b89177..b9bea67 100644 --- a/animeworld/utility.py +++ b/animeworld/utility.py @@ -4,14 +4,14 @@ import requests import inspect from typing import * -from requests_html import HTMLSession +import requests_html from datetime import datetime import locale from .exceptions import DeprecatedLibrary -class MySession(requests.Session): +class MySession(requests_html.HTMLSession): """ Sessione requests. """ @@ -23,8 +23,7 @@ def __init__(self) -> None: def fixCookie(self): for _ in range(2): # numero di tentativi - session = HTMLSession() - r = session.get('https://www.animeworld.tv') + r = self.get('https://www.animeworld.tv') script = """ () => { @@ -44,11 +43,9 @@ def fixCookie(self): result = r.html.render(script=script) if 'SecurityAW' in result.keys(): - print(result['SecurityAW']) self.cookies.update({'SecurityAW': result['SecurityAW']}) if 'csfrToken' in result.keys(): - print(result['csfrToken']) self.headers.update({'csrf-token': result['csfrToken']}) break else: From 1d4d8eb57cacb5cb14ea7607eb803ef80d653d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=B3nos?= Date: Tue, 19 Jul 2022 20:07:50 +0200 Subject: [PATCH 4/4] Aggiunto unittest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit file test.py per testare le funzionalità della libreria TODO: da apliare --- .gitignore | 3 ++- animeworld/utility.py | 4 ++-- test.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 test.py diff --git a/.gitignore b/.gitignore index 3740b04..efd4571 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ animeworld.egg-info/ build/ tests/ *.pyc -.idea \ No newline at end of file +.idea +.vscode \ No newline at end of file diff --git a/animeworld/utility.py b/animeworld/utility.py index b9bea67..9d1dc28 100644 --- a/animeworld/utility.py +++ b/animeworld/utility.py @@ -39,8 +39,8 @@ def fixCookie(self): } } """ - - result = r.html.render(script=script) + # TODO: controllare il timeout + result = r.html.render(script=script, timeout=100) if 'SecurityAW' in result.keys(): self.cookies.update({'SecurityAW': result['SecurityAW']}) 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