-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d301604
commit 42d86cd
Showing
4 changed files
with
108 additions
and
4 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
__all__ = ['strip_yt_link'] | ||
|
||
def _strip_yt_link(link): | ||
''' | ||
Parses a link to youtube to get the screen name and tweet id | ||
:input link: a link with the domain 'youtube.com' or 'youtu.be' | ||
:returns: a list of dictionaries with the original link, the video id, or the channel id | ||
''' | ||
|
||
dict_ = {} | ||
dict_['resolved_url'] = link | ||
|
||
|
||
if 'v=' in link: | ||
vid = link[link.index('v='):].strip('v=') | ||
if len(vid) > 11: | ||
vid = vid[:11] | ||
channel = None | ||
elif 'channel' in link: | ||
channel = link[link.index('channel'):].strip('channel/') | ||
if len(channel) > 24: | ||
channel = channel[:24] | ||
vid = None | ||
elif 'youtu.be' in link: | ||
vid = link.replace('https://', '').replace('http://', '').replace('youtu.be','').strip('/').strip('?a') | ||
channel = None | ||
else: | ||
vid = None | ||
channel = None | ||
|
||
dict_['video_id'] = vid | ||
dict_['channel'] = channel | ||
|
||
return dict_ | ||
|
||
def strip_yt_link(link): | ||
''' | ||
Parses a link to twitter to get the screen name and tweet id | ||
:input link: a link or list of links with the domain 'youtube.com' or 'youtu.be' | ||
:returns: a list of dictionaries with the original link, the video id, or the channel id | ||
''' | ||
|
||
if isinstance(link, str): | ||
return _strip_yt_link(link) | ||
|
||
elif isinstance(link, list): | ||
links_to_strip = list(set(link)) | ||
links = [_strip_yt_link(link) for link in links_to_strip] | ||
return links | ||
|
||
else: | ||
links_to_strip = list(set(list(link))) | ||
links = [_strip_yt_link(link) for link in links_to_strip] | ||
return links |