init (pre-existing): added template oop implementation

This commit is contained in:
Emile Clark-Boman 2025-07-06 21:29:08 +10:00
commit d51238838c
13 changed files with 178 additions and 0 deletions

0
src/core/__init__.py Normal file
View file

3
src/core/mirror.py Normal file
View file

@ -0,0 +1,3 @@
class Mirror:
def __init__(self, url: str) -> None:
self.url = url

11
src/core/provider.py Normal file
View file

@ -0,0 +1,11 @@
from core.mirror import Mirror
'''
A provider can have multiple mirrors
'''
class Provider:
name = 'ProviderTemplate'
def __init__(self) -> None:
pass
def GetMirrors(self) -> list[str]:
raise NotImplementedError(f'method GetMirrors not implemented for {self.name} inheriting from Provider')

View file

45
src/providers/hianime.py Normal file
View file

@ -0,0 +1,45 @@
# Dependencies
import re
import requests
from urllib.parse import quote_plus as urlencode
# Assets
from core.provider import Provider
search_expr = '<a href="([^"]+)" title="[^"]+" class="dynamic-name" data-jname="[^"]+"'
search_expr2 = ('<div class="flw-item">\s*'
'<div class="film-poster">\s*')
#'<div class="film-detail">.+</div>\s*'
#'<div class="clearfix">.+\s*'
#'</div>\s*'
#'</div>')
class hianime(Provider):
_NAME = 'HiAnime'
_BOOTSTRAP = 'https://hianime.tv'
def __init__(self) -> None:
pass
def GetMirrors(self) -> list[str]:
content = requests.get('https://hianime.tv').text
expr = '<a href="([a-z\./:]+)" title="">'
# for some reason hianime doesn't advertise `hianime.to` as a mirror
matches = re.findall(expr, content)
if 'https://hianime.to' not in matches:
matches.append('https://hianime.to')
return matches
def SearchAnime(self, mirror: str, query: str) -> list[str]: #-> list[Anime]:
urlsafe_query = urlencode(query)
url = f'{mirror}/search?keyword={urlsafe_query}&sort=default'
content = requests.get(url).text
#print(content)
matches = re.findall(search_expr, content)
return matches

30
src/test.py Normal file
View file

@ -0,0 +1,30 @@
from providers.hianime import hianime
from requests import async
def main():
provider = hianime()
mirrors = provider.GetMirrors()
#print(mirrors)
mirror = mirrors[0]
tasks = []
animes = provider.SearchAnime(mirror, 'blue lock')
print(len(animes))
for result in animes:
print(result)
result = mirror + result
new_task = async.get(result)
tasks.append(new_task)
# wait for tasks
async.map(tasks)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass

12
src/util/speedtest.py Normal file
View file

@ -0,0 +1,12 @@
from core.mirror import Mirror
'''
Speedtest multiple mirrors to see which is faster
My idea is to run some bash thing like
ping hianime.nz -c 1 -q | head -n 1
And then run one of those asyncronyously (in parallel)
for each mirror we're testing
'''
def Speedtest(mirrors: list[Mirror]) -> list[int]:
pass