latest video by youtube api

给你一囗甜甜゛ 提交于 2020-01-17 23:04:11

问题


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.


回答1:


This is a known pitfall of the API. Please consider carefully the following (I am adapting one of my older answers on this issue):

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

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://stackoverflow.com/questions/59545303/latest-video-by-youtube-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!