Esempio n. 1
0
 def artists(self, query=None):
     '''Returns a list of artists as dictionary objects.'''
     logger.debug("Generating artist list.")
     start_time = time()
     # Make a list to hold the results of our search
     result = []
     if query:
         # Clean up the search term
         query = utils.clean_text(query).split(' ')
         # Get all the artists from the database using the search view,
         # the key of which is a list consisting of [artist, album, title].
         # This is done so that the results will be alphabetized by artist
         # name and also so we have those fields to search.
         for artist in self.cache.view('artists/search'):
             # Create an object to append to our results
             entry = {
                 'id': artist['value'],
                 'name': artist['key'][0]
                 }
             if entry in result:
                 continue
             search_field =  utils.clean_text(';'.join(artist['key']))
             match = True
             for term in query:
                 if term not in search_field:
                     match = False
             if match:
                 result.append(entry)
     else:
         # Get all the artists from the database and append them to the
         # results list.
         for artist in self.cache.view('artists/all', group="true"):
             entry = {
                 'id': artist['value'],
                 'name': artist['key']
                 }
             result.append(entry)
     finish_time = time() - start_time
     logger.debug("Generated list of %d artists in %0.2f seconds." % (len(result), finish_time))
     return result
Esempio n. 2
0
 def albums(self, artists=None, query=None):
     '''Returns a list of albums as dictionary objects.'''
     logger.debug("Generating album list.")
     start_time = time()
     # Create a list to hold the albums we're going to return to the client
     result = []
     # If the client didn't give any arguments, get all the artists from the
     # database and append them to our results list.
     if not artists and not query:
         for album in self.cache.view('albums/all', group="true"):
             result.append({'id': album['value'], 'title': album['key']})
     if query and artists:
         # Clean up the search term
         query = utils.clean_text(query).split(' ')
         for artist in artists:
             # Get all the albums from the database using the search view
             for album in self.cache.view('albums/search', key=artist):
                 # Create an object that we can append to the results list
                 entry = {
                         'id': album['value']['album_hash'],
                         'title': album['value']['album']
                         }
                 if entry in result:
                     continue
                 match = True
                 for term in query:
                     if term not in utils.clean_text(album['value']['search_string']):
                         match = False
                 if match:
                     result.append(entry)
     if query and not artists:
         # Clean up the search term
         query = utils.clean_text(query).split(' ')
         # Get all the albums from the database using the search view
         for album in self.cache.view('albums/search'):
             # Create an object that we can append to the results list
             entry = {
                 'id': album['value']['album_hash'],
                 'title': album['value']['album']
                 }
             if entry in result:
                 continue
             # Clean up the search field and see if our search term is
             # in it. If it is, make sure it's not a duplicate result
             # and then append it to the results list.
             match = True
             for term in query:
                 if term not in utils.clean_text(album['value']['search_string']):
                     match = False
             if match:
                 result.append(entry)
     if artists and not query:
         # Client asked for albums by a specific artist(s), so get only
         # those albums from the database.
         for artist_hash in artists:
             for album in self.cache.view('albums/by_artist_id',
                                         key=artist_hash):
                 # Create an object to append to our results list
                 entry = {
                     'id': album['value']['album_hash'],
                     'title': album['value']['album']
                     }
                 # Get the artist ID
                 artist = album['key']
                 # Make sure this isn't a duplicate result
                 if entry not in result:
                     result.append(entry)
     finish_time = time() - start_time
     logger.debug("Generated list of %d albums in %0.2f seconds." % (len(result), finish_time))
     return sorted(result, key=itemgetter('title'))
Esempio n. 3
0
 def songs(self, artists=None, albums=None, query=None, suggest=None):
     '''Returns a list of songs as dictionary objects.'''
     logger.debug("Generating song list.")
     start_time = time()
     # Create a list to hold songs we're going to return to the client
     result = []
     if suggest:
         if query:
             query = utils.clean_text(query)
             for song in self.cache.view('search/suggestion', group="true"):
                 if utils.clean_text(song['key']).startswith(query):
                     result.append(song['key'])
                     if len(result) == 10:
                         break
         else:
             for song in self.cache.view('search/suggestion', group="true"):
                 result.append(song['key'])
         finish_time = time() - start_time
         logger.debug("Generated list of %d songs in %0.2f seconds." % (len(result), finish_time))
         return result
     # If the client didn't supply any arguments, we just get all the songs
     # from the database
     if not query and not artists and not albums:
         for song in self.cache.view('songs/all'):
             result.append(song['value'])
     if artists:
         # The client only wants songs by a specific artist(s)
         for artist in artists:
             for song in self.cache.view('songs/by_artist_id', key=artist):
                 result.append(song['value'])
     if albums:
         # The client only wants songs from a specific album(s). We need
         # to see if we've already got some songs we just need to filter or
         # whether we need to grab some from the database.
         if result:
             temp_result = []
             # Look through our existing results and add the ones that are
             # from the specified album(s) to our temporary list.
             for song in result:
                 if song['album_hash'] in albums:
                     temp_result.append(song)
             # Replace the existing results with our refined ones
             result = temp_result
         # Since our results are empty, make sure that's not because the
         # search just didn't have any results, in which case we want to
         # return an empty list.
         elif not artists:
             # Grab the songs from the specified album(s) from the database
             for album in albums:
                 for song in self.cache.view('songs/by_album_id', key=album):
                     result.append(song['value'])
     if query:
         # Clean up the search string
         query = utils.clean_text(query).split(' ')
         # Figure out whether we already have some songs we need to filter
         # or if we need to grab them all from the database.
         if not (albums or artists):
             # Get all the songs from the database.
             for song in self.cache.view('songs/all'):
                 # Create a searchable string by joining together the artist,
                 # album and title fields and cleaning them up.
                 search_field = utils.clean_text(";".join([song['key'][0],
                                           song['key'][1], song['key'][3]]))
                 match = True
                 for term in query:
                     if term not in search_field:
                         match = False
                 if match:
                     result.append(song['value'])
         elif result:
             temp_result = []
             # Look through our existing results for any songs that contain
             # our search term.
             for song in result:
                 search_field = utils.clean_text(";".join([song['artist'],
                                             song['album'], song['title']]))
                 match = True
                 for term in query:
                     if term not in search_field:
                         match = False
                 if match:
                     temp_result.append(song)
             # Replace the existing results with the filtered ones.
             result = temp_result
     finish_time = time() - start_time
     logger.debug("Generated list of %d songs in %0.2f seconds." % (len(result), finish_time))
     return sorted(result, key=itemgetter('artist', 'album',
                                            'tracknumber'))