为了学习此api,我正在尝试创建一个机器人。
此机器人执行的操作之一是在频道上传视频时首先发表评论。
在某些频道上有效,但是在某些频道上无效。
例如,在此频道https://www.youtube.com/channel/UC295-Dw_tDNtZXFeAPAW6Aw它声称最新的视频是https://www.youtube.com/watch?v=cZI3Krk59T4,而实际的最新视频是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 quas。
这是API的已知陷阱。请仔细考虑以下内容(我正在对此问题进行调整one of my older answers):
查询频道的上载列表的PlaylistItems endpoint会产生一个items列表,该列表按上载日期排序。但是items本身包含附加的publishedAt日期时间属性。 (下面的重点是我的。)
publishedAt(日期时间)
视频发布的日期和时间。 请注意,此时间可能不同于视频的上传时间。例如,如果将视频作为私人视频上传,然后在以后公开,则此属性将指定视频的时间公开了。
然后获得的输出是正确的:
$ 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