Spaces:
Sleeping
Sleeping
Update app.py
Browse files95 unmodified lines9697989910010110210318 unmodified lines1221231241251261273 unmodified lines13113213313413513640 unmodified lines17717817918018118212 unmodified lines1951961971981992009 unmodified lines21021121221321421515 unmodified lines2312322332342352362372382393 unmodified lines24324424524624724824995 unmodified linesreturn f"/{p}" if p else "/"# ===== Core mirroring =====def mirror_course(intake_url: str, course_name: str, mapping_text: str,base_root: str, dry_run: bool) -> Tuple[str, str]:# Password gateif APP_PASSWORD and password != APP_PASSWORD:return "", "Wrong password."18 unmodified lines# Parse translator→language maptmap = HARDCODED_MAP # keys are lowercased# Walk: Root/{Translator}/{Course}/{VideoType}/{Title/files}logs = []created, skipped, uploaded, failed = 0, 0, 0, 03 unmodified linesif not is_folder(t):continuetranslator_name = t["name"]# Find the course folder within this translatorfor candidate in list_children(svc, t["id"]):if not is_folder(candidate):40 unmodified linesreq = svc.files().get_media(fileId=ff["id"])MediaIoBaseDownload(buf, req).next_chunk()upload_file(dbx, dp, buf); uploaded += 1logs.append(f"✓ {dp}")except Exception as e:failed += 1; logs.append(f"× {dp} :: {e}")12 unmodified linesreq = svc.files().get_media(fileId=f["id"])MediaIoBaseDownload(buf, req).next_chunk()upload_file(dbx, dp, buf); uploaded += 1logs.append(f"✓ {dp}")except Exception as e:failed += 1; logs.append(f"× {dp} :: {e}")9 unmodified linesreq = svc.files().get_media(fileId=title["id"])MediaIoBaseDownload(buf, req).next_chunk()upload_file(dbx, dp, buf); uploaded += 1logs.append(f"✓ {dp}")except Exception as e:failed += 1; logs.append(f"× {dp} :: {e}")15 unmodified lineswith gr.Row():intake = gr.Textbox(label="Drive intake folder URL", placeholder="https://drive.google.com/drive/folders/<ID>")with gr.Row():base_root = gr.Textbox(value="/Course Translations", label="Dropbox base root")course = gr.Textbox(label="Course name (exact match)", placeholder="C3 Acid-Base Essentials")mapping = gr.Textbox(lines=6, label="Translator→Language map (optional)", placeholder="Alice = German\nBob=French")dry = gr.Checkbox(value=False, label="Dry run (no uploads)")pw = gr.Textbox(type="password", label="Access password")run_btn = gr.Button("Run")3 unmodified linesrun_btn.click(fn=mirror_course,inputs=[intake, course, mapping, base_root, dry, pw],outputs=[logs, status])95 unmodified lines9697989910010110210310418 unmodified lines1231241251261271281291303 unmodified lines13413513613713813914014140 unmodified lines18218318418518618718818919019119219319419512 unmodified lines2082092102112122132142152162172182192202212229 unmodified lines23223323423523623723823924024124224324424515 unmodified lines2612622632642652662672682692703 unmodified lines27427527627727827928095 unmodified linesreturn f"/{p}" if p else "/"# ===== Core mirroring =====def mirror_course(intake_url: str, course_name: str, base_root: str,only_translators: str, overwrite: bool,dry_run: bool, password: str) -> Tuple[str, str]:# Password gateif APP_PASSWORD and password != APP_PASSWORD:return "", "Wrong password."18 unmodified lines# Parse translator→language maptmap = HARDCODED_MAP # keys are lowercased# Optional translator allowlistallow = {x.strip().lower() for x in (only_translators or "").split(",") if x.strip()}# Walk: Root/{Translator}/{Course}/{VideoType}/{Title/files}logs = []created, skipped, uploaded, failed = 0, 0, 0, 03 unmodified linesif not is_folder(t):continuetranslator_name = t["name"]if allow and translator_name.strip().lower() not in allow:continue# Find the course folder within this translatorfor candidate in list_children(svc, t["id"]):if not is_folder(candidate):40 unmodified linesreq = svc.files().get_media(fileId=ff["id"])MediaIoBaseDownload(buf, req).next_chunk()upload_file(dbx, dp, buf); uploaded += 1if not overwrite:try:dbx.files_get_metadata(dp);logs.append(f"• Exists, skipped: {dp}")skipped += 1; continueexcept dropbox.exceptions.ApiError:passupload_file(dbx, dp, buf); uploaded += 1logs.append(f"✓ {dp}")except Exception as e:failed += 1; logs.append(f"× {dp} :: {e}")12 unmodified linesreq = svc.files().get_media(fileId=f["id"])MediaIoBaseDownload(buf, req).next_chunk()upload_file(dbx, dp, buf); uploaded += 1# skip if exists and not overwritingif not overwrite:try:dbx.files_get_metadata(dp);logs.append(f"• Exists, skipped: {dp}")skipped += 1; continueexcept dropbox.exceptions.ApiError:passupload_file(dbx, dp, buf); uploaded += 1logs.append(f"✓ {dp}")except Exception as e:failed += 1; logs.append(f"× {dp} :: {e}")9 unmodified linesreq = svc.files().get_media(fileId=title["id"])MediaIoBaseDownload(buf, req).next_chunk()upload_file(dbx, dp, buf); uploaded += 1if not overwrite:try:dbx.files_get_metadata(dp);logs.append(f"• Exists, skipped: {dp}")skipped += 1; continueexcept dropbox.exceptions.ApiError:passupload_file(dbx, dp, buf); uploaded += 1logs.append(f"✓ {dp}")except Exception as e:failed += 1; logs.append(f"× {dp} :: {e}")15 unmodified lineswith gr.Row():intake = gr.Textbox(label="Drive intake folder URL", placeholder="https://drive.google.com/drive/folders/<ID>")with gr.Row():base_root = gr.Textbox(value="/Course Translations", label="Dropbox base root")course = gr.Textbox(label="Course name (exact match)")only_translators = gr.Textbox(label="Only these translators (comma-separated, optional)")overwrite = gr.Checkbox(value=False, label="Overwrite existing files in Dropbox")dry = gr.Checkbox(value=False, label="Dry run (no uploads)")pw = gr.Textbox(type="password", label="Access password")run_btn = gr.Button("Run")3 unmodified linesrun_btn.click(fn=mirror_course,inputs=[intake, course, base_root, only_translators, overwrite, dry, pw],outputs=[logs, status])
app.py+37-6