dl/commands/tinfoil.py
Michel Roux fadb481c0f
All checks were successful
dl / lint (push) Successful in 1m39s
dl / docker (push) Successful in 54s
rewrite removing
2023-10-02 16:36:22 +02:00

125 lines
2.8 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):
print(f"Removing {path}/{file}")
# os.remove(os.path.join(path, file))
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 extract_shortid(full_id):
return full_id[0:-4]
def get_title_shortid(name):
full_id = get_title_fullid(name)
return extract_shortid(full_id) if full_id in titles else None
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")
for base in os.listdir("nsz/base"):
if os.path.isfile(os.path.join("nsz/base", base)):
full_id = get_title_fullid(base)
short_id = get_title_shortid(base)
if not short_id:
remove("nsz/base", base)
for dlc in os.listdir("nsz/dlc"):
if extract_shortid(full_id) in dlc:
remove("nsz/dlc", dlc)
for update in os.listdir("nsz/updates"):
if extract_shortid(full_id) in update:
remove("nsz/updates", update)
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)