Optimize network calls for music syncing

This commit is contained in:
Matt 2020-08-02 00:25:08 -04:00
parent da21f49928
commit 5f34222712
2 changed files with 81 additions and 44 deletions

View file

@ -189,16 +189,16 @@ def get_items(parent_id, item_type=None, basic=False, params=None):
yield items yield items
def get_artists(parent_id=None, basic=False, params=None, server_id=None): def get_artists(parent_id=None):
query = { url = "Artists"
'url': "Artists",
'params': { params = {
'UserId': "{UserId}", 'UserId': "{UserId}",
'ParentId': parent_id, 'ParentId': parent_id,
'SortBy': "SortName", 'SortBy': "SortName",
'SortOrder': "Ascending", 'SortOrder': "Ascending",
'Fields': api.basic_info() if basic else api.music_info(), 'Fields': api.music_info(),
'CollapseBoxSetItems': False, 'CollapseBoxSetItems': False,
'IsVirtualUnaired': False, 'IsVirtualUnaired': False,
'EnableTotalRecordCount': False, 'EnableTotalRecordCount': False,
@ -206,14 +206,23 @@ def get_artists(parent_id=None, basic=False, params=None, server_id=None):
'IsMissing': False, 'IsMissing': False,
'Recursive': True 'Recursive': True
} }
return _get(url, params)
def get_library_items(library_id, item_type):
url = "Users/{UserId}/Items"
params = {
'ParentId': library_id,
'IncludeItemTypes': item_type,
'SortBy': "SortName",
'SortOrder': "Ascending",
'Fields': api.info(),
'Recursive': True,
} }
if params: return _get(url, params)
query['params'].update(params)
for items in _get_items(query, server_id):
yield items
def get_albums_by_artist(artist_id, basic=False): def get_albums_by_artist(artist_id, basic=False):

View file

@ -407,36 +407,64 @@ class FullSync(object):
with Database('jellyfin') as jellyfindb: with Database('jellyfin') as jellyfindb:
obj = Music(self.server, jellyfindb, musicdb, self.direct_path) obj = Music(self.server, jellyfindb, musicdb, self.direct_path)
for items in server.get_artists(library['Id'], False, self.sync['RestorePoint'].get('params')): library_id = library['Id']
artists_data = server.get_artists(library_id)
artists = artists_data['Items']
num_artists = artists_data['TotalRecordCount']
albums = server.get_library_items(library_id, 'MusicAlbum')['Items']
songs = server.get_library_items(library_id, 'Audio')['Items']
self.sync['RestorePoint'] = items['RestorePoint'] for index, artist in enumerate(artists):
start_index = items['RestorePoint']['params']['StartIndex'] artist_name = artist.get('Name')
for index, artist in enumerate(items['Items']): percent = int((float(index) / float(num_artists)) * 100)
message = artist_name
percent = int((float(start_index + index) / float(items['TotalRecordCount'])) * 100)
message = artist['Name']
dialog.update(percent, heading="%s: %s" % (translate('addon_name'), library['Name']), message=message) dialog.update(percent, heading="%s: %s" % (translate('addon_name'), library['Name']), message=message)
obj.artist(artist, library=library) obj.artist(artist, library=library)
artist_albums = [ album for album in albums if artist_name in album.get('Artists') ]
for albums in server.get_albums_by_artist(artist['Id']): for album in artist_albums:
for album in albums['Items']:
obj.album(album) obj.album(album)
album_id = album.get('Id')
for songs in server.get_items(album['Id'], "Audio"): album_songs = [ song for song in songs if album_id == song.get('AlbumId') ]
for song in songs['Items']: for song in album_songs:
dialog.update(percent, dialog.update(percent,
message="%s/%s/%s" % (message, album['Name'][:7], song['Name'][:7])) message="%s/%s/%s" % (message, album['Name'][:7], song['Name'][:7]))
obj.song(song) obj.song(song)
for songs in server.get_songs_by_artist(artist['Id']):
for song in songs['Items']:
dialog.update(percent, message="%s/%s" % (message, song['Name']))
obj.song(song)
# for items in server.get_artists(library['Id'], False, self.sync['RestorePoint'].get('params')):
#
# self.sync['RestorePoint'] = items['RestorePoint']
# start_index = items['RestorePoint']['params']['StartIndex']
#
# for index, artist in enumerate(items['Items']):
#
# percent = int((float(start_index + index) / float(items['TotalRecordCount'])) * 100)
# message = artist['Name']
# dialog.update(percent, heading="%s: %s" % (translate('addon_name'), library['Name']), message=message)
# obj.artist(artist, library=library)
#
# import web_pdb; web_pdb.set_trace()
# for albums in server.get_albums_by_artist(artist['Id']):
#
# for album in albums['Items']:
# obj.album(album)
#
# for songs in server.get_items(album['Id'], "Audio"):
# for song in songs['Items']:
#
# dialog.update(percent,
# message="%s/%s/%s" % (message, album['Name'][:7], song['Name'][:7]))
# obj.song(song)
#
# for songs in server.get_songs_by_artist(artist['Id']):
# for song in songs['Items']:
#
# dialog.update(percent, message="%s/%s" % (message, song['Name']))
# obj.song(song)
#
if self.update_library: if self.update_library:
self.music_compare(library, obj, jellyfindb) self.music_compare(library, obj, jellyfindb)