#!/usr/bin/env python3
import re, os, json, time, urllib.request, urllib.parse

token = None
for line in open("/root/site/yandex/.env", encoding="utf-8"):
    if line.startswith("YANDEX_ACCESS_TOKEN="):
        token = line.split("=", 1)[1].strip()
H = {"Authorization": f"OAuth {token}"}
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0"}

def fetch(url, binary=False):
    req = urllib.request.Request(url, headers=UA)
    with urllib.request.urlopen(req, timeout=90) as r:
        data = r.read()
    return data if binary else data.decode("utf-8", errors="replace")

def disk_list(path):
    url = "https://cloud-api.yandex.net/v1/disk/resources?path=" + urllib.parse.quote(path)
    req = urllib.request.Request(url, headers=H)
    with urllib.request.urlopen(req, timeout=60) as r:
        return json.loads(r.read().decode())

def disk_upload(path, local_file):
    url = ("https://cloud-api.yandex.net/v1/disk/resources/upload?path=" +
           urllib.parse.quote(path) + "&overwrite=true")
    req = urllib.request.Request(url, headers=H)
    with urllib.request.urlopen(req, timeout=60) as r:
        href = json.loads(r.read().decode())["href"]
    with open(local_file, "rb") as f:
        data = f.read()
    req = urllib.request.Request(href, data=data, method="PUT")
    with urllib.request.urlopen(req, timeout=180) as r:
        return r.status

def extract_photos(html_file):
    t = open(html_file, encoding="utf-8", errors="replace").read()
    urls = {}
    for m in re.finditer(r'<a href="(/upload/iblock/[a-z0-9]+/[A-Za-z0-9]+\.(?:JPG|jpg|jpeg|png))"[^>]*data-fancybox="(?:big|small)-gallery"', t):
        urls.setdefault(m.group(1).rsplit("/", 1)[-1].lower(), "https://dobrotniy-dom.ru" + m.group(1))
    return list(urls.values())

WORK = "/root/site/video/site_renders"
JOBS = [("Троицкий", "/root/site/video/ognikovo.html"),
        ("Богородский", "/root/site/video/blaznovo.html")]

for name, html_file in JOBS:
    urls = extract_photos(html_file)
    print(f"=== {name}: {len(urls)} фото")
    # текущее число файлов в папке на диске
    try:
        d = disk_list("/Рендеры с сайта/" + name)
        n = len([x for x in d.get("_embedded", {}).get("items", []) if x["type"] == "file"])
    except Exception:
        n = 0
    folder = os.path.join(WORK, name)
    os.makedirs(folder, exist_ok=True)
    for i, url in enumerate(sorted(urls), n + 1):
        ext = url.rsplit(".", 1)[-1].lower()
        local = os.path.join(folder, f"{i:02d}.{ext}")
        if not os.path.exists(local):
            try:
                data = fetch(url, binary=True)
                open(local, "wb").write(data)
            except Exception as e:
                print("  DOWNLOAD FAIL", url, e); continue
        try:
            disk_upload(f"/Рендеры с сайта/{name}/{i:02d}.{ext}", local)
            print(f"  + {i:02d}.{ext}")
        except Exception as e:
            print("  UPLOAD FAIL", url, e)
        time.sleep(0.15)
print("MERGE_DONE")
