File size: 3,545 Bytes
73a6587
4aebf77
 
 
 
 
 
 
 
 
73a6587
4aebf77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73a6587
4aebf77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from modules.youtube_metadata.db import get_youtube_metadata_collection
import pandas as pd

page_size = 10  # change if you like


# -------------------------------
# Fetch channel videos as HTML table with pagination
# -------------------------------
def fetch_channel_html(channel_id: str, page: int = 1, page_size: int = 10):
    collection = get_youtube_metadata_collection()
    offset = (page - 1) * page_size

    all_results = collection.get(
        where={"channel_id": channel_id}, include=["metadatas"]
    )
    total_count = (
        len(all_results["metadatas"])
        if all_results and "metadatas" in all_results
        else 0
    )
    results = collection.get(
        where={"channel_id": channel_id},
        include=["documents", "metadatas"],
        limit=page_size,
        offset=offset,
    )

    # handle empty
    if not results or not results.get("metadatas"):
        return f"""
        <div style="display:flex;justify-content:center;align-items:center;
                    height:200px;flex-direction:column;color:#666;">
            ⚠️ No videos found for this channel (page {page}).
        </div>
        """

    videos = results["metadatas"]

    # build table
    html = (
        f"<div>Total: {total_count} videos</div>"
        + """
    <table border="1" style="border-collapse:collapse;width:100%;font-family:sans-serif;">
        <thead style="background:#f0f0f0;">
            <tr>
                <th>#</th>
                <th>Title</th>
                <th>Video URL</th>
                <th>Description</th>
            </tr>
        </thead>
        <tbody>
    """
    )

    for idx, v in enumerate(videos, start=offset + 1):
        html += f"""
        <tr>
            <td>{idx}</td>
            <td>{v.get('video_title','')}</td>
            <td><a href="https://youtube.com/watch?v={v.get('video_id')}" 
                   target="_blank">Watch Video</a></td>
            <td>{v.get('description','')}</td>
        </tr>
        """

    html += "</tbody></table>"
    return html


# -------------------------------
# Fetch channel videos as HTML table with pagination
# -------------------------------
def fetch_channel_dataframe(channel_id: str):
    collection = get_youtube_metadata_collection()

    results = collection.get(
        where={"channel_id": channel_id}, include=["documents", "metadatas"]
    )
    total_count = len(results["metadatas"]) if results and "metadatas" in results else 0
    # handle empty
    if not results or not results.get("metadatas"):
        return pd.DataFrame(data=[])

    videos = results["metadatas"]

    items = []
    for idx, v in enumerate(videos, start=1):
        item = {
            "#": idx,
            "title": v.get("video_title", "-"),
            "description": v.get("description", ""),
            "url": f"""<a style="color: blue" href="https://youtube.com/watch?v={v.get('video_id')}" 
                   target="_blank">▶️Watch Video</a>""",
        }
        items.append(item)
    return pd.DataFrame(data=items)


def update_table(channel_id, page):
    return fetch_channel_html(channel_id, page, page_size), f"Page {page}"


def prev_page(channel_id, page):
    new_page = max(1, page - 1)
    return (
        fetch_channel_html(channel_id, new_page, page_size),
        f"Page {new_page}",
        new_page,
    )


def next_page(channel_id, page):
    new_page = page + 1
    return (
        fetch_channel_html(channel_id, new_page, page_size),
        f"Page {new_page}",
        new_page,
    )