From 877442d55f99ddfec343db31d74d5b5ef7caf8af Mon Sep 17 00:00:00 2001 From: Hadeed Ahmad <me@hadeedahmad.com> Date: Wed, 9 Apr 2025 05:04:58 +0500 Subject: [PATCH] Make simple backend --- .gitignore | 3 +++ main.py | 16 ++++++++++++---- utils.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 utils.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..35236e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +.venv/ +__pycache__/ diff --git a/main.py b/main.py index 584457e..cdd3cf6 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,14 @@ -def main(): - print("Hello from ytlean!") +from fastapi import FastAPI +import utils + +app = FastAPI() -if __name__ == "__main__": - main() +@app.get("/video") +def get_video(channel_name: str): + uploads = utils.get_uploads_playlist(channel_name) + videos = utils.get_all_videos(uploads) + + return { + "id": videos[0] + } diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..d494c75 --- /dev/null +++ b/utils.py @@ -0,0 +1,35 @@ +from dotenv import load_dotenv +import os +import requests + +load_dotenv() +YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY") +YOUTUBE_API_BASE = "https://www.googleapis.com/youtube/v3" + + +def get_uploads_playlist(channel_name: str): + url = f"{YOUTUBE_API_BASE}/channels" + params = { + "forHandle": channel_name, + "key": YOUTUBE_API_KEY + } + + res = requests.get(url, params) + channel_id = res.json().get("items")[0]["id"] + + return f"UULF{channel_id[2:]}" + + +def get_all_videos(playlist_id: str): + url = f"{YOUTUBE_API_BASE}/playlistItems" + params = { + "playlistId": playlist_id, + "part": "contentDetails", + "maxResults": 50, + "key": YOUTUBE_API_KEY + } + + res = requests.get(url, params) + items = res.json().get("items") + + return [i["contentDetails"]["videoId"] for i in items]