Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import os
- import re
- import ssl
- import time
- import traceback
- import urllib.request
- PLEX_TOKEN = "<insert token here>"
- BASIC_RE = r"(?:\[(?P<group>.+?)\] ?)?(?P<name>.+?)(?: -)? (?P<number>\d+)(?: ?v\d)?.*?(?: [\[()](?P<quality>(?:\d+p|.+?))[\])])?(?:\s*[\[()](?P<hash>[A-Fa-f0-9]+)[\])])?\s*\.(?P<ext>(?:mkv|ass|srt))"
- POKEMON_RE = r"\[(?P<group>.+?)\]_?(?P<name>.+?)(?:_-)?_(?P<number>\d+)(?:_?\[?v\d\]?)?(?:_?[\[()](?P<quality>(?:\d+p|.+?))[\])])?(?:\s*|_[\[()](?P<hash>[A-Fa-f0-9]+)[\])])?\.(?P<ext>(?:mkv|ass|srt))"
- REGEX_LIST = [BASIC_RE, POKEMON_RE]
- FILE_EXTS = [".mkv", ".ass", ".srt"]
- MONITOR_DIR = "/path/to/downloaded/files/"
- MINIMUM_UNMODIFIED_TIME = 120 # seconds
- DEFAULTS = {
- "season": 1,
- "offset": 0,
- "show_dir": "",
- "base_path": "/parent/path/to/save/to/"
- }
- SEASON_MAP = {
- # Autumn 2018
- "Uchuu Senkan Tiramisu S2": {"show_dir": "Uchuu Senkan Tiramisu", "season": 2},
- "Thunderbolt Fantasy S2": {"show_dir": "Thunderbolt Fantasy", "season": 2},
- "Fairy Tail Final Season": {"show_dir": "Fairy Tail (2018)", "offset": 277},
- "Ace Attorney S2": {"show_dir": "Ace Attorney", "season": 2},
- "Inazuma_Eleven_Orion_no_Kokuin": {"show_dir": "Inazuma Eleven - Orion no Kokuin"},
- "Jojo's_Bizarre_Adventure_Golden_Wind": {"show_dir": "JoJo's Bizarre Adventure - Golden Wind"},
- "SSSS.GRIDMAN": {"show_dir": "SSSS.Gridman"},
- "To Aru Majutsu no Index III": {"show_dir": "Toaru Majutsu no Index III"},
- "Zombie Land Saga": {"show_dir": "Zombieland Saga"},
- "Sword Art Onlineː Alicization": {"show_dir": "Sword Art Online - Alicization"},
- # Winter 2019
- "BanG Dream! S2": {"show_dir": "BanG Dream!", "season": 2},
- "Mob Psycho": {"show_dir": "Mob Psycho 100", "season": 2},
- "Dororo": {"show_dir": "Dororo (2019)"},
- "Kakegurui xx": {"show_dir": "Kakegurui", "season": 2},
- "B-Project S2": {"show_dir": "B-Project", "season": 2},
- "Date A Live S3": {"show_dir": "Date A Live", "season": 3},
- "Fukigen na Mononokean S2": {"show_dir": "Fukigen na Mononokean", "season": 2},
- "Kaguya-sama ~Love is War~": {"show_dir": "Kaguya-sama wa Kokurasetai"},
- "Date A Live III": {"show_dir": "Date A Live", "season": 3},
- "Kemono Friends S2": {"show_dir": "Kemono Friends", "season": 2},
- "Star☆Twinkle Precure": {"show_dir": "Star Twinkle Precure"},
- # Spring 2019
- "One Punch Man S2": {"show_dir": "One Punch Man", "season": 2},
- "Hora, Mimi ga Mieteru yo!": {"show_dir": "Hora, Mimi ga Mieteru yo!", "season": 2},
- "Midara na Ao-chan wa Benkyou ga Dekinai - Ao's Too Horny to Study": {"show_dir": "Midara na Ao-chan wa Benkyou ga Dekinai"},
- }
- BLACKLIST = [
- "One Piece",
- ]
- def make_link(source_path):
- filename = os.path.basename(source_path)
- dst_file = filename
- for regex in REGEX_LIST:
- parse_result = re.match(regex, filename)
- if parse_result:
- break
- else:
- print("\"{}\" does not match expected name format!".format(filename))
- return False
- parsed_values = parse_result.groupdict()
- show_name = parsed_values["name"]
- settings = DEFAULTS.copy()
- settings["show_dir"] = show_name
- if show_name in SEASON_MAP:
- settings.update(SEASON_MAP[show_name])
- if settings["offset"] != 0:
- episode_number = int(parsed_values["number"]) - settings["offset"]
- dst_file = dst_file.replace(" - {} ".format(parsed_values["number"]),
- " - S%02dE%02d " % (settings["season"], episode_number))
- season_path = os.path.join(settings["base_path"], settings["show_dir"], "Season %02d" % settings["season"])
- if not os.path.exists(season_path):
- os.makedirs(season_path)
- print('Made directory "{}"'.format(season_path))
- dst_path = os.path.join(season_path, dst_file)
- if not os.path.isfile(dst_path):
- os.link(source_path, dst_path)
- print('Copied "{}" to "{}"'.format(filename, dst_path))
- return True
- return False
- def scan_for_files(dir_path):
- any_files_moved = False
- for filename in os.listdir(dir_path):
- file_ext = os.path.splitext(filename)[-1]
- if file_ext not in FILE_EXTS or any([show in filename for show in BLACKLIST]):
- continue
- try:
- file_path = os.path.join(dir_path, filename)
- if time.time() - os.path.getmtime(file_path) < MINIMUM_UNMODIFIED_TIME:
- continue
- os.chmod(file_path, 0o644)
- file_moved = make_link(file_path)
- if file_moved:
- any_files_moved = True
- except Exception as e:
- traceback.print_exc()
- return any_files_moved
- def update_plex():
- plex_request = urllib.request.Request(
- "https://<url-for-plex-server>/library/sections/<anime-library-number>/refresh",
- headers={"X-Plex-Token": PLEX_TOKEN},
- method="GET")
- urllib.request.urlopen(plex_request)
- if __name__ == "__main__":
- files_moved = scan_for_files(MONITOR_DIR)
- if files_moved:
- print("Updating Plex...")
- update_plex()
Advertisement
Add Comment
Please, Sign In to add comment