import gradio as gr import os import json import pandas as pd from googleapiclient.discovery import build # YouTube class to interact with the YouTube Data API class YouTube: def __init__(self, api_key, channel_id): self.api_key = api_key self.channel_id = channel_id self.youtube = build("youtube", "v3", developerKey=api_key) # Retrieve general channel information def get_general_channel_info(self): try: request = self.youtube.channels().list(part="snippet,statistics", id=self.channel_id) response = request.execute() channel_info = response["items"][0] channel_stats = { "Subscribers": channel_info["statistics"]["subscriberCount"], "Views": channel_info["statistics"]["viewCount"], "Total videos": channel_info["statistics"]["videoCount"], "Title": channel_info["snippet"]["title"], "Description": channel_info["snippet"]["description"], "Cover": channel_info["snippet"]["thumbnails"]["high"]["url"], "URL": f"https://www.youtube.com/channel/{self.channel_id}", } return channel_stats except Exception as e: return {"error": str(e)} # Retrieve video data from the channel's uploads playlist def get_video_data(self): try: channel_response = self.youtube.channels().list(part="contentDetails", id=self.channel_id).execute() playlist_id = channel_response["items"][0]["contentDetails"]["relatedPlaylists"]["uploads"] videos = [] next_page_token = None while True: playlist_items_response = self.youtube.playlistItems().list( part="snippet", maxResults=50, playlistId=playlist_id, pageToken=next_page_token, ).execute() videos.extend(playlist_items_response["items"]) next_page_token = playlist_items_response.get("nextPageToken") if next_page_token is None: break video_data = [] for video in videos: video_id = video["snippet"]["resourceId"]["videoId"] video_details = self.youtube.videos().list(part="statistics,snippet", id=video_id).execute() if video_details["items"]: video_info = video_details["items"][0] snippet = video_info["snippet"] statistics = video_info["statistics"] video_data.append( { "Video": f"https://www.youtube.com/watch?v={video_id}", "Thumbnail": snippet["thumbnails"]["high"]["url"], "Title": snippet["title"], "PublishedAt": snippet["publishedAt"], "👀": statistics.get("viewCount", 0), "👍": statistics.get("likeCount", 0), "💬": statistics.get("commentCount", 0) } ) df = pd.DataFrame(video_data) return df except Exception as e: return {"error": str(e)} # Load JSON scheme file def load_scheme(file_path): with open(file_path) as f: return json.load(f) # Format and display general channel statistics as HTML def display_channel_statistics(statistics): if "error" in statistics: return statistics["error"] return f"""