Posted: . At: 10:09 AM. This was 12 months ago. Post ID: 17967
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

A simple Python script to get the most viewed videos on a Youtube channel.

This Python script will print a listing of all 20 most viewed Youtube videos on a channel. Use the instructions here to install this.

youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
 
from googleapiclient.discovery import build
 
# Replace YOUR_API_KEY with your actual API key
api_key = "YOUR_API_KEY"
youtube = build('youtube', 'v3', developerKey=api_key)
channel_id = "UUN1hnUccO4FD5WfM7ithXaw" # Replace with the channel ID you want to get the videos from
videos = []
next_page_token = None
 
while True:
 
    # Get the playlist items for the uploads playlist of the channel
    res = youtube.playlistItems().list(part='snippet', playlistId=channel_id, maxResults=50, pageToken=next_page_token).execute()
 
    for item in res['items']:
 
        # Get the video ID and view count for each video
 
        video_id = item['snippet']['resourceId']['videoId']
        video_res = youtube.videos().list(part='statistics', id=video_id).execute()
        view_count = int(video_res['items'][0]['statistics']['viewCount'])
 
        # Add the video ID, title, and view count to the list of videos
 
        videos.append({'id': video_id, 'title': item['snippet']['title'], 'views': view_count})
 
    # Check if there are more pages of results
 
    next_page_token = res.get('nextPageToken')
 
    if not next_page_token:
 
        break
 
# Sort the videos by view count (descending)
 
videos.sort(key=lambda x: x['views'], reverse=True)
 
# Print the top 10 most viewed videos
 
for video in videos[:20]:
 
    print(video['title'], video['views'])

Below is an example of the output of this script. This requires a Youtube API key.

(john) ┗━━━━━━━━━━┓ john@localhost ~/Documents
           ┗━━━━━━━━━━━━━╾ ╍▷ python youtube.py 
Maroon 5 - Sugar (Official Music Video) 3861512735
Maroon 5 - Girls Like You ft. Cardi B (Official Music Video) 3406634224
Maroon 5 - One More Night (Official Music Video) 1035599467
Maroon 5 - Memories (Official Video) 924551244
Maroon 5 - Animals 864055464
Maroon 5 - Girls Like You ft. Cardi B (Volume 2) (Official Music Video) 854753713
Maroon 5 - Payphone ft. Wiz Khalifa (Explicit) (Official Music Video) 831753300
Maroon 5 - Moves Like Jagger ft. Christina Aguilera (Official Music Video) 782619720
Maroon 5 - Don't Wanna Know (Official Music Video) 724780679
Maroon 5 - She Will Be Loved (Official Music Video) 705597149
Maroon 5 - What Lovers Do ft. SZA (Official Music Video) 667234206
Maroon 5 - Maps 490348095
Maroon 5 - Misery (Official Music Video) 353018576
Maroon 5 - Maps (Lyric Video) 346624146
Maroon 5 - This Love (Official Music Video) 278479916
Maroon 5 - Cold ft. Future (Official Music Video) 260612369
Maroon 5 - Won't Go Home Without You (Official Music Video) 250313312
Maroon 5 - Animals (Lyric Video) 205904355
Maroon 5 - Sunday Morning (Closed Captioned) 203608175
Maroon 5 - Wait (Official Music Video) 188986544

This is a very nice little script, this showcases an interesting use of the Youtube API. I hope this script will be very helpful to someone who is working on the same thing.

Get the Youtube channel ID # using this website.

https://commentpicker.com/youtube-channel-id.php

This bash script will get video information from a Youtube video ID.

#!/bin/sh
 
VIDEO=$1
 
yt-dlp --skip-download --print-json https://www.youtube.com/watch?v=$VIDEO | jq '{"date": .upload_date,"title": .title,"URL": .display_id,"duration": .duration, "like_count": .like_count, "resolution": .resolution, "fulltitle": .fulltitle}'

This is an easy way to get useful video information.

This is an example of the output this will give you.

┗━━━━━━━━━━┓ john@localhost ~/Documents
           ┗━━━━━━━━━━━━━╾ ╍▷ ./youtube.sh 0UXXK2ixB0M
{
  "date": "20230429",
  "title": "Picard is Famished",
  "URL": "0UXXK2ixB0M",
  "duration": 89,
  "like_count": 53,
  "resolution": "1920x1080",
  "fulltitle": "Picard is Famished"
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.