Get Latest Video from Channel via YouTube API

 
import logging
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from youtube_api_handler import get_latest_video, ChannelInput
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.post("/latest-video/")
def latest_video(channel_input: ChannelInput):
    try:
        video_info = get_latest_video(channel_input)
        return JSONResponse(status_code=200, content=video_info)
    except Exception as e:
        logger.error(f"Failed to fetch video information: {e}")
        raise HTTPException(status_code=500, detail="Failed to fetch video information.")
    except Exception as e:
        logger.error(f"Failed to fetch latest video: {e}")
        raise HTTPException(status_code=500, detail="Failed to fetch latest video.")

About this template

A FastAPI-based application that retrieves the latest video details from your favorite channels using the YouTube API. Simply provide a channel ID or URL to get comprehensive results, including video ID, title, and description. The app requires a YouTube API key, which can be set as an environment variable.

Introduction to the Template

Welcome to the "How to get the latest video from a channel via YouTube API" template guide. This template is designed to help you create a FastAPI-based application that retrieves the latest video details from a YouTube channel. You'll be able to get the video ID, title, and description by providing a channel ID or URL. This guide will walk you through the steps to use this template on the Lazy platform.

Clicking Start with this Template

To begin using this template, click on the "Start with this Template" button. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Initial Setup

Before you can use this template, you'll 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 the YouTube Data API v3.
  • Enable the YouTube Data API v3 for your project.
  • Go to "Credentials" and click on "Create credentials". Select "API key" and then copy the generated key.
  • Back 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 you have set up your YouTube API key in the environment secrets, press the "Test" button to deploy the app. The Lazy CLI will handle the deployment, and you won't need to worry about installing libraries or setting up your environment.

Entering Input

After pressing the "Test" button and once the app is deployed, the Lazy App's CLI interface will appear. If the code requires user input, you will be prompted to provide it there. For this template, you will need to input the channel ID or URL when prompted by the CLI.

Using the App

After deployment, Lazy will provide you with a dedicated server link to use the API. You can make requests to the `/latest-video/` endpoint to retrieve the latest video from the specified YouTube channel. Additionally, you will be provided with a link to the FastAPI documentation at `/docs` where you can test the API endpoints directly from your browser.

Here's a sample request you might make to the API:


POST /latest-video/
Content-Type: application/json

{
  "channel_id_or_url": "UC_x5XG1OV2P6uZZ5FSM9Ttw"
}

And a sample response you might receive:


{
  "video_id": "dQw4w9WgXcQ",
  "title": "Rick Astley - Never Gonna Give You Up",
  "description": "The official video for “Never Gonna Give You Up” by Rick Astley..."
}

Integrating the App

If you wish to integrate this app with an external tool or service, you may need to add the app's server link provided by Lazy to that tool. For example, if you're building a website that displays the latest video from a YouTube channel, you would make an API call to the server link from your website's backend to retrieve the video details.

Remember, this template is designed to work seamlessly on the Lazy platform, so all the heavy lifting of deployment and environment management is taken care of for you. Enjoy building your application!

Category
Technology
Last published
July 26, 2024

More templates like this

Backend Server

This skeleton is streamlined for creating backend services using FastAPI. It's an excellent choice for building microservices or APIs with minimal frontend requirements.

Fast API
Python

Login and Registration

This powerful app skeleton is a good starting place for apps that require login and registration

Laravel
Fast API
Flask

GET LAZY APP APPLICATION LAUNCHER - Paid Subscriptions

The Get Lazy application Launcher is used to launch and monitor your apps in a production environment - For paid subscriptions only.

Fast API
Python
Home
/
Get Latest Video from Channel via YouTube API