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"""

{statistics["Title"]}
Subscribers: {statistics["Subscribers"]}
Views: {statistics["Views"]}
Total videos: {statistics["Total videos"]}
""" # Format and display video data as HTML table with enhanced CSS def display_video_data(video_data): if "error" in video_data: return video_data["error"] video_data['Title'] = video_data['Title'].apply(lambda x: x.split('#')[0]) video_data['PublishedAt'] = pd.to_datetime(video_data['PublishedAt']).dt.date video_data['Video'] = video_data.apply( lambda row: f'{row["Title"]}
{row["PublishedAt"]}', axis=1 ) video_data['Thumbnail'] = video_data['Thumbnail'].apply(lambda x: f'
') video_data['Video'] = video_data['Thumbnail'] + video_data['Video'] video_data = video_data.drop(['Thumbnail', 'Title', 'PublishedAt'], axis=1) # Custom CSS for table styling custom_css = """ """ return custom_css + video_data.to_html(escape=False, index=False).replace('text-align: right;', 'text-align: center;') # Load API key from environment variable api_key = os.getenv('API_KEY') scheme = load_scheme("scheme.json") # Fetch data based on selected category and platform def fetch_data(selected_category, platform): channel_id = scheme[selected_category][platform] if platform == "youtube": youtube_account = YouTube(api_key, channel_id) statistics = youtube_account.get_general_channel_info() statistics_display = display_channel_statistics(statistics) video_data = youtube_account.get_video_data() video_data_display = display_video_data(video_data) return statistics_display, video_data_display # Define and launch Gradio interface css = """ """ gr_interface = gr.Interface( fn=fetch_data, inputs=[ gr.Dropdown(choices=list(scheme.keys()), label="Select Channel", elem_classes=["fixed-top"]), gr.Dropdown(choices=list(next(iter(scheme.values())).keys()), label="Select Platform", elem_classes=["fixed-top"]) ], outputs=[ gr.HTML(label="General Channel Statistics"), gr.HTML(label="Video Data") ], title="Social Media Analytics", description="Select a channel and platform to fetch analytics data.", css=css ) gr_interface.launch()