def getArtist(self, **kwargs): rest = SubsonicREST(kwargs) success, response = rest.check_login() if not success: return response artistId = kwargs.get('id') if artistId: artist = request.env['oomusic.artist'].browse([int(artistId)]) if not artist.exists(): return rest.make_error(code='70', message='Artist not found') else: return rest.make_error( code='10', message='Required int parameter "id" is not present') root = etree.Element('subsonic-response', status='ok', version=rest.version_server) xml_artist = rest.make_ArtistID3(artist) root.append(xml_artist) for album in artist.album_ids: xml_album = rest.make_AlbumID3(album) xml_artist.append(xml_album) return rest.make_response(root)
def getAlbum(self, **kwargs): rest = SubsonicREST(kwargs) success, response = rest.check_login() if not success: return response albumId = kwargs.get('id') if albumId: album = request.env['oomusic.album'].browse([int(albumId)]) if not album.exists(): return rest.make_error(code='70', message='Album not found') else: return rest.make_error( code='10', message='Required int parameter "id" is not present') root = etree.Element('subsonic-response', status='ok', version=rest.version_server) xml_album = rest.make_AlbumID3(album) root.append(xml_album) for track in album.track_ids: xml_song = rest.make_Child_track(track, tag_name='song') xml_album.append(xml_song) return rest.make_response(root)
def getStarred2(self, **kwargs): rest = SubsonicREST(kwargs) success, response = rest.check_login() if not success: return response ArtistObj = request.env['oomusic.artist'] AlbumObj = request.env['oomusic.album'] TrackObj = request.env['oomusic.track'] folderId = kwargs.get('musicFolderId') if folderId: folder = request.env['oomusic.folder'].browse([int(folderId)]) if not folder.exists(): return rest.make_error(code='70', message='Folder not found') domain = [('id', 'child_of', int(folderId))] if folderId else [] root = etree.Element('subsonic-response', status='ok', version=rest.version_server) xml_starred_list = rest.make_listSongs('starred2') root.append(xml_starred_list) artists = ArtistObj.search(domain + [('star', '=', '1')]) albums = AlbumObj.search(domain + [('star', '=', '1')]) tracks = TrackObj.search(domain + [('star', '=', '1')]) for artist in artists: xml_artist = rest.make_ArtistID3(artist) xml_starred_list.append(xml_artist) for album in albums: xml_album = rest.make_AlbumID3(album) xml_starred_list.append(xml_album) for track in tracks: xml_song = rest.make_Child_track(track, tag_name='song') xml_starred_list.append(xml_song) return rest.make_response(root)
def getAlbumList2(self, **kwargs): rest = SubsonicREST(kwargs) success, response = rest.check_login() if not success: return response AlbumObj = request.env['oomusic.album'] list_type_accepted = [ 'random', 'newest', 'frequent', 'recent', 'starred', 'alphabeticalByName', 'alphabeticalByArtist', 'byYear', 'byGenre' ] list_type = kwargs.get('type') if not list_type: return rest.make_error( code='10', message='Required string parameter "type" is not present') if list_type not in list_type_accepted: return rest.make_error(code='0', message='Invalid list type: %s' % list_type) fromYear = kwargs.get('fromYear') toYear = kwargs.get('toYear') if list_type == 'byYear' and (not fromYear or not toYear): return rest.make_error( code='10', message= 'Required string parameter "fromYear" or "toYear" is not present' ) genre = kwargs.get('genre') if list_type == 'byGenre' and not genre: return rest.make_error( code='10', message='Required string parameter "genre" is not present') folderId = kwargs.get('musicFolderId') if folderId: folder = request.env['oomusic.folder'].browse([int(folderId)]) if not folder.exists(): return rest.make_error(code='70', message='Folder not found') size = min(int(kwargs.get('size', 10)), 500) offset = int(kwargs.get('offset', 0)) # Build domain domain = [('folder_id', 'child_of', int(folderId))] if folderId else [] if list_type == 'byYear': if int(fromYear) > int(toYear): domain += [('year', '>=', toYear), ('year', '<=', fromYear)] else: domain += [('year', '>=', fromYear), ('year', '<=', toYear)] elif list_type == 'byGenre': domain += [('genre_id.name', 'ilike', genre)] elif list_type == 'starred': domain += [('star', '=', '1')] # Search for albums if list_type == 'random': album_ids = AlbumObj.search(domain).ids if album_ids: album_ids = random.sample(album_ids, min(size, len(album_ids))) albums = AlbumObj.browse(album_ids) elif list_type == 'newest': albums = AlbumObj.search(domain, order='create_date desc') elif list_type == 'recent': q_select = ' SELECT album_id FROM oomusic_track ' q_where = ' WHERE user_id = %s and last_play is not NULL ' % ( request.env.user.id) if folderId: q_where += ' and root_folder_id = %s ' % (folderId) q_order = ' ORDER BY last_play desc;' query = q_select + q_where + q_order request.env.cr.execute(query) res = request.env.cr.fetchall() album_ids = _uniquify_list([r[0] for r in res if r[0] is not None]) albums = AlbumObj.browse(album_ids) elif list_type == 'frequent': q_select = ' SELECT album_id FROM oomusic_track ' q_where = ' WHERE user_id = %s and play_count > 0 ' % ( request.env.user.id) if folderId: q_where += ' and root_folder_id = %s ' % (folderId) q_order = ' ORDER BY play_count desc;' query = q_select + q_where + q_order request.env.cr.execute(query) res = request.env.cr.fetchall() album_ids = _uniquify_list([r[0] for r in res if r[0] is not None]) albums = AlbumObj.browse(album_ids) elif list_type == 'alphabeticalByName': albums = AlbumObj.search(domain, order='name') elif list_type == 'alphabeticalByArtist': albums = AlbumObj.search(domain, order='artist_id') elif list_type == 'byYear': if int(fromYear) > int(toYear): albums = AlbumObj.search(domain, order='year desc') else: albums = AlbumObj.search(domain, order='year') else: albums = AlbumObj.search(domain) root = etree.Element('subsonic-response', status='ok', version=rest.version_server) xml_album_list = rest.make_AlbumList2() root.append(xml_album_list) if albums: min_val = min(offset, len(albums)) max_val = min_val + size for album in albums[min_val:max_val]: xml_album = rest.make_AlbumID3(album) xml_album_list.append(xml_album) return rest.make_response(root)
def search3(self, **kwargs): rest = SubsonicREST(kwargs) success, response = rest.check_login() if not success: return response query = kwargs.get('query') if not query: return rest.make_error( code='10', message='Required str parameter "query" is not present') artistCount = min(int(kwargs.get('artistCount', 20)), 500) artistOffset = int(kwargs.get('artistOffset', 0)) albumCount = min(int(kwargs.get('albumCount', 20)), 500) albumOffset = int(kwargs.get('albumOffset', 0)) songCount = min(int(kwargs.get('songCount', 20)), 500) songOffset = int(kwargs.get('songOffset', 0)) folderId = kwargs.get('musicFolderId') if folderId: folder = request.env['oomusic.folder'].browse([int(folderId)]) if not folder.exists(): return rest.make_error(code='70', message='Folder not found') domain = [('folder_id', 'child_of', int(folderId))] if folderId else [] artists = request.env['oomusic.artist'].search(domain + [('name', 'ilike', query)]) albums = request.env['oomusic.album'].search(domain + [('name', 'ilike', query)]) tracks = request.env['oomusic.track'].search(domain + [('name', 'ilike', query)]) root = etree.Element('subsonic-response', status='ok', version=rest.version_server) xml_search = rest.make_SearchResult2(tag_name='searchResult3') root.append(xml_search) if artists: min_val = min(artistOffset, len(artists)) max_val = min_val + artistCount for artist in artists[min_val:max_val]: xml_artist = rest.make_ArtistID3(artist) xml_search.append(xml_artist) if albums: min_val = min(albumOffset, len(albums)) max_val = min_val + albumCount for album in albums[min_val:max_val]: xml_album = rest.make_AlbumID3(album) xml_search.append(xml_album) if tracks: min_val = min(songOffset, len(tracks)) max_val = min_val + songCount for track in tracks[min_val:max_val]: xml_song = rest.make_Child_track(track, tag_name='song') xml_search.append(xml_song) return rest.make_response(root)