-2

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 += 3
    return 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.

J.Doe
  • 85
  • 2
  • 8

1 Answers1

1

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
stvar
  • 6,551
  • 2
  • 13
  • 28
  • Can you translate your request into a get request ? – J.Doe Dec 31 '19 at 20:00
  • You may do it yourself since the shell function `youtube-data` is quite evolved (see it on my github account). Anyway, the URLs on which `wget` is called are the same as yours are (modulo using `pageToken` for pagination): ```https://www.googleapis.com/youtube/v3/playlistItems?key=$APP_KEY&part=contentDetails,id,snippet,status&maxResults=50``` – stvar Dec 31 '19 at 20:10
  • But how come this request https://developers.google.com/youtube/v3/docs/playlistItems/list?apix_params=%7B%22part%22%3A%22snippet%22%2C%22playlistId%22%3A%22UU295-Dw_tDNtZXFeAPAW6Aw%22%2C%22videoId%22%3A%22pceedMMwwcE%26t%22%7D doesn't even find the video ? – J.Doe Dec 31 '19 at 20:16
  • Since I am calling `youtube-data` with page being `2` -- that is am querying the first *two* pages of the uploads playlist --, each invocation of `youtube-data` makes two HTTP requests on the PlaylistItems' `list` endpoint. The two URLs are almost identical: the second is containing the `pageToken` parameter referring to the second page: `&pageToken=CDIQAA`. Note that `CDIQAA` is received as the parameter `nextPageToken` on the first API invocation (the one corresponding to the first page). – stvar Dec 31 '19 at 20:19
  • I see, so it is not possible to fetch the latest video in one call with playlistItems in this case. Am i correct ? – J.Doe Dec 31 '19 at 20:24