latest video by youtube api

2024/11/19 2:49:28

In order to learn this api I am trying to create a bot.

one of the things this bot does is to first comment when a channel uploads a video.

On some channels it works however on some channels it doesn't work.

For example on this channel https://www.youtube.com/channel/UC295-Dw_tDNtZXFeAPAW6Aw it claims the latest video is https://www.youtube.com/watch?v=cZI3Krk59T4 when the real latest video is https://www.youtube.com/watch?v=pceedMMwwcE&t.

self.youtube = build('youtube', 'v3', developerKey=api, credentials=credentials)
self.upload_id = self.youtube.channels().list(id=self.channel_id, part='contentDetails').execute()['items'][0]['contentDetails']['relatedPlaylists']['uploads']def get_latest_video(self):url = f'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=1&playlistId={self.upload_id}&key={self.api}'json_url = urllib.request.urlopen(url)data = json.loads(json_url.read())self.quata_spent += 3return data['items'][0]['snippet']['resourceId']['videoId']

which is the same as calling this https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=1&playlistId={self.upload_id}&key={self.api} Has anyone else has encountered this inconsistency ?

edit:

I found out that using the search method instead of the playlistItems works fine. Does anyone know why ? I cant afford using the search method as it costs 100 quatas per request.

Answer

This is a common pitfall of the API. Please consider carefully the following:

The PlaylistItems endpoint queried for the uploads list of a channel produces an items list which is ordered by videoPublishedAt. But the items themselves contains publishedAt datetime properties attached. (The emphasis below is mine.)

videos#snippet.publishedAt (datetime)

The date and time that the video was published. Note that this time might be different than the time that the video was uploaded. For example, if a video is uploaded as a private video and then made public at a later time, this property will specify the time that the video was made public.

Then the output obtained is fact correct:

$ youtube-data --channel=UC295-Dw_tDNtZXFeAPAW6Aw --uploads --page=+2 --table --relative-date|grep -wEn '^(cZI3Krk59T4|pceedMMwwcE)'1:cZI3Krk59T4   2   days  8  hours ago    33 LIFE-SAVING OUTDOOR TRICKS YOU NEED TO TRY YOURSELF
62:pceedMMwwcE   8  hours 19   mins ago    25 CRAZY IDEAS TO HAVE FUN WITH FRIENDS$ youtube-data --playlist=UU295-Dw_tDNtZXFeAPAW6Aw --videos --page=+2 --table --relative-date|grep -wEn '^(cZI3Krk59T4|pceedMMwwcE)'1:cZI3Krk59T4   2   days  8  hours ago    33 LIFE-SAVING OUTDOOR TRICKS YOU NEED TO TRY YOURSELF
62:pceedMMwwcE   8  hours 19   mins ago    25 CRAZY IDEAS TO HAVE FUN WITH FRIENDS
https://en.xdnf.cn/q/120002.html

Related Q&A

Function to switch between two frames in tkinter

Im looking through the code at passing-functions-parameters-tkinter-using-lambda, and needed a tad more functionality inside his class PageOne(tk.Frame). Instead of using lambda commands below (as he …

Get values from a tuple in a list in Python

x = Bookshop() x.orders = [ [1, ("5464", 4, 9.99), ("8274",18,12.99), ("9744", 9, 44.95)], [2, ("5464", 9, 9.99), ("9744", 9, 44.95)], [3, ("5464&…

Last Digit of the Sum of Fibonacci Numbers

I am trying to find the last digit of sum of Fibonacci Series. I calculate the sum as F(n+2) - 1. The below code is working fine but it is slow for large numbers (e.g 99999). How can I optimize this?n…

Django websites not loading

I have two Django websites on one server using Apache with mod_wsgi on Windows 10. For some reason the Django websites dont load, however, I have a normal website that does. Ive had it work in the past…

list manipulation and recursion

I have a mansory-grid in a pdf-page. The grid is choosen randomly, so i do not know how much upright cells or cross cells I have to fill. In my list I have all images that I want to proceed, each marke…

Getting TypeError: int object is not callable

Getting TypeError: int object is not callable. What am i doing wrong as i just want to add 10 to the z variableprint ("Hello World")x=int(input("Enter X")) y=int(input("Enter Y…

How to create a loop from 1-9 and from a-z?

I currently use:for i in range(1,10):print iWhich prints the digits 1 to 9. But I want to add a-z to the mix. How can I combine them?

Need Python to accept upper and lower case input

I want my Python code to accept both uppercase and lowercase input.Ive tried casefold, but with no luck. Any help?advice ="" while advice !=("Yes"):print("Would you like some…

What is [1] , in sock.getsockname()[1]? [duplicate]

This question already has answers here:How to access List elements(5 answers)Closed 7 years ago.I was going through socket programming in Python and I saw this: sock.getsockname()[1] Can anyone please …

How to use sys.exit() if input is equal to specific number

I am looking to correct this code so that when the user inputs 99999 then the code stops running, im also looking to make it so that if the user input is 999 it sets the total to 0 import sysdef money_…