dl/commands/tinfoil.py
Michel Roux 47e985e642
All checks were successful
dl / lint (push) Successful in 2m52s
dl / docker (push) Successful in 4m27s
Update dep, fix tinfoil
2023-10-27 11:03:33 +02:00

130 lines
2.9 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("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))
def clean(path, database):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)) and file not in database:
remove(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 and id in titles else None
clean("nsz/base", bases)
clean("nsz/dlc", dlcs)
clean("nsz/updates", 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:
ascii = base[0:1].isascii()
shortid = get_title_shortid(base)
if shortid and ascii:
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)
shortid = fullid[0:-4]
for dlc in dlcs:
if shortid in dlc:
remove("nsz/dlc", dlc)
for update in updates:
if shortid in update:
remove("nsz/updates", update)