re.compile()
, search()
group(1)
timedelta.total_seconds()
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 osfrom 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()from datetime import timedelta
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_pattern = re.compile(r'(\d+)H') mins_pattern = re.compile (r'(\d+)M') secs_pattern = re.compile (r'(\d+)S')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 0video_seconds = timedelta( hours = hours, minutes=mins, seconds=secs ).total_seconds() print(video_seconds)