youtube 的最新视频 api

latest video by youtube api

为了学习这个api我正在尝试创建一个机器人。

这个机器人所做的其中一件事是在频道上传视频时首先发表评论。

它在某些频道上有效,但在某些频道上无效。

例如在这个频道上 https://www.youtube.com/channel/UC295-Dw_tDNtZXFeAPAW6Aw 它声称最新的视频是 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']

这和调用这个是一样的 https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=1&playlistId={self.upload_id}&key={self.api} 还有其他人遇到过这种不一致吗?

编辑:

我发现使用搜索方法代替 playlistItems 效果很好。有谁知道为什么?我负担不起使用搜索方法的费用,因为每个请求需要 100 quatas。

这是API的一个常见陷阱。请仔细考虑以下内容:

附加了 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 日期时间属性。 (下面的重点是我的。)

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.

那么得到的输出是事实正确的:

$ 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