get last 5 yt videos to add to video page

This commit is contained in:
Death916 2025-05-07 02:37:09 -07:00
parent da3288b015
commit 8bcee63280

View file

@ -20,6 +20,7 @@ class Youtube:
def get_newest_video(self):
# get the newest video from the youtube channel
keys = self.get_api_key()
#use basic auth to get newest video
youtube = build("youtube", "v3", developerKey=keys["youtube_api_key"])
# get the channel id
@ -44,6 +45,27 @@ class Youtube:
self.get_newest_video()
return self.current_yt_video
def get_last_5_yt_videos(self):
# get the last 5 videos from the youtube channel
keys = self.get_api_key()
youtube = build("youtube", "v3", developerKey=keys["youtube_api_key"])
id = keys["channel_id"]
request = youtube.search().list(
part="snippet",
channelId=id,
order="date",
maxResults=5,
type="video"
)
response = request.execute()
items = response.get("items", [])
videos = []
for item in items:
video_id = item["id"]["videoId"]
video_url = f"https://www.youtube.com/watch?v={video_id}"
videos.append(video_url)
return videos
if __name__ == "__main__":
yt = Youtube()
yt.get_current_yt_video()