Get Trending Videos via YouTube API

 import logging
import os
import requests
from fastapi import FastAPI, HTTPException
from fastapi.responses import RedirectResponse
from some_get_request_handler import handle_get_endpoint
from some_post_request_handler import handle_post_endpoint, Data


logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)

app = FastAPI()

@app.get("/", include_in_schema=False)
def root():
    return RedirectResponse(url='/docs')

# Importing KeywordData class from youtube_trending_videos.py
from youtube_trending_videos import get_trending_videos

app.post("/trending/")(get_trending_videos)

if __name__ == "__main__":

About this template

This app is provides fast api endpoint and is designed to fetch trending videos from YouTube based on keywords provided by the user. It utilizes the YouTube Data API v3 to search for videos that are currently trending in the US region. The app requires a valid YouTube API key, which must be set as an environment variable named `YOUTUBE_API_KEY`. The response includes detailed information about each video, including a direct link to the video on YouTube.

Introduction to the Template

Welcome to the "How to get trending videos for a specific keyword via YouTube API" template. This guide will walk you through the process of using this Lazy template to create an application that fetches trending videos from YouTube based on user-provided keywords. The application leverages the YouTube Data API v3 and is designed to be simple to use, even for non-technical builders.

Clicking Start with this Template

To begin, click on the "Start with this Template" button. This will initialize the template in the Lazy Builder interface, pre-populating the code necessary for the application.

Initial Setup: Adding Environment Secrets

Before testing the application, you need to set up an environment secret for the YouTube API key. Here's how to obtain and set up your YouTube API key:

  • Go to the Google Developers Console (https://console.developers.google.com/).
  • Create a new project or select an existing one.
  • Click on "Enable APIs and Services" and search for "YouTube Data API v3".
  • Enable the YouTube Data API v3 for your project.
  • Go to "Credentials" and click on "Create credentials". Select "API key" from the dropdown.
  • Copy the generated API key.
  • In the Lazy Builder interface, navigate to the Environment Secrets tab.
  • Create a new secret with the key `YOUTUBE_API_KEY` and paste the API key you copied as the value.

Test: Pressing the Test Button

Once the environment secret is set, press the "Test" button. This will deploy the application and launch the Lazy CLI. You will be provided with a server link to interact with the API.

Entering Input: Filling in User Input

If the application requires user input, the Lazy CLI will prompt you for it after pressing the test button. For this template, you will be asked to provide keywords for which you want to find trending YouTube videos.

Using the App

After deployment, you can use the provided server link to send requests to the API. Additionally, you will receive a link to the FastAPI documentation (`/docs`) where you can test the API endpoints directly from your browser.

Here's a sample request to the `/trending/` endpoint:


POST /trending/
Content-Type: application/json

{
  "keywords": "music"
}

And a sample response might look like this:


[
  {
    "title": "Trending Music Video",
    "description": "Latest trending music video on YouTube",
    "videoUrl": "https://www.youtube.com/watch?v=example"
    // ... other video details
  }
  // ... more videos
]

Integrating the App

If you wish to integrate this application with an external tool or service, you will need to use the server link provided by Lazy. For instance, you can add the API endpoint to a frontend application to display trending videos based on user input. Ensure that you handle the API key securely and do not expose it in the frontend.

For further integration, such as adding the app's server link in an external tool, follow the specific instructions provided by that tool for adding external API endpoints.

By following these steps, you should now have a fully functional application that can fetch trending YouTube videos based on keywords. Enjoy building with Lazy!

Category
Technology
Last published
July 26, 2024

More templates like this

SecureUserAuthenticator

Develop a secure User Authentication system for users to register, log in, and manage their profiles, laying the foundation for user-specific data management and permissions in the CMS.

Laravel
Python
Flask
Javascript

Simple Multiplayer Telegram game

This app is a simple frontend for a game where users can upvote and downvote the most popular word in their country, learn about the flags of other countries, and view what other people voted for on a leaderboard.

Telegram
Python
Javascript

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.

Python
Flask
Home
/
Get Trending Videos via YouTube API