File size: 2,739 Bytes
3d3a70e
fc75fc9
6f1cbb7
 
 
 
 
 
 
 
 
fc75fc9
6f1cbb7
 
 
 
fc75fc9
6f1cbb7
 
fc75fc9
6f1cbb7
 
 
 
 
d1d2b77
6f1cbb7
 
 
d1d2b77
6f1cbb7
 
fc75fc9
6f1cbb7
 
fc75fc9
6f1cbb7
d1d2b77
6f1cbb7
 
 
 
d1d2b77
6f1cbb7
 
 
 
d1d2b77
6f1cbb7
 
 
 
 
 
 
 
d1d2b77
 
6f1cbb7
 
 
 
d1d2b77
6f1cbb7
 
 
fc75fc9
6f1cbb7
 
fc75fc9
6f1cbb7
fc75fc9
6f1cbb7
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import gradio as gr
from yt_dlp import YoutubeDL
import os
import tempfile
import shutil

# -----------------------
# Helper Functions
# -----------------------
def download_tiktok_thumbnails(username_url, cookies_file=None, max_videos=20):
    tmp_dir = tempfile.mkdtemp()
    ydl_opts = {
        "skip_download": True,
        "write_thumbnail": True,
        "outtmpl": os.path.join(tmp_dir, "%(id)s.%(ext)s"),
        "quiet": True,
    }
    if cookies_file:
        ydl_opts["cookiefile"] = cookies_file.name

    try:
        with YoutubeDL(ydl_opts) as ydl:
            ydl.download([username_url])
    except Exception as e:
        return f"❌ Error: {e}", None, None

    # Collect all thumbnails
    thumbs_all = [os.path.join(tmp_dir, f) for f in os.listdir(tmp_dir)
                  if f.lower().endswith((".jpg", ".webp", ".png"))]

    # Apply limiter
    thumbs = thumbs_all[:max_videos]

    if not thumbs:
        return "❌ No thumbnails found", None, None

    return f"βœ… Fetched {len(thumbs)} thumbnails", thumbs, tmp_dir

def prepare_zip(tmp_dir):
    zip_path = os.path.join(tempfile.gettempdir(), "tiktok_thumbnails.zip")
    shutil.make_archive(zip_path.replace(".zip",""), 'zip', tmp_dir)
    return zip_path

def fetch_and_zip_tiktok(username_url, cookies_file, max_videos):
    status, thumbs, tmp_dir = download_tiktok_thumbnails(username_url, cookies_file, max_videos)
    zip_file = prepare_zip(tmp_dir) if thumbs else None
    return status, thumbs, zip_file

# -----------------------
# Gradio Interface
# -----------------------
with gr.Blocks() as demo:
    gr.Markdown("## 🎡 TikTok Account Thumbnails Downloader with Cookies")
    gr.Markdown(
        "Upload your `cookies.txt` (exported from your browser) first, then enter a TikTok account URL.\n"
        "Thumbnails from private or age-restricted videos may require cookies."
    )

    cookies_upload = gr.File(label="Upload cookies.txt (required for private content)", file_types=[".txt"])
    url_input = gr.Textbox(label="TikTok Account URL", placeholder="https://www.tiktok.com/@username")
    max_videos_slider = gr.Slider(minimum=1, maximum=50, step=1, value=20, label="Max Videos to Fetch")
    fetch_btn = gr.Button("πŸ“₯ Fetch Thumbnails")

    status_output = gr.Textbox(label="Status")
    thumbs_gallery = gr.Gallery(label="Thumbnails Preview", elem_id="thumbs_gallery", columns=5, height="auto")
    download_btn = gr.File(label="Download All Thumbnails (ZIP)")

    inputs_list = [url_input, cookies_upload, max_videos_slider]
    outputs_list = [status_output, thumbs_gallery, download_btn]

    fetch_btn.click(fetch_and_zip_tiktok, inputs=inputs_list, outputs=outputs_list)

if __name__ == "__main__":
    demo.launch()