nyaa-jelly-tools/nyaa-tools-old/helper.py
2026-09-13 14:22:34 -04:00

124 lines
3.7 KiB
Python
Executable file

#!/usr/bin/env python3
import requests
import toml
import xml.dom.minidom
from dataclasses import dataclass
import re
import inquirer
release_groups = toml.load("/etc/nyaa-tools/release-groups.toml")
release_group_names = release_groups.keys()
query = inquirer.text("Name of the anime")
nyaa_search = requests.get("https://nyaa.si/?page=rss&q={}".format(query)).text
items = xml.dom.minidom.parseString(nyaa_search).getElementsByTagName("item")
@dataclass
class Episode:
title: str
link: str
release_group: str
def episode_info(ep: Episode, season: int, offset: int) -> str:
return (
"Title: {}\n".format(ep.title)
+ "Season: {}\n".format(season)
+ "Chapter offset: {}\n".format(offset)
+ "Release Group: {}".format(ep.release_group)
)
matches: list[Episode] = []
for item in items:
title: str = item.getElementsByTagName("title")[0].firstChild.data
link: str = item.getElementsByTagName("link")[0].firstChild.data
regex = re.search("\\[([^ ]+)\\].+", title)
if regex is not None:
matches.append(Episode(title, link, regex.group(1)))
if not matches:
print("No matching entries")
exit(255)
known_matches = filter(lambda ep: ep.release_group in release_group_names, matches)
parsed_matches: list[Episode] = []
for match in known_matches:
rg_regex = re.search(release_groups[match.release_group], match.title)
if rg_regex is not None:
title = rg_regex.group(1)
parsed_matches.append(Episode(title, match.link, match.release_group))
if not parsed_matches:
print("No known release groups were found, however the following were identified:")
for match in set(map(lambda m: m.release_group, matches)):
print("-", match)
print(
"Please add one of them to the release-groups.toml or wait for a release from a known group"
)
exit(255)
choices = set(map(lambda m: m.release_group, parsed_matches))
if len(choices) > 1:
chosen_rg = inquirer.list_input(
"Choose a release group",
choices=sorted(set(map(lambda m: m.release_group, parsed_matches))),
)
else:
chosen_rg = choices.pop()
final_match = next(m for m in parsed_matches if m.release_group == chosen_rg)
season = 1
offset = 0
while True:
print(episode_info(final_match, season, offset))
ans = inquirer.list_input(
"The following entry will be added",
choices=["Confirm", "Edit", "Cancel"],
)
if ans == "Confirm":
break
elif ans == "Cancel":
exit(0)
while True:
print(episode_info(final_match, season, offset))
ans = inquirer.list_input(
"What do you want to edit", choices=["Title", "Season", "Offset", "Done"]
)
if ans == "Done":
break
if ans == "Title":
final_match.title = inquirer.text("New title", default=final_match.title)
elif ans == "Season":
season = int(inquirer.text(
"Season number", validate=lambda _, x: re.match("\\d+", x)
))
elif ans == "Offset":
offset = int(inquirer.text(
"Chapter offset", validate=lambda _, x: re.match("\\d+", x)
))
series = toml.load("/etc/nyaa-tools/series.toml")
if "tracking" not in series.keys():
series["tracking"] = []
entry = {
"name": final_match.title,
"season": season,
"release_group": final_match.release_group,
}
if offset != 0:
entry["offset"] = offset
series["tracking"].append(entry)
with open("/etc/nyaa-tools/series.toml", "w") as f:
toml.dump(series, f)
print(
"Done! Please run `nyaa-fetch '[{}] {}'` to download previous episodes".format(
final_match.release_group, final_match.title
)
)