Make simple backend
This commit is contained in:
parent
61da972a58
commit
877442d55f
3 changed files with 50 additions and 4 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
.env
|
||||
.venv/
|
||||
__pycache__/
|
16
main.py
16
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]
|
||||
}
|
||||
|
|
35
utils.py
Normal file
35
utils.py
Normal file
|
@ -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]
|
Loading…
Add table
Reference in a new issue