Created
September 12, 2018 03:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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