Skip to content

Commit

Permalink
Ottimizzazione
Browse files Browse the repository at this point in the history
- Rimosso file globals
- Aggiunto una corretta gestione di Sessione di Requests
- Ottimizzazione codice
  • Loading branch information
MainKronos committed Apr 14, 2022
1 parent 82577d4 commit 6752bbd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 55 deletions.
23 changes: 2 additions & 21 deletions animeworld/anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import time
from typing import *

from .globals import HDR, cookies
from .utility import HealthCheck
from .utility import HealthCheck, SES
from .exceptions import Error404, AnimeNotAvailable
from .episodio import Episodio

Expand All @@ -34,23 +33,9 @@ def __init__(self, link: str):
- `link`: Link dell'anime.
"""
self.link = link
self.__fixCookie()
self.html = self.__getHTML().content
self.__check404()

# Private
def __fixCookie(self):
"""
Aggiorna il cookie `AWCookieVerify`.
"""
try:
soupeddata = BeautifulSoup(self.__getHTML().content, "html.parser")

cookies['AWCookieVerify'] = re.search(r'document\.cookie="AWCookieVerify=(.+) ;', soupeddata.prettify()).group(1)

except AttributeError:
pass

# Private
def __getHTML(self) -> requests.Response:
"""
Expand All @@ -63,9 +48,7 @@ def __getHTML(self) -> requests.Response:
r = None
while True:
try:
r = requests.get(self.link, headers = HDR, cookies=cookies, timeout=(3, 27), allow_redirects=True)

cookies.update(r.cookies.get_dict())
r = SES.get(self.link, timeout=(3, 27), allow_redirects=True)

if len(list(filter(re.compile(r'30[^2]').search, [str(x.status_code) for x in r.history]))): # se c'è un redirect strano
continue
Expand Down Expand Up @@ -195,8 +178,6 @@ def getEpisodes(self) -> List[Episodio]: # Ritorna una lista di Episodi

soupeddata = BeautifulSoup(self.__getHTML().content, "html.parser")

HDR.update({"csrf-token": soupeddata.find('meta', {'id': 'csrf-token'}).get('content')})

raw = {} # dati in formato semi-grezzo
eps = [] # Lista di Episodio()

Expand Down
6 changes: 3 additions & 3 deletions animeworld/episodio.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
Modulo contenente la struttura a classe degli episodi.
"""
import requests
# import requests
from bs4 import BeautifulSoup
from typing import *

from .globals import HDR, cookies
from .utility import SES
from .server import Server, AnimeWorld_Server, VVVVID, YouTube, Streamtape


Expand Down Expand Up @@ -48,7 +48,7 @@ def links(self) -> List[Server]: # lista dei provider dove sono hostati gli ep
"""
tmp = [] # tutti i links

res = requests.post(self.__link, headers = HDR, cookies=cookies, timeout=(3, 27))
res = SES.post(self.__link, timeout=(3, 27))
data = res.json()

for provID in data["links"]:
Expand Down
4 changes: 2 additions & 2 deletions animeworld/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def __init__(self, link):

class DeprecatedLibrary(Exception):
"""Libreria deprecata a causa di un cambiamento della struttura del sito."""
def __init__(self, funName, line):
def __init__(self, file, funName, line):
self.funName = funName
self.line = line
self.message = f"Il sito è cambiato, di conseguenza la libreria è DEPRECATA. -> [{funName} - {line}]"
self.message = f"Il sito è cambiato, di conseguenza la libreria è DEPRECATA. -> [File {file} in {funName} - {line}]"
super().__init__(self.message)
7 changes: 0 additions & 7 deletions animeworld/globals.py

This file was deleted.

18 changes: 8 additions & 10 deletions animeworld/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"""
Modulo contenente le strutture a classi di tutti i server che hostano gli episodi di animewolrd.
"""
import requests
from bs4 import BeautifulSoup
import youtube_dl
import re
import os
from typing import *
import time

from .globals import HDR, cookies
from .utility import HealthCheck
from .utility import HealthCheck, SES
from .exceptions import ServerNotSupported


Expand Down Expand Up @@ -45,7 +43,7 @@ def __init__(self, link: str, Nid: int, name: str, number: str):
self.number = number
"""Numero dell'episodio."""

self._HDR = HDR # Protected
# self._HDR = HDR # Protected
self._defTitle = f"{self.number} - {self.name}" # nome del file provvisorio

