Skip to content

Instantly share code, notes, and snippets.

@iandark
Created March 16, 2024 06:50
Show Gist options
  • Select an option

  • Save iandark/13f19a177a0e0372f620d6f5b095c268 to your computer and use it in GitHub Desktop.

Select an option

Save iandark/13f19a177a0e0372f620d6f5b095c268 to your computer and use it in GitHub Desktop.
Python script providing a sorted list of videos from a specific YouTube channel based on video duration
from googleapiclient.discovery import build
import re
youtube = build('youtube', 'v3', developerKey='API_KEY')
channel_id = 'CHANNEL_ID'
def get_uploads_playlist_id(channel_id):
response = youtube.channels().list(
part='contentDetails',
id=channel_id
).execute()
uploads_playlist_id = response['items'][0]['contentDetails']['relatedPlaylists']['uploads']
return uploads_playlist_id
def format_duration(seconds):
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
return f"{hours:02}:{minutes:02}:{seconds:02}"
def get_videos_details(playlist_id):
videos = []
nextPageToken = None
while True:
response = youtube.playlistItems().list(
part='contentDetails',
playlistId=playlist_id,
maxResults=50,
pageToken=nextPageToken
).execute()
video_ids = [item['contentDetails']['videoId'] for item in response['items']]
video_response = youtube.videos().list(
part='snippet,contentDetails',
id=','.join(video_ids)
).execute()
for item in video_response['items']:
video_id = item['id']
title = item['snippet']['title']
duration = item['contentDetails']['duration']
seconds = iso8601_duration_as_seconds(duration)
formatted_duration = format_duration(seconds)
link = f"https://www.youtube.com/watch?v={video_id}"
videos.append((title, link, formatted_duration, seconds))
nextPageToken = response.get('nextPageToken')
if not nextPageToken:
break
return videos
def iso8601_duration_as_seconds(duration):
match = re.match('PT(\d+H)?(\d+M)?(\d+S)?', duration).groups()
hours = int(match[0][:-1]) if match[0] else 0
minutes = int(match[1][:-1]) if match[1] else 0
seconds = int(match[2][:-1]) if match[2] else 0
return hours * 3600 + minutes * 60 + seconds
def main():
uploads_playlist_id = get_uploads_playlist_id(channel_id)
videos = get_videos_details(uploads_playlist_id)
sorted_videos = sorted(videos, key=lambda x: x[3], reverse=True)
for index, (title, link, formatted_duration, _) in enumerate(sorted_videos, start=1):
print(f"{index}. {title}\nLink: {link}\nDuração: {formatted_duration}\n")
print(f"Total videos returned: {len(sorted_videos)}")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment