def search(query, max_results=10, _type=_SEARCH_TYPE.TRACK): try: current_offset = 0 results = {} stop = False limit = _MAX_RESULTS_SEARCH_REQUEST if limit > max_results: limit = max_results while not stop: response = _getClient().search(query, limit=limit, offset=current_offset, type=_type.value) items_key = '{0}s'.format(_type.value) if len(response[items_key]['items']) > 0: current_offset = current_offset + \ len(response[items_key]['items']) for i in response[items_key]['items']: results[i['id']] = i if not response[items_key]['next'] \ or len(results) >= max_results: stop = True except spotipy.client.SpotifyException: log.error('Unable to search', exc_info=True) return None return list(results.values())
def get_settings(): log.info("Retrieving settings") try: settings = { 'settings': config.get_config_dict() } return jsonify(settings) except Exception: log.error("Error Retrieving settings", exc_info=True) abort(500, 'Error Retrieving settings')
def info(): if not request.json or 'url' not in request.json: abort(400, 'Error getting playlist info: url obligatory') try: url = request.json['url'] log.info("Get playlist info[%s]", url) info = spotdl.fetch_info(url) return jsonify(info) except Exception: log.error("Error getting playlist info", exc_info=True) abort(500, 'Error getting playlist info')
def put_settings(): if not request.json or 'settings' not in request.json: abort(400, 'Error Saving settings: settings obligatory') log.info("Saving settings") try: config.save_config(request.json['settings']) config.init_config() return jsonify({'status': 'OK'}) except Exception: log.error("Error Saving settings", exc_info=True) abort(500, 'Error Saving settings')
def run(self): log.info("Init Downloader") try: self.init_date = time.localtime() self.download() self.end_date = time.localtime() log.info("Finished Downloader") except Exception as e: log.error("Error in Downloader", exc_info=True) self.end_date = time.localtime() raise Exception("Error in Downloader")
def search(): if not request.json or 'query' not in request.json: abort(400, 'Error searching: query obligatory') try: query = request.json['query'] log.info("Searching[%s]", query) items = spotdl.search(query, max_results_per_type=int( const.config.search_max_results)) return jsonify(items) except Exception: log.error("Error searching", exc_info=True) abort(500, 'Error searching')
def youtube(): if 'url' not in request.args: abort(400, 'Error getting playlist redirecting to youtube: url obligatory') try: url = request.args['url'] log.info("Redirecting to youtube[%s]", url) yt_url = spotdl.fetch_yt_url(url) return redirect(yt_url, code=302) except Exception: log.error("Error redirecting to youtube", exc_info=True) abort(500, 'Error redirecting to youtube')
def fetch_playlist(playlist): splits = internals.get_splits(playlist) try: username = splits[-3] except IndexError: # Wrong format, in either case log.error('The provided playlist URL is not in a recognized format!') sys.exit(10) playlist_id = splits[-1] try: results = spotify.user_playlist(username, playlist_id, fields='tracks,next,name') except spotipy.client.SpotifyException: log.error('Unable to find playlist') log.info('Make sure the playlist is set to publicly visible and then try again') sys.exit(11) return results
def _fetch_playlist(playlist): splits = internals.get_splits(playlist) try: username = splits[-3] except IndexError: # Wrong format, in either case log.error('The provided playlist URL is not in a recognized format!') return None playlist_id = splits[-1] try: results = _getClient().user_playlist( username, playlist_id, fields='tracks,next,name,images,artists,description') except spotipy.client.SpotifyException: log.error('Unable to find playlist', exc_info=True) log.info('Make sure the playlist is set to ' + 'publicly visible and then try again') return None return results
def run(self): log.info("Init Downloader") self.status = "Init Downloader" try: self.init_date = time.localtime() self.download() self.end_date = time.localtime() log.info("Finished Downloader") self.status = "Finished Downloader" except Exception as e: log.error("Error in Downloader", exc_info=True) self.status = "Error in Downloader: {0}".format(str(e)) self.end_date = time.localtime() raise Exception("Error in Downloader")
def download_history(): log.info("Retrieving download_history") try: items = list(map(lambda d: { 'url': d[0], 'name': d[1].get_name(), 'status': d[1].get_status().__dict__, 'init_date': d[1].get_init_date(format="%H:%M:%S"), 'end_date': d[1].get_end_date(format="%H:%M:%S"), }, current_downloads.items())) response = { 'items': items } return jsonify(response) except Exception: log.error("Error retrieving download_history", exc_info=True) abort(500, 'Error retrieving download_history')
def post_download(): if not request.json or 'url' not in request.json: abort(400, 'Error downloading playlist info: url obligatory') try: url = request.json['url'] log.info("Downloading url[%s]", url) if url not in current_downloads: downloader = SpotifyDownloader(url) downloader.start() current_downloads[url] = downloader return jsonify({'status': 'OK'}) else: return jsonify({'status': 'ALREADY_ADDED'}) except Exception: log.error("Error downloading playlist info", exc_info=True) abort(400, 'Error downloading playlist info')
def play_and_record(track_uri="spotify:track:1L1dpImK36DoZPr7rxy0hJ", filename="foo", songname="bar"): log.debug("entered playing and record") client.playback.stop() client.tracklist.set_repeat(False) client.tracklist.clear() client.tracklist.set_single(True) client.mixer.set_mute(False) client.mixer.set_volume(100) client.tracklist.add(uris=[track_uri]) listener.filename = filename listener.songname = songname try: # Activate cache if caching_enabled: log.debug('caching start') client.playback.play() timeout = time.time() + 20 while listener.get_playback_stopped(): if time.time() > timeout: raise RuntimeWarning('Could not play track for caching {}'.format(songname)) time.sleep(0.05) time.sleep(3) log.debug('stopping cache play') client.playback.stop() timeout = time.time() + 20 while not listener.get_playback_stopped(): if time.time() > timeout: raise RuntimeWarning('Caching record took too long {}'.format(songname)) time.sleep(0.05) log.debug('caching end') # start recording listener.reset_play_time() listener.start_recording() # make sure thread has started timeout = time.time() + 15 while not listener.record_thread.is_alive(): if time.time() > timeout: raise RuntimeError('Record thread still not alive track {}'.format(songname)) time.sleep(3) client.playback.play() timeout = time.time() + 10 while listener.get_playback_stopped(): if time.time() > timeout: raise RuntimeWarning('Could not play track {}'.format(songname)) time.sleep(0.05) # wait for end timeout = time.time() + 60 * 20 while not listener.get_playback_stopped(): if time.time() > timeout: raise RuntimeWarning('Record took longer than 20 minutes track {}'.format(songname)) time.sleep(0.1) # make sure thread has stopped timeout = time.time() + 15 while listener.record_thread.is_alive(): if time.time() > timeout: raise RuntimeError('Record thread still alive track {}'.format(songname)) except RuntimeWarning as e: log.error(str(e)) finally: client.playback.stop() return listener.play_time