Spaces:
Sleeping
Sleeping
File size: 1,839 Bytes
0aee47a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
"""
ivitools.scan
扫描 ivi 文件相关
"""
import os
import json
import time
import zipfile
import tempfile
from tqdm import tqdm
from colorama import Fore, Cursor
from colorama.ansi import clear_line
from .touch import touch_ivi
def scan_ivi_file(path: str):
print(Fore.RESET + f"Scanning {path} ...")
# First, make a temp folder.
tmp_dir = tempfile.gettempdir()
if not os.path.exists(os.path.join(tmp_dir, "ivitools")):
os.mkdir(os.path.join(tmp_dir, "ivitools"))
for root, dirs, files in os.walk(os.path.join(tmp_dir, "ivitools"), topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
# Then, extract the ivi file.
extract_dir = os.path.join(tmp_dir, "ivitools", str(time.time()))
# Use the time to make folders different
zipfile.ZipFile(open(path, "rb")).extractall(extract_dir)
# Finally, display the result.
Cursor.UP()
clear_line()
print(path)
meta = touch_ivi(path)
print(f'{meta["title"]}({meta["bvid"]})')
graph = json.load(open(os.path.join(extract_dir, "ivideo.json"), encoding="utf-8"))
print(f"There're {len(graph.keys())} nodes in the file! ")
bar = tqdm(graph.keys())
for item in bar:
bar.set_description("Scanning node ")
if not ("cid" in graph[str(item)].keys()):
raise Exception(f"Missing CID in the node {item}")
return
else:
cid = graph[str(item)]["cid"]
if not (os.path.exists(os.path.join(extract_dir, str(cid) + ".mp4"))):
raise Exception(f"Missing video source of the node {item}")
time.sleep(0.01)
print(Fore.GREEN + "Congratulation! Your file is OK. ", Fore.RESET)
|