Created
June 3, 2020 07:38
-
-
Save hridaydutta123/a673db5e0145bc571d28c71035cb9405 to your computer and use it in GitHub Desktop.
Convert YouTube API duration to seconds
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
import re | |
def YTDurationToSeconds(duration): | |
match = re.match('PT(\d+H)?(\d+M)?(\d+S)?', duration).groups() | |
hours = _js_parseInt(match[0]) if match[0] else 0 | |
minutes = _js_parseInt(match[1]) if match[1] else 0 | |
seconds = _js_parseInt(match[2]) if match[2] else 0 | |
return hours * 3600 + minutes * 60 + seconds | |
# js-like parseInt | |
# https://gist.github.com/douglasmiranda/2174255 | |
def _js_parseInt(string): | |
return int(''.join([x for x in string if x.isdigit()])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment