feat(anime provider): improve browser impersonation by using sessions

This commit is contained in:
Benex254
2024-08-05 09:47:06 +03:00
parent c7d0d516fd
commit b32c2545d0
3 changed files with 12 additions and 16 deletions

View File

@@ -2,14 +2,3 @@ from .allanime.api import AllAnimeAPI
from .animepahe.api import AnimePaheApi from .animepahe.api import AnimePaheApi
anime_sources = {"allanime": AllAnimeAPI, "animepahe": AnimePaheApi} anime_sources = {"allanime": AllAnimeAPI, "animepahe": AnimePaheApi}
class Anime_Provider:
def search_for_anime(self):
pass
def get_anime(self):
pass
def get_episode_streams(self):
pass

View File

@@ -2,11 +2,11 @@ import json
import logging import logging
from typing import Iterator from typing import Iterator
import requests
from requests.exceptions import Timeout from requests.exceptions import Timeout
from ....libs.anime_provider.allanime.types import AllAnimeEpisode from ....libs.anime_provider.allanime.types import AllAnimeEpisode
from ....libs.anime_provider.types import Anime, Server from ....libs.anime_provider.types import Anime, Server
from ...anime_provider.base_provider import AnimeProvider
from .constants import ( from .constants import (
ALLANIME_API_ENDPOINT, ALLANIME_API_ENDPOINT,
ALLANIME_BASE, ALLANIME_BASE,
@@ -23,7 +23,7 @@ Logger = logging.getLogger(__name__)
# TODO: create tests for the api # TODO: create tests for the api
# #
# ** Based on ani-cli ** # ** Based on ani-cli **
class AllAnimeAPI: class AllAnimeAPI(AnimeProvider):
""" """
Provides a fast and effective interface to AllAnime site. Provides a fast and effective interface to AllAnime site.
""" """
@@ -32,7 +32,7 @@ class AllAnimeAPI:
def _fetch_gql(self, query: str, variables: dict): def _fetch_gql(self, query: str, variables: dict):
try: try:
response = requests.get( response = self.session.get(
self.api_endpoint, self.api_endpoint,
params={ params={
"variables": json.dumps(variables), "variables": json.dumps(variables),
@@ -76,7 +76,6 @@ class AllAnimeAPI:
"countryorigin": countryorigin, "countryorigin": countryorigin,
} }
try: try:
search_results = self._fetch_gql(ALLANIME_SEARCH_GQL, variables) search_results = self._fetch_gql(ALLANIME_SEARCH_GQL, variables)
return normalize_search_results(search_results) # pyright:ignore return normalize_search_results(search_results) # pyright:ignore
except Exception as e: except Exception as e:
@@ -140,7 +139,7 @@ class AllAnimeAPI:
# get the stream url for an episode of the defined source names # get the stream url for an episode of the defined source names
parsed_url = decode_hex_string(url) parsed_url = decode_hex_string(url)
embed_url = f"https://{ALLANIME_BASE}{parsed_url.replace('clock', 'clock.json')}" embed_url = f"https://{ALLANIME_BASE}{parsed_url.replace('clock', 'clock.json')}"
resp = requests.get( resp = self.session.get(
embed_url, embed_url,
headers={ headers={
"Referer": ALLANIME_REFERER, "Referer": ALLANIME_REFERER,

View File

@@ -0,0 +1,8 @@
import requests
class AnimeProvider:
session: requests.Session
def __init__(self) -> None:
self.session = requests.session()