#!/usr/bin/env python3 import argparse import os import re import subprocess import sys import time import requests parser = argparse.ArgumentParser() parser.add_argument("-r", "--remote", type=str, required=True) parser.add_argument("-f", "--force", action=argparse.BooleanOptionalAction) args = parser.parse_args() remote = args.remote force = args.force def drive_list(path): proc = subprocess.run( ["rclone", "lsf", f"{remote}:{path}"], check=True, capture_output=True, ) return proc.stdout.decode().split("\n") def download(path, file): if not force and os.path.isfile(os.path.join(path, file)): return try: print(f"Download {path}/{file}") subprocess.run( ["rclone", "copy", "-P", "-v", f"{remote}:{path}/{file}", f"{path}/"], check=True, stdout=sys.stdout, ) except subprocess.CalledProcessError: print("Waiting for 10 minutes ...") time.sleep(10 * 60) download(path, file) def remove(path, file): if os.path.isfile(os.path.join(path, file)): print(f"Removing {path}/{file}") os.remove(os.path.join(path, file)) req_titles = requests.get( "https://raw.githubusercontent.com/blawar/titledb/master/FR.fr.json" ) json_titles = req_titles.json() titles = [] for json_title in json_titles: titles.append(json_titles[json_title]["id"]) print("Titles OK") bases = drive_list("nsz/base") print("Base OK") dlcs = drive_list("nsz/dlc") print("DLC OK") updates = drive_list("nsz/updates") print("Updates OK") def get_title_fullid(name): re_id = re.search(r"\[([0-9A-F]*)\]", name) return re_id.group(1) if re_id else None def get_title_shortid(name): id = get_title_fullid(name) return id[0:-4] if id in titles else None try: subprocess.run( ["rclone", "sync", "-P", "-v", f"{remote}:nsz/homebrew", "nsz/homebrew/"], check=True, stdout=sys.stdout, ) except subprocess.CalledProcessError: pass for base in bases: shortid = get_title_shortid(base) if shortid: download("nsz/base", base) for dlc in dlcs: if shortid in dlc: download("nsz/dlc", dlc) for update in updates: if shortid in update: download("nsz/updates", update) else: remove("nsz/base", base) fullid = get_title_fullid(base) for dlc in dlcs: if fullid in dlc: remove("nsz/dlc", dlc) for update in updates: if fullid in update: remove("nsz/updates", update)