1
0
Files
anime-rss/anime-rss.py
2021-10-14 22:57:02 +02:00

83 lines
2.6 KiB
Python

import feedparser
import json
import re
import subprocess
import argparse
from notifypy import Notify
feedparser.USER_AGENT = "anime-rss/1.0"
# ANSI escape sequence coloring
CSI = "\x1B["
BOLD = CSI + "1;49m"
RESET = CSI + "0;0m"
RED = CSI + "91;49m"
GREEN = CSI + "92;49m"
BOLD_RED = BOLD + RED
BOLD_GREEN = BOLD + GREEN
UNDERLINE = CSI + "4;4m"
parser = argparse.ArgumentParser()
parser.add_argument("c", help="path to the config.json file", type=str, default="config.json", nargs='?')
parser.add_argument("a", help="path to the animes.json file", type=str, default="animes.json", nargs='?')
args = parser.parse_args()
def build_target_folder():
if config["dirPerAnime"]:
return config["animeDownloadDir"] + "/" + anime["Title"].replace(" ", "_")
else:
return config["animeDownloadDir"]
def send_notification(message):
if config["notifications"]:
notification = Notify()
notification.title = "anime-rss"
notification.message = message
notification.icon = "notfication-icon.png"
notification.send()
with open(args.a, "r") as feeds:
feeds = json.load(feeds)
with open(args.c, "r") as config:
config = json.load(config)
for feed in feeds["Feeds"]:
print(BOLD + "Checking feed: " + feed["ID"] + RESET)
NewsFeed = feedparser.parse(feed["URL"])
for anime in feed["Animes"]:
print(
"Checking anime: " + BOLD_RED + anime["Title"] + RESET + " - Airing every " + UNDERLINE + anime["Airing"] +
RESET + " " + anime["Local"] + "/x")
entries = NewsFeed.entries
if anime["Airing"] == "Done":
entries.reverse()
for entry in entries:
if anime["Title"] in entry.title:
regex = feed["Episode-Regex"].replace("$SHOWS$", anime["Title"])
match = re.match(regex, entry.title)
if match:
episodeNumber = match.groups()
if int(episodeNumber[0]) > int(anime["Local"]):
print(BOLD_GREEN + "NEW NEW NEW " + anime["Title"] + " " + episodeNumber[0] + RESET)
if subprocess.run(["transmission-remote", "-a", entry.link, "-w", build_target_folder()]).returncode == 0:
anime["Local"] = str(int(anime["Local"]) + 1).zfill(2)
send_notification("Episode " + anime["Local"] + " of " + anime["Title"] + " added!")
else:
send_notification("Transmission not running or not reachable!")
with open(args.a, "w") as new_feeds:
new_feeds.write(json.dumps(feeds, indent=3))