def playlists(playlist_id, **kwargs): source = Source() if kwargs: # POST action = kwargs.get('action', None) if not action: raise ValueError('no "action" parameter') if action not in ['enqueue', 'play_source']: raise ValueError('Unknown action {}'.format(action)) playlist_id = kwargs['playlist'] if 'playlist' in kwargs else kwargs.get('source', None) if playlist_id is None: raise ValueError('no "source" parameter') playlist_id = to_int(playlist_id, 'playlist id must be a number') try: if action == 'enqueue': count = source.enqueue_by_id(playlist_id) log.info('Enqueued source {}, {} entries'.format(playlist_id, count)) return {'result': 'BAD'} if count is None else {'count': count, 'result': 'OK'} # elif action == 'play_source': log.info('Playing source {}'.format(playlist_id)) return {'result': 'OK'} if source.play_source(playlist_id) else {'result': 'BAD'} except IndexError: raise ValueError('there is no playlist with id {}'.format(playlist_id)) elif playlist_id is None: # GET return {'playlists': source.get_playlists()} else: try: return source.get_playlist(playlist_id) except IndexError: raise ValueError('there is no playlist with id {}'.format(playlist_id))
def get_source(self, source_index): source_index = to_int(source_index, 'source_index parameter must be an int') sources = self.get_sources() for source in enumerate(sources): if source.index == source_index: log.debug('Returning source %s' % source) return source return None
def song(song_id, **kwargs): handler = Song() song = handler.find_by_id(song_id) if song: rating = to_int(kwargs.get('rating', None), 'rating must be a number') if not rating is None: log.info('Setting rating for song %d=%d', song_id, rating) handler.set_rating(song, rating) log.debug('Returning song %s', song) return song
def parse_player_args(player_arguments): log.debug('parsing player arguments {}'.format(player_arguments)) kwargs = {} if 'time' in player_arguments: kwargs['time'] = to_int(player_arguments['time'], 'time must be a number') if 'entry_id' in player_arguments: entry_id = player_arguments['entry_id'] if ',' in entry_id: entries = [int(entry) for entry in entry_id.split(',')] else: entries = int(entry_id) kwargs['entry_id'] = entries if 'volume' in player_arguments: kwargs['volume'] = to_float(player_arguments['volume'], 'volume must be number') return kwargs
def set_rating(self, entry_id, rating): """Sets the provided rating to the given entry id, int 0 to 5""" rating = to_int(rating, 'Rating parameter must be an int') entry = self.db.entry_lookup_by_id(entry_id) if not entry is None: self.db.entry_set(entry, RB.RhythmDBPropType.RATING, rating)