This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/pynyaata2/bridge/yggtorrent.py

92 lines
2.8 KiB
Python
Raw Permalink Normal View History

2023-01-05 23:19:32 +00:00
from os import getenv
2022-12-29 23:01:41 +00:00
from typing import List
2023-01-04 21:46:13 +00:00
from urllib import parse
2022-12-29 23:01:41 +00:00
from bs4 import BeautifulSoup
from pydantic import HttpUrl, parse_obj_as
2023-06-04 15:53:29 +00:00
from pynyaata2.cache import cache_data
from pynyaata2.filters import filter_data
from pynyaata2.session import requests
from pynyaata2.types import Bridge, Color, RemoteFile, async_wrap
2022-12-29 23:01:41 +00:00
2023-01-24 14:28:29 +00:00
from requests import HTTPError
2023-01-05 23:19:32 +00:00
YGG_IP = getenv("YGG_IP")
2023-06-04 15:53:29 +00:00
YGG_DOMAIN = "www3.yggtorrent.do"
2022-12-29 23:01:41 +00:00
class YggTorrent(Bridge):
color = Color.SUCCESS
title = "YggTorrent"
2023-01-05 23:19:32 +00:00
base_url = parse_obj_as(HttpUrl, f"https://{YGG_DOMAIN}")
2023-01-05 23:45:34 +00:00
hidden_url = parse_obj_as(HttpUrl, f"http://{YGG_IP}") if YGG_IP else base_url
2023-01-04 21:58:09 +00:00
favicon = parse_obj_as(HttpUrl, f"{base_url}/favicon.ico")
2022-12-29 23:01:41 +00:00
category = "Animation Série"
sub_category = 2179
def search_url(self, query: str = "", page: int = 1) -> HttpUrl:
2023-01-04 21:46:13 +00:00
params = parse.urlencode(
{
"name": query,
"category": 2145,
"sub_category": self.sub_category,
"do": "search",
"order": "desc",
"sort": "size" if query else "publish_date",
}
)
2022-12-29 23:01:41 +00:00
2023-01-05 23:45:34 +00:00
return parse_obj_as(HttpUrl, f"{self.base_url}/engine/search?{params}")
2022-12-29 23:01:41 +00:00
2023-01-13 22:11:55 +00:00
@async_wrap
2022-12-29 23:01:41 +00:00
@cache_data
@filter_data
2023-01-13 22:11:55 +00:00
def search(self, query: str = "", page: int = 1) -> List[RemoteFile]:
2023-01-05 23:45:34 +00:00
search_url = self.search_url(query, page)
response = requests.get(
f"{self.hidden_url}/{search_url.path}?{search_url.query}",
headers={"Host": YGG_DOMAIN},
)
2022-12-29 23:01:41 +00:00
if response.status_code != 200:
2023-01-24 14:28:29 +00:00
raise HTTPError(response)
2022-12-29 23:01:41 +00:00
torrents: List[RemoteFile] = []
html = BeautifulSoup(response.content, "html.parser")
trs = html.select("table.table tr")
for i, tr in enumerate(trs):
if not i:
continue
tds = tr.find_all("td")
torrents.append(
RemoteFile(
bridge=self.__class__.__name__,
id=tds[0].div.get_text(),
category=self.category,
name=tds[1].get_text(),
link=tds[1].a["href"],
comment=tds[3].get_text(),
comment_url=f"{tds[1].a['href']}#comm",
torrent=f"{self.base_url}/engine/download_torrent?id={tds[0].div.get_text()}",
2023-01-06 21:01:11 +00:00
size=tds[5].get_text(),
2022-12-29 23:01:41 +00:00
date=tds[4].div.get_text(),
seeds=tds[7].get_text(),
leechs=tds[8].get_text(),
downloads=tds[6].get_text(),
nb_pages=html.select("ul.pagination li")[-2].get_text(),
)
)
return torrents
class YggAnimation(YggTorrent):
title = "YggAnimation"
category = "Animation Série"
sub_category = 2179