How to get my entire YouTube watch history?

Solution 1:

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

Note: There is a similar question on SO asked here: YouTube API v3 returns truncated watch history

Solution 2:

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

How it's done: essentially it opens the 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-href which contains the link to the next page, something like:

"/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).

So until they change their feed/history page, it's doable and done. I might even update it.

Solution 3:

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

Solution 4:

While this isn't currently possible using just the YouTube API, there is an (albeit slightly involved) method to calculate your watch time):

  1. download a list of your watch history as a JSON file using Google Takeout.
  2. Unfortunately the JSON file doesn't include the video durations, so the next step is to extract the video IDs (the part after "watch?v=" in the "titleURL" object
  3. Now take your list of video IDs, and send a request to the youtube API that looks something like this:
 function execute() {
    return gapi.client.youtube.videos.list({
      "part": [
        "contentDetails"
      ],
      "id": [
        "VIDEO IDs"
      ],
      "fields": "items(contentDetails(duration))"
    })

(Code created using YouTube API Explorer)

Note: You may need to break the list of video IDs into smaller lists (I had to) or the API may reject the request. As [pointed out by stvar in the comments] the ID list maximum length is 50, so this is the maximum length your lists can be. (full disclosure: I was using Python to send the requests)

  1. Finally, just extract the duration values and add them up (though this might not be quite as easy as it sounds)

The best part of this is I don't believe this actually violates any ToS.