Spaces:
Running
Running
more link
#2
by Hev832 - opened
27 unmodified lines282930313233343536373839404142434445464748495051525354555657585960616263646527 unmodified linesoutput_dir = os.path.join(BASE_DIR, 'song_output')def get_youtube_video_id(url, ignore_playlist=True):"""Examples:http://youtu.be/SA2iWivDJiEhttp://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feeduhttp://www.youtube.com/embed/SA2iWivDJiEhttp://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US"""query = urlparse(url)if query.hostname == 'youtu.be':if query.path[1:] == 'watch':return query.query[2:]return query.path[1:]if query.hostname in {'www.youtube.com', 'youtube.com', 'music.youtube.com'}:if not ignore_playlist:# use case: get playlist id not current video in playlistwith suppress(KeyError):return parse_qs(query.query)['list'][0]if query.path == '/watch':return parse_qs(query.query)['v'][0]if query.path[:7] == '/watch/':return query.path.split('/')[1]if query.path[:7] == '/embed/':return query.path.split('/')[2]if query.path[:3] == '/v/':return query.path.split('/')[2]# returns None for invalid YouTube urlreturn Nonedef yt_download(link):ydl_opts = {'format': 'bestaudio',27 unmodified lines282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858627 unmodified linesoutput_dir = os.path.join(BASE_DIR, 'song_output')def get_youtube_video_id(url, ignore_playlist=True):"""Extracts video ID from YouTube, YouTube Shorts, Spotify, and SoundCloud URLs.Examples:YouTube:http://youtu.be/SA2iWivDJiEhttp://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feeduhttp://www.youtube.com/embed/SA2iWivDJiEhttp://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_USYouTube Shorts:https://www.youtube.com/shorts/abc123xyzSpotify:https://open.spotify.com/track/5nTtCOCds6I0PHMNtqelasSoundCloud:https://soundcloud.com/artist/trackname"""query = urlparse(url)if query.hostname in {'youtu.be', 'www.youtube.com', 'youtube.com', 'music.youtube.com'}:# YouTube and YouTube Shortsif query.hostname == 'youtu.be':return query.path[1:]if query.path == '/watch':return parse_qs(query.query).get('v', [None])[0]if query.path.startswith('/embed/'):return query.path.split('/')[2]if query.path.startswith('/v/'):return query.path.split('/')[2]if query.path.startswith('/shorts/'):return query.path.split('/')[2]elif query.hostname == 'open.spotify.com':# Spotifyif query.path.startswith('/track/'):return query.path.split('/')[2]elif query.hostname == 'soundcloud.com':# SoundCloud# Example SoundCloud URL: https://soundcloud.com/artist/tracknamereturn query.path[1:] # returns "artist/trackname"# Return None for invalid or unsupported URLsreturn Nonedef yt_download(link):ydl_opts = {'format': 'bestaudio',
src/main.py+37-16