80 lines
3.0 KiB
Python
80 lines
3.0 KiB
Python
import os
|
|
import shutil
|
|
import argparse
|
|
import xml.etree.ElementTree as ET
|
|
from urllib.parse import urlparse, unquote
|
|
|
|
def localname(tag: str) -> str:
|
|
return tag.split('}', 1)[-1].lower()
|
|
|
|
def extract_filename(u: str) -> str:
|
|
s = (u or "").strip()
|
|
if not s:
|
|
return ""
|
|
p = urlparse(s)
|
|
path = p.path if p.scheme else s
|
|
path = path.replace("\\", "/")
|
|
name = path.split("/")[-1]
|
|
return unquote(name)
|
|
|
|
def replace_urls_in_vmix(xml_path: str, prefix_url: str) -> int:
|
|
if not os.path.isfile(xml_path):
|
|
raise FileNotFoundError(f"Файл не найден: {xml_path}")
|
|
|
|
backup_path = xml_path + ".bak"
|
|
shutil.copy2(xml_path, backup_path)
|
|
|
|
tree = ET.parse(xml_path)
|
|
root = tree.getroot()
|
|
|
|
replaced = 0
|
|
for node in root.iter():
|
|
if localname(node.tag).startswith("datasource") and node.get("friendlyName") == "JSON":
|
|
for sub in node.iter():
|
|
if localname(sub.tag) == "url":
|
|
old = (sub.text or "").strip()
|
|
# ✅ проверка: если уже новый URL — пропускаем
|
|
if old.startswith(prefix_url):
|
|
print(f"Пропускаем — уже новый URL: {old}")
|
|
continue
|
|
tail = extract_filename(old)
|
|
if not tail:
|
|
continue
|
|
new_full = prefix_url + tail
|
|
if old != new_full:
|
|
print(f"{old} -> {new_full}")
|
|
sub.text = new_full
|
|
replaced += 1
|
|
|
|
# сохраняем без XML декларации
|
|
tree.write(xml_path, encoding="utf-8", xml_declaration=False)
|
|
return replaced
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="Заменяет <url> в datasource[friendlyName=JSON] на PREFIX + имя файла."
|
|
)
|
|
parser.add_argument("--file", "-f", required=True, help="Путь к .vmix файлу")
|
|
parser.add_argument("--url", "-u", required=False, help="Префикс нового URL (например, https://gfx.tvstart.ru/app/static/gfx_)")
|
|
parser.add_argument("--pause", action="store_true", help="Ожидать Enter в конце (удобно при запуске двойным кликом)")
|
|
args = parser.parse_args()
|
|
|
|
prefix = args.url or input("Введите префикс нового URL (например, https://gfx.tvstart.ru/app/static/gfx_): ").strip()
|
|
if not prefix:
|
|
print("Префикс пустой — ничего не сделано.")
|
|
return
|
|
|
|
try:
|
|
count = replace_urls_in_vmix(args.file, prefix)
|
|
print(f"Готово. Заменено тегов <url>: {count}")
|
|
print(f"Резервная копия: {args.file}.bak")
|
|
except Exception as e:
|
|
print("Ошибка:", e)
|
|
|
|
if args.pause:
|
|
input("Нажмите Enter для выхода...")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|