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/nyaa.py

106 lines
3.3 KiB
Python
Raw Permalink Normal View History

2022-10-26 13:06:04 +00:00
from datetime import datetime
2022-12-22 00:01:21 +00:00
from os import getenv
2022-10-26 13:06:04 +00:00
from typing import List
2023-01-04 21:46:13 +00:00
from urllib import parse
2022-10-26 13:06:04 +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.types import Bridge, Color, RemoteFile, async_wrap
2022-10-26 13:06:04 +00:00
2022-12-21 22:32:42 +00:00
import requests
2022-10-26 13:06:04 +00:00
2022-12-22 00:01:21 +00:00
VF_WORDS = getenv("VF_WORDS", "vf,vostfr,multi,fre").split(",")
2022-10-26 13:06:04 +00:00
class Nyaa(Bridge):
color = Color.INFO
title = "Nyaa"
base_url = parse_obj_as(HttpUrl, "https://nyaa.si")
2023-01-04 21:58:09 +00:00
favicon = parse_obj_as(HttpUrl, f"{base_url}/static/favicon.png")
2022-10-26 13:06:04 +00:00
def search_url(self, query: str = "", page: int = 1) -> HttpUrl:
to_query = "|".join(map(lambda word: f"({query} {word})", VF_WORDS))
2023-01-04 21:46:13 +00:00
params = parse.urlencode(
2022-10-26 13:06:04 +00:00
{
"f": 0,
"c": "1_3",
"q": to_query,
"s": "size" if query else "id",
"o": "desc",
"p": page,
}
)
return parse_obj_as(HttpUrl, f"{self.base_url}?{params}")
2023-01-13 22:11:55 +00:00
@async_wrap
2022-10-26 13:06:04 +00:00
@cache_data
2022-12-22 00:01:21 +00:00
@filter_data
2023-01-13 22:11:55 +00:00
def search(self, query: str = "", page: int = 1) -> List[RemoteFile]:
2022-10-26 13:06:04 +00:00
response = requests.get(self.search_url(query, page))
if response.status_code != 200:
2022-12-21 22:32:42 +00:00
raise requests.HTTPError(response)
2022-10-26 13:06:04 +00:00
torrents: List[RemoteFile] = []
html = BeautifulSoup(response.content, "html.parser")
trs = html.select("table.torrent-list tr")
for i, tr in enumerate(trs):
if not i:
continue
tds = tr.find_all("td")
urls = tds[1].find_all("a")
links = tds[2].find_all("a")
2023-01-06 21:01:11 +00:00
nb_pages = html.select("ul.pagination li")[-2]
current = nb_pages.select_one("span")
if current:
current.decompose()
2022-10-26 13:06:04 +00:00
torrents.append(
RemoteFile(
2022-12-22 00:01:21 +00:00
bridge=self.__class__.__name__,
2022-10-26 13:06:04 +00:00
id=urls[1 if len(urls) > 1 else 0]["href"].split("/")[-1],
category=tds[0].a["title"],
color=Color[tr["class"][0].upper()],
name=urls[1 if len(urls) > 1 else 0].get_text(),
link=f"{self.base_url}{urls[1 if len(urls) > 1 else 0]['href']}",
comment=urls[0].get_text() if len(urls) > 1 else 0,
comment_url=f"{self.base_url}{urls[0]['href']}",
magnet=links[1]["href"],
torrent=f"{self.base_url}{links[0]['href']}",
size=tds[3].get_text(),
date=datetime.fromtimestamp(int(tds[4]["data-timestamp"])),
seeds=tds[5].get_text(),
leechs=tds[6].get_text(),
downloads=tds[7].get_text(),
2023-01-06 21:01:11 +00:00
nb_pages=nb_pages.get_text(),
2022-10-26 13:06:04 +00:00
)
)
return torrents
2022-12-29 23:01:41 +00:00
class EraiRaws(Nyaa):
title = "Erai-raws"
def search_url(self, query: str = "", page: int = 1) -> HttpUrl:
2023-01-04 21:46:13 +00:00
params = parse.urlencode(
2022-12-29 23:01:41 +00:00
{
"f": 0,
"c": "1_2",
"q": f"{query} fre",
"s": "size" if query else "id",
"o": "desc",
"p": page,
}
)
return parse_obj_as(HttpUrl, f"{self.base_url}/user/Erai-raws?{params}")