02 Calculate duration of videos

re.compile(), search() group(1)
timedelta.total_seconds()

01

Use the timedelta class from the datetime library. Pass in the hours,minutes, and seconds and add the method total_seconds(). Return value to video_seconds variable

import re
import os
from datetime import timedelta
from googleapiclient.discovery import build import json # Get new api key this one is fake api_key = 'AIzaSyBycc_eAr1_P3NWDzR08IHkCA3_44wKEDM' youtube = build('youtube','v3',developerKey=api_key) channel_id = 'UCCezIgC97PvUuR4_gbFUs5g' playlist_id = 'PL-osiE80TeTsWmV9i9c58mdDCSskIFdDS' pListItem_response = youtube.playlistItems().list( part='contentDetails', playlistId=playlist_id ) response = pListItem_response.execute() vid_ids = [] for data in response['items']: vid_ids.append(data['contentDetails']['videoId']) videos_response = youtube.videos().list( part='contentDetails', id=','.join(vid_ids) ) response = videos_response.execute()
        
hours_pattern = re.compile(r'(\d+)H')
mins_pattern = re.compile (r'(\d+)M')
secs_pattern = re.compile (r'(\d+)S')
os.system('clear') for vid in response['items']: duration = vid['contentDetails']['duration'] hours = hours_pattern.search(duration) mins = mins_pattern.search(duration) secs = secs_pattern.search(duration)
    hours = int(hours. group(1)) if hours else 0
    mins = int(mins. group(1)) if mins else 0
    secs = int(secs. group(1)) if secs else 0
    video_seconds = timedelta(
        hours = hours,
        minutes=mins,
        seconds=secs
    ).total_seconds()
    print(video_seconds)