dl/commands/tinfoil.py

134 lines
3.0 KiB
Python
Raw Normal View History

2023-09-10 15:38:49 +00:00
#!/usr/bin/env python3
import argparse
2023-09-11 07:02:57 +00:00
import os
2023-09-10 15:38:49 +00:00
import re
import subprocess
import sys
2023-10-01 21:57:58 +00:00
import time
2023-09-10 15:38:49 +00:00
import requests
parser = argparse.ArgumentParser()
2023-09-10 15:44:39 +00:00
parser.add_argument("-r", "--remote", type=str, required=True)
2023-09-12 07:00:29 +00:00
parser.add_argument("-f", "--force", action=argparse.BooleanOptionalAction)
2023-09-10 15:38:49 +00:00
args = parser.parse_args()
remote = args.remote
2023-09-12 07:00:29 +00:00
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")
2023-10-02 08:23:06 +00:00
def download(path, file):
2023-10-01 21:49:43 +00:00
if not force and os.path.isfile(os.path.join(path, file)):
2023-09-12 07:00:29 +00:00
return
2023-10-01 21:49:43 +00:00
try:
2023-10-02 09:21:44 +00:00
print(f"Download {path}/{file}")
2023-10-01 21:49:43 +00:00
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)
2023-10-02 08:23:06 +00:00
download(path, file)
2023-09-12 07:00:29 +00:00
2023-09-10 15:38:49 +00:00
2023-10-02 14:36:22 +00:00
def remove(path, file):
2023-10-03 16:11:40 +00:00
if os.path.isfile(os.path.join(path, file)):
print(f"Removing {path}/{file}")
os.remove(os.path.join(path, file))
2023-10-02 14:36:22 +00:00
2023-10-05 13:21:14 +00:00
def clean(path, database):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)) and file not in database:
2023-10-05 13:36:43 +00:00
remove(path, file)
2023-10-05 13:21:14 +00:00
2023-09-10 15:38:49 +00:00
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")
2023-09-12 07:00:29 +00:00
bases = drive_list("nsz/base")
2023-09-11 07:02:57 +00:00
print("Base OK")
2023-09-10 15:38:49 +00:00
2023-09-12 07:00:29 +00:00
dlcs = drive_list("nsz/dlc")
2023-09-10 15:38:49 +00:00
print("DLC OK")
2023-09-12 07:00:29 +00:00
updates = drive_list("nsz/updates")
2023-09-10 15:38:49 +00:00
print("Updates OK")
2023-09-11 07:02:57 +00:00
2023-10-05 14:06:32 +00:00
def get_title_fullid(name):
2023-10-03 16:11:40 +00:00
re_id = re.search(r"\[([0-9A-F]*)\]", name)
2023-10-05 14:06:32 +00:00
return re_id.group(1) if re_id else None
2023-10-02 07:39:57 +00:00
2023-10-05 14:06:32 +00:00
def get_title_shortid(name):
id = get_title_fullid(name)
2023-09-10 15:38:49 +00:00
2023-10-27 09:03:33 +00:00
return id[0:-4] if id and id in titles else None
2023-10-02 07:39:57 +00:00
2023-10-05 13:21:14 +00:00
clean("nsz/base", bases)
clean("nsz/dlc", dlcs)
clean("nsz/updates", updates)
2023-10-02 07:39:57 +00:00
try:
subprocess.run(
2023-10-02 08:45:12 +00:00
["rclone", "sync", "-P", "-v", f"{remote}:nsz/homebrew", "nsz/homebrew/"],
2023-10-02 07:39:57 +00:00
check=True,
stdout=sys.stdout,
)
except subprocess.CalledProcessError:
pass
2023-10-03 16:11:40 +00:00
2023-10-02 07:39:57 +00:00
for base in bases:
2023-10-27 09:03:33 +00:00
ascii = base[0:1].isascii()
2023-10-05 14:06:32 +00:00
shortid = get_title_shortid(base)
2023-10-02 07:39:57 +00:00
2023-10-27 09:03:33 +00:00
if shortid and ascii:
2023-10-03 16:11:40 +00:00
download("nsz/base", base)
2023-10-03 16:52:59 +00:00
for dlc in dlcs:
2023-10-05 14:06:32 +00:00
if shortid in dlc:
2023-10-03 16:52:59 +00:00
download("nsz/dlc", dlc)
for update in updates:
2023-10-05 14:06:32 +00:00
if shortid in update:
2023-10-03 16:52:59 +00:00
download("nsz/updates", update)
2023-10-03 16:11:40 +00:00
else:
remove("nsz/base", base)
2023-09-10 15:38:49 +00:00
2023-10-05 14:06:32 +00:00
fullid = get_title_fullid(base)
2023-10-27 09:29:09 +00:00
if not fullid:
continue
shortid = fullid[0:-4]
2023-10-05 14:06:32 +00:00
2023-10-03 16:52:59 +00:00
for dlc in dlcs:
2023-10-05 14:07:32 +00:00
if shortid in dlc:
2023-10-03 16:52:59 +00:00
remove("nsz/dlc", dlc)
for update in updates:
2023-10-05 14:07:32 +00:00
if shortid in update:
2023-10-03 16:52:59 +00:00
remove("nsz/updates", update)