def search_from_playlist(self, url): id = url.split("=")[1] max_results = 50 log.debug("Retrieving data from Youtube...") youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) search_response = youtube.playlistItems().list( playlistId=id, part="id, status, snippet", maxResults=max_results).execute() self.results = [] ids = [] for search_result in search_response.get("items", []): if search_result["status"]["privacyStatus"] != "public": continue s = baseFile.song(self) s.title = search_result["snippet"]["title"] ids.append(search_result["snippet"]["resourceId"]["videoId"]) s.url = "https://www.youtube.com/watch?v=" + search_result[ "snippet"]["resourceId"]["videoId"] self.results.append(s) ssr = youtube.videos().list(id=",".join(ids), part="contentDetails", maxResults=50).execute() for i in range(len(self.results)): self.results[i].duration = seconds_to_string( isodate.parse_duration(ssr["items"][i]["contentDetails"] ["duration"]).total_seconds()) log.debug("{0} results found.".format(len(self.results)))
def search_from_url(self, url): log.debug("Getting download URL for {0}".format(url, )) if "playlist?list=" in url: return self.search_from_playlist(url) ydl = youtube_dl.YoutubeDL({ 'quiet': True, 'no_warnings': True, 'logger': log, 'format': 'bestaudio/best', 'outtmpl': u'%(id)s%(ext)s' }) with ydl: result = ydl.extract_info(url, download=False) if 'entries' in result: videos = result['entries'] else: videos = [result] for video in videos: s = baseFile.song(self) s.title = video["title"] s.url = video[ "webpage_url"] # Cannot use direct URL here cause Youtube URLS expire after a minute. s.duration = seconds_to_string(video["duration"]) self.results.append(s) log.debug("{0} results found.".format(len(self.results)))
def search(self, text, page=1): if text.startswith("https") or text.startswith("http"): return self.search_from_url(text) type = "video" max_results = 20 log.debug("Retrieving data from Youtube...") youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) search_response = youtube.search().list(q=text, part="id,snippet", maxResults=max_results, type=type).execute() self.results = [] ids = [] for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": s = baseFile.song(self) s.title = search_result["snippet"]["title"] ids.append(search_result["id"]["videoId"]) s.url = "https://www.youtube.com/watch?v=" + search_result[ "id"]["videoId"] self.results.append(s) ssr = youtube.videos().list(id=",".join(ids), part="contentDetails", maxResults=1).execute() for i in range(len(self.results)): self.results[i].duration = seconds_to_string( isodate.parse_duration(ssr["items"][i]["contentDetails"] ["duration"]).total_seconds()) log.debug("{0} results found.".format(len(self.results)))
def search(self, text, page=1): if text == "" or text == None: raise ValueError("Text must be passed and should not be blank.") if text.startswith("https") or text.startswith("http"): return self.search_from_url(text) type = "video" max_results = config.app["services"]["youtube"]["max_results"] log.debug("Retrieving data from Youtube...") ydl = youtube_dl.YoutubeDL({'quiet': True, 'ignore_errors': True, 'no_warnings': True, 'logger': log, 'format': 'bestaudio/best', 'outtmpl': u'%(id)s%(ext)s'}) with ydl: search_param = "ytsearch{}:{}".format(max_results, text) result = ydl.extract_info(search_param, download=False) self.results = [] for search_result in result["entries"]: s = base.song(self) s.title = search_result["title"] s.url = "https://www.youtube.com/watch?v="+search_result["id"] s.duration = seconds_to_string(search_result["duration"]) if search_result.get("track") != None: s.title = search_result["track"] if search_result.get("album") != None: s.album = search_result["album"] if search_result.get("artist") != None: s.artist = search_result["artist"] self.results.append(s) log.debug("{0} results found.".format(len(self.results)))
def search(self, text, page=1): if text == "" or text == None: raise ValueError("Text must be passed and should not be blank.") log.debug("Retrieving data from Tidal...") # Check for top:// protocol. if text.startswith("top://"): text = text.replace("top://", "") return self.search_for_top(text) fieldtypes = ["artist", "album", "playlist"] field = "track" for i in fieldtypes: if text.startswith(i + "://"): field = i text = text.replace(i + "://", "") log.debug("Searching for %s..." % (field)) search_response = self.session.search(value=text, field=field) self.results = [] if field == "track": data = search_response.tracks elif field == "artist": data = [] artist = search_response.artists[0].id if config.app["services"]["tidal"]["include_albums"]: albums = self.session.get_artist_albums(artist) for album in albums: tracks = self.session.get_album_tracks(album.id) for track in tracks: track.album = album data.append(track) if config.app["services"]["tidal"]["include_compilations"]: compilations = self.session.get_artist_albums_other(artist) for album in compilations: tracks = self.session.get_album_tracks(album.id) for track in tracks: data.append(track) if config.app["services"]["tidal"]["include_singles"]: singles = self.session.get_artist_albums_ep_singles(artist) for album in singles: tracks = self.session.get_album_tracks(album.id) for track in tracks: # track.single = True data.append(track) for search_result in data: s = base.song(self) s.title = search_result.name s.artist = search_result.artist.name s.duration = seconds_to_string(search_result.duration) s.url = search_result.id s.tracknumber = str(search_result.track_num) s.album = search_result.album.name if search_result.album.num_tracks == None: s.single = True s.info = search_result self.results.append(s) log.debug("{0} results found.".format(len(self.results)))
def search_for_top(self, artist): search_response = self.session.search(value=artist, field="artist") self.results = [] artist = search_response.artists[0].id results = self.session.get_artist_top_tracks(artist) for search_result in results: s = base.song(self) s.title = search_result.name s.artist = search_result.artist.name s.duration = seconds_to_string(search_result.duration) s.url = search_result.id s.tracknumber = str(search_result.track_num) s.album = search_result.album.name if search_result.album.num_tracks == None: s.single = True s.info = search_result self.results.append(s) log.debug("{0} results found.".format(len(self.results)))
def search_from_playlist(self, url): id = url.split("=")[1] max_results = 50 log.debug("Retrieving data from Youtube...") ydl = youtube_dl.YoutubeDL({'quiet': True, 'ignore_errors': True, 'no_warnings': True, 'logger': log, 'format': 'bestaudio/best', 'outtmpl': u'%(id)s%(ext)s'}) with ydl: result = ydl.extract_info(url, download=False) self.results = [] for search_result in result["entries"]: s = base.song(self) s.title = search_result["title"] s.url = "https://www.youtube.com/watch?v="+search_result["id"] s.duration = seconds_to_string(search_result["duration"]) if search_result.get("track") != None: s.title = search_result["track"] if search_result.get("album") != None: s.album = search_result["album"] if search_result.get("artist") != None: s.artist = search_result["artist"] self.results.append(s) log.debug("{0} results found.".format(len(self.results)))
def search(self, text): if text == "" or text == None: raise ValueError("Text must be passed and should not be blank.") log.debug("Retrieving data from vk...") self.results = [] results = self.get( "/vk/search", params=dict( text=text, maxresults=config.app["services"]["vk"]["max_results"])) if results.status_code != 200: return results = results.json() for search_result in results: s = base.song(self) s.title = search_result["title"] s.artist = search_result["artist"] s.duration = seconds_to_string(search_result["duration"]) s.url = search_result["url"] s.info = search_result self.results.append(s)