-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug: fix wrong formats in the yt video endpoint response (#119)
- Loading branch information
1 parent
50156c2
commit db3142d
Showing
4 changed files
with
74 additions
and
32 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from urllib.parse import urlparse, parse_qs | ||
|
||
|
||
def extract_video_id(url): | ||
# eg: | ||
# - http://youtu.be/SA2iWivDJiE | ||
# - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu | ||
# - http://www.youtube.com/embed/SA2iWivDJiE | ||
# - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US | ||
query = urlparse(url) | ||
if query.hostname == "youtu.be": | ||
return query.path[1:] | ||
if query.hostname in {"www.youtube.com", "youtube.com"}: | ||
if query.path == "/watch": | ||
return parse_qs(query.query)["v"][0] | ||
if query.path[:7] == "/embed/": | ||
return query.path.split("/")[2] | ||
if query.path[:3] == "/v/": | ||
return query.path.split("/")[2] | ||
return None | ||
|
||
|
||
def yt_duration_to_seconds(duration): | ||
# eg: P1W2DT6H21M32S | ||
week = 0 | ||
day = 0 | ||
hour = 0 | ||
min = 0 | ||
sec = 0 | ||
|
||
duration = duration.lower() | ||
|
||
value = "" | ||
for c in duration: | ||
if c.isdigit(): | ||
value += c | ||
continue | ||
|
||
elif c == "p": | ||
pass | ||
elif c == "t": | ||
pass | ||
elif c == "w": | ||
week = int(value) * 604800 | ||
elif c == "d": | ||
day = int(value) * 86400 | ||
elif c == "h": | ||
hour = int(value) * 3600 | ||
elif c == "m": | ||
min = int(value) * 60 | ||
elif c == "s": | ||
sec = int(value) | ||
|
||
value = "" | ||
|
||
return week + day + hour + min + sec |