def _sanitize(self, title: str) -> str: # Toglie i caratteri illegali per i file
Expand Down Expand Up @@ -106,7 +104,7 @@ def _downloadIn(self, title: str, folder: str, hook: Callable[[Dict], None]) ->
return bool # File scaricato
```
"""
with requests.get(self._getFileLink(), headers = self._HDR, stream = True) as r:
with SES.get(self._getFileLink(), stream = True) as r:
r.raise_for_status()
ext = r.headers['content-type'].split('/')[-1]
if ext == 'octet-stream': ext = 'mp4'
Expand Down Expand Up @@ -249,10 +247,10 @@ def _getFileLink(self):
external_link = "https://www.animeworld.tv/api/episode/serverPlayerAnimeWorld?id={}".format(anime_id)
"https://www.animeworld.tv/api/episode/serverPlayerAnimeWorld?id=vKmnNB"

sb_get = requests.get(self.link, headers = self._HDR, cookies=cookies)
sb_get = SES.get(self.link)
sb_get.raise_for_status()

sb_get = requests.get(external_link, headers = self._HDR, cookies=cookies)
sb_get = SES.get(external_link)
soupeddata = BeautifulSoup(sb_get.content, "html.parser")
sb_get.raise_for_status()

Expand Down Expand Up @@ -291,10 +289,10 @@ def _getFileLink(self):
anime_id = self.link.split("/")[-1]
external_link = "https://www.animeworld.tv/api/episode/serverPlayerAnimeWorld?id={}".format(anime_id)

sb_get = requests.get(self.link, headers = self._HDR, cookies=cookies)
sb_get = SES.get(self.link)
sb_get.raise_for_status()

sb_get = requests.get(external_link, headers = self._HDR, cookies=cookies)
sb_get = SES.get(external_link)
soupeddata = BeautifulSoup(sb_get.content, "html.parser")
sb_get.raise_for_status()

Expand Down Expand Up @@ -333,7 +331,7 @@ class Streamtape(Server):
@HealthCheck
def _getFileLink(self):

sb_get = requests.get(self.link, headers = self._HDR, cookies=cookies, allow_redirects=False)
sb_get = SES.get(self.link, allow_redirects=False)

with open('inde.html', 'wb') as f:
f.write(sb_get.content)
Expand Down
60 changes: 48 additions & 12 deletions animeworld/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,67 @@
from bs4 import BeautifulSoup
import inspect
from typing import *
import re

from datetime import datetime
import time
import locale

from .globals import HDR, cookies
from .exceptions import DeprecatedLibrary

class MySession(requests.Session):
"""
Sessione requests.
"""
def __init__(self) -> None:
super().__init__()
self.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'})
self.fixCookie()

def fixCookie(self):
AWCookieVerify = re.compile(br'document\.cookie="AWCookieVerify=(.+) ;')
csrf_token = re.compile(br'<meta.*?id="csrf-token"\s*?content="(.*?)">')

for _ in range(1): # 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')})
break
else:
frame = inspect.getframeinfo(inspect.currentframe())
raise DeprecatedLibrary(frame.filename, frame.function, frame.lineno)


def HealthCheck(fun):
"""
Controlla se la libreria è deprecata
"""
def wrapper(*args, **kwargs):
try:
return fun(*args, **kwargs)
attempt = False # tentativo utilizzato
while True:
try:
return fun(*args, **kwargs)
except Exception as e:
if not attempt:
SES.fixCookie()
else:
raise e
attempt = True

except AttributeError:
frame = inspect.trace()[-1]
funName = frame[3]
errLine = frame[2]
raise DeprecatedLibrary(funName, errLine)
filename = frame[1]
raise DeprecatedLibrary(filename, funName, errLine)
return wrapper

@HealthCheck
Expand Down Expand Up @@ -70,14 +110,7 @@ def find(keyword: str) -> List[Dict]:
"""

locale.setlocale(locale.LC_TIME, "it_IT.UTF-8")

res = requests.get("https://www.animeworld.tv", headers = HDR)
cookies.update(res.cookies.get_dict())
soupeddata = BeautifulSoup(res.content, "html.parser")
myHDR = {"csrf-token": soupeddata.find('meta', {'id': 'csrf-token'}).get('content')}


res = requests.post("https://www.animeworld.tv/api/search/v2?", params = {"keyword": keyword} ,headers=myHDR, cookies=cookies)
res = SES.post("https://www.animeworld.tv/api/search/v2?", params = {"keyword": keyword})

data = res.json()
if "error" in data: return []
Expand Down Expand Up @@ -113,4 +146,7 @@ def find(keyword: str) -> List[Dict]:
"malVote": elem["malVote"],
"trailer": elem["trailer"]
}for elem in data
]
]

SES = MySession() # sessione contenente Cookie e headers
"Sessione requests."

0 comments on commit 6752bbd

Please sign in to comment.