Skip to content

Instantly share code, notes, and snippets.

@MattEding
Created September 12, 2018 03:30
Show Gist options
  • Save MattEding/7b1645fbdfb61dc0a43b430664cf486a to your computer and use it in GitHub Desktop.
Save MattEding/7b1645fbdfb61dc0a43b430664cf486a to your computer and use it in GitHub Desktop.
Given a playlist URL of YouTube videos, calculate the running time length given a specified watching speed.
from collections import namedtuple
from bs4 import BeautifulSoup
import requests
Time = namedtuple('Time', 'hr min sec')
def playlist_duration(url, speed=1):
source = requests.get(url)
soup = BeautifulSoup(source.text, 'lxml')
videos = soup.find_all(class_='timestamp')
hours, minutes, seconds = 0, 0, 0
for video in videos:
time = video.text
m, s = map(int, time.split(':'))
minutes += m / speed
seconds += s / speed
if seconds >= 60:
seconds -= 60
minutes +=1
if minutes >= 60:
minutes -= 60
hours += 1
return Time(int(hours), int(minutes), int(seconds))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment