# -*- coding: utf-8 -*- """youtube_video_recommendation.ipynb Automatically generated by Colab. Original file is located at https://colab.research.google.com/drive/1pLpvcg7hC7hp2PmGBc8qe86ktv3eT9gp """ import gradio as gr import requests import os API_KEY = os.getenv("API_KEY")# Replace with your own API key def search_youtube(query): if not query.strip(): return "Please enter a search term." search_url = "https://www.googleapis.com/youtube/v3/search" search_params = { "part": "snippet", "q": query, "type": "video", "maxResults": 25, "key": API_KEY } search_response = requests.get(search_url, params=search_params).json() video_ids = [item["id"]["videoId"] for item in search_response.get("items", [])] if not video_ids: return "❌ No results found." stats_url = "https://www.googleapis.com/youtube/v3/videos" stats_params = { "part": "snippet,statistics", "id": ",".join(video_ids), "key": API_KEY } stats_response = requests.get(stats_url, params=stats_params).json() if "items" not in stats_response: return "❌ No results found or API limit reached." results = [] for item in stats_response["items"]: snippet = item["snippet"] title = snippet["title"] description = snippet.get("description", "") channel = snippet.get("channelTitle", "") tags = snippet.get("tags", []) video_id = item["id"] url = f"https://www.youtube.com/watch?v={video_id}" # Skip YouTube Shorts if "shorts" in title.lower() or "/shorts/" in url.lower(): continue combined_text = " ".join([title, description, channel] + tags).lower() stats = item.get("statistics", {}) thumbnail = snippet["thumbnails"]["medium"]["url"] views = int(stats.get("viewCount", 0)) likes = int(stats.get("likeCount", 0)) results.append({ "title": title, "thumbnail": thumbnail, "views": views, "likes": likes, "channel": channel, "url": url }) if not results: return "❌ No suitable videos found." # Grid layout for results grid_cards = "" for vid in results: grid_cards += f"""
""" return f"""