javascript 如何获取用户的整个 YouTube 观看记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16556770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a user's entire YouTube watch history?
提问by Fsmv
I'm trying to get a full list of watched videos for a given user in my YouTube API application. I want to add up total duration of all videos.
我正在尝试为我的 YouTube API 应用程序中的给定用户获取已观看视频的完整列表。我想把所有视频的总时长加起来。
When I get the list of videos from history playlist, the API caps it at 50 items. There's pagination but total amount of items is 50 (not just per page); I can't access more data with the API it appears.
当我从历史播放列表中获取视频列表时,API 将其限制为 50 个项目。有分页,但项目总数为 50(不仅仅是每页);我无法使用它出现的 API 访问更多数据。
Is there any way I can get this playlist without the data cap? I'm hoping for another method (of using the API) or a way to do it without the API. I know YouTube stores this data because I can view my entire history (far more that 50 videos).
有什么办法可以在没有数据上限的情况下获得此播放列表?我希望有另一种方法(使用 API)或一种无需 API 的方法。我知道 YouTube 会存储这些数据,因为我可以查看我的整个历史记录(远远超过 50 个视频)。
I'm using this code:
我正在使用此代码:
var requestOptions = {
playlistId: playlistId,
part: 'snippet',
maxResults: 50
};
gapi.client.youtube.playlistItems.list(requestOptions);
where playlistId
is the id of the history playlist I got from a gapi.client.youtube.channels.list
request.
playlistId
我从gapi.client.youtube.channels.list
请求中获得的历史播放列表的 ID在哪里。
Edit (2017):I want to clarify that it was always my intention to download my own history, just out of interest to see how much time I have spent watching videos. I still have not been able to do this.
编辑(2017 年):我想澄清一下,我一直打算下载自己的历史记录,只是为了看看我花了多少时间观看视频。我仍然无法做到这一点。
回答by Zv_oDD
I wrote a scraper(in Python 2.7(updated for 3.5) and Scrapy) for this task a while ago. Sans official API, it uses a logged in session cookie and html parsing. Dumps to SQLite by default. https://github.com/zvodd/Youtube-Watch-History-Scraper
不久前,我为此任务编写了一个刮刀(在 Python 2.7(更新为 3.5)和 Scrapy 中)。Sans 官方 API,它使用登录的会话 cookie 和 html 解析。默认转储到 SQLite。 https://github.com/zvodd/Youtube-Watch-History-Scraper
How it's done: essentially it opens the url
它是如何完成的:本质上它打开了 url
https://www.youtube.com/feed/history'
with a valid(logged in) session cookie taken from Chrome. Scrapes all video entries for name, vid(url), channel/user, description, length. Then it finds the button at the bottom of the page with the attribute data-uix-load-more-hrefwhich contains the link to the next page, something like:
使用从 Chrome 获取的有效(登录)会话 cookie。抓取所有视频条目的名称、vid(url)、频道/用户、描述、长度。然后它会在页面底部找到带有属性data-uix-load-more-href的按钮,其中包含指向下一页的链接,例如:
"/browse_ajax?action_continuation=1&continuation=98h32hfoasau0fu928hf2hf908h98hr%253D%253D&target_id=item-section-552363&direct_render=1"
... re-scrapes the video entries from there and dumps them all into an sqlite database; which you can search entries by any of the fields (name, length, user, description, etc).
...从那里重新抓取视频条目并将它们全部转储到 sqlite 数据库中;您可以按任何字段(名称、长度、用户、描述等)搜索条目。
So until they change their feed/history page, it's doable and done. I might even update it.
所以在他们改变他们的提要/历史页面之前,这是可行的。我什至可能会更新它。
回答by vol7ron
The API currently only retrieves the last two weeks of Watch History. For more information refer to the Bug Issue reported: https://code.google.com/p/gdata-issues/issues/detail?id=4642
API 当前仅检索最近两周的观看历史记录。有关更多信息,请参阅报告的错误问题:https: //code.google.com/p/gdata-issues/issues/detail?id=4642
Note:There is a similar question on SO asked here: YouTube API v3 returns truncated watch history
注意:这里有一个关于 SO 的类似问题:YouTube API v3 返回截断的观看历史记录
回答by vol7ron
It seems like this is a known bug originally reported in 2013. The exact same behavior is explained on a Google Code thread: https://code.google.com/p/gdata-issues/issues/detail?id=4642
这似乎是 2013 年最初报告的已知错误。在 Google 代码线程中解释了完全相同的行为:https: //code.google.com/p/gdata-issues/issues/detail?id=4642
回答by noderman
Brainstorming, never tried: Have you tried notusing the API and instead parsing the https://www.youtube.com/feed/historyURL?
头脑风暴,从未尝试过:您是否尝试过不使用 API 而是解析https://www.youtube.com/feed/historyURL?
Theoretically, the user browsing could be emulated, including the pagination. I am not aware of how hard though (probably very), since you need to deal with authentication and YouTube probably tries to verify that a human is browsing.
理论上,可以模拟用户浏览,包括分页。我不知道有多难(可能非常困难),因为您需要处理身份验证,而 YouTube 可能会尝试验证是否有人在浏览。