A Python script to see my most watched YouTube videos

ubershmekel

Yuval Greenfield

Posted on September 22, 2023

A Python script to see my most watched YouTube videos

I use YouTube Music and I was wondering which songs I've listened to the most. You can't find that information in the YouTube Music app. But luckily, Google keeps a complete history at Google Takeout. So I can get the list of my most played songs. Here are the steps to do that.

1. Download YouTube Music takeout data

Go to https://takeout.google.com/settings/takeout and deselect all takeouts.

Deselect all the takeouts

2. Select YouTube and configure the export

YouTube takeout

3. Deselect all YouTube content options

Deselect all YouTube content options

4. Select just "history" content option

Select just "history" content option

5. Export JSON, not HTML

Export JSON, not HTML

6. Create the export

After this step, you will be emailed a link with a zip file that has your history.

Create the export

7. Unzip the file

If you're on a Mac, that's just double-clicking the zip file.

8. Place the Python code next to watch-history.json

In the new folder that was unzipped, you'll find watch-history.json inside a few folders. In my case the path was Downloads/YouTube and YouTube Music/history/watch-history.json so I developed the script at Downloads/YouTube and YouTube Music/history/top_yt.py

Python code

Paste the following into top_yt.py:

import json
from collections import Counter
from pprint import pprint

watched = Counter()
for item in json.load(open('watch-history.json')):
    if "titleUrl" not in item:
        # Watched a video that has been removed
        continue
    title = item["title"]
    url = item["titleUrl"]
    key = f"{title} @> {url}"
    watched[key] += 1

pprint(watched.most_common(20))
Enter fullscreen mode Exit fullscreen mode

Running the code, seeing the result

$ python3 top_yt.py
[('Watched Here Comes The Sun (Remastered 2009) @> '
  'https://www.youtube.com/watch?v=xUNqsfFUwhY',
  334),
 ('Watched Guess Who Is Back @> https://www.youtube.com/watch?v=OZrh6U4CyCw',
  65),
 ('Watched The Rumbling (TV Size) @> '
  'https://www.youtube.com/watch?v=KHCIO7a3jSI',
  61),
 ('Watched ətˈæk 0N tάɪtn @> https://www.youtube.com/watch?v=zroFzv7sFis', 61),
 ('Watched Omokage (produced by Vaundy) @> '
  'https://www.youtube.com/watch?v=jg-uSq64Ru8',
  48),
...
Enter fullscreen mode Exit fullscreen mode

For me, the top result wasn't a song I liked too much, but it was my alarm clock for almost a year. Of these songs, I recommend you check out this one: Omokage by milet, Aimer, Lilas Ikuta, Vaundy

I hope you find this script useful. Cheers.

💖 💪 🙅 🚩
ubershmekel
Yuval Greenfield

Posted on September 22, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related