dl/commands/tinfoil.py
Michel Roux efec5add4c
All checks were successful
dl / lint (push) Successful in 1m33s
dl / docker (push) Successful in 50s
verbose + fix remove
2023-10-02 11:21:44 +02:00

126 lines
2.7 KiB
Python
Executable File

#!/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(f"Removing {path}/{file}")
os.remove(f"{path}/{file}")
print("Waiting for 10 minutes ...")
time.sleep(10 * 60)
download(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_shortid(name):
re_id = re.search(r"\[([0-9A-F]*)\]", name)
if not re_id:
return
id = re_id.group(1)
return id[0:-4] if id in titles else None
def remove(path):
to_remove = []
for name in os.listdir(path):
if os.path.isfile(name):
id = get_title_shortid(name)
if not id:
to_remove.append(f"{path}/{name}")
if len(to_remove) > 100:
print("Too much to delete:")
for be_removed in to_remove:
print(be_removed)
else:
for be_removed in to_remove:
print(f"Removing {be_removed}")
os.remove(be_removed)
remove("nsz/base")
remove("nsz/dlc")
remove("nsz/updates")
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:
id = get_title_shortid(base)
if not id:
continue
download("nsz/base", base)
for dlc in dlcs:
if id in dlc:
download("nsz/dlc", dlc)
for update in updates:
if id in update:
download("nsz/updates", update)