mirror of
https://github.com/jellyfin/jellyfin-kodi.git
synced 2024-12-25 02:06:09 +00:00
Merge pull request #387 from mcarlton00/i-like-big-libraries-and-i-cannot-lie
Optimize music sync for large libraries
This commit is contained in:
commit
c92b4a4e82
2 changed files with 68 additions and 76 deletions
|
@ -164,6 +164,25 @@ def get_episode_by_season(show_id, season_id):
|
||||||
yield items
|
yield items
|
||||||
|
|
||||||
|
|
||||||
|
def get_item_count(parent_id, item_type=None, params=None):
|
||||||
|
|
||||||
|
url = "Users/{UserId}/Items"
|
||||||
|
|
||||||
|
query_params = {
|
||||||
|
'ParentId': parent_id,
|
||||||
|
'IncludeItemTypes': item_type,
|
||||||
|
'EnableTotalRecordCount': True,
|
||||||
|
'LocationTypes': "FileSystem,Remote,Offline",
|
||||||
|
'Recursive': True,
|
||||||
|
'Limit': 1
|
||||||
|
}
|
||||||
|
if params:
|
||||||
|
query_params['params'].update(params)
|
||||||
|
|
||||||
|
result = _get(url, query_params)
|
||||||
|
|
||||||
|
return result.get('TotalRecordCount', 1)
|
||||||
|
|
||||||
def get_items(parent_id, item_type=None, basic=False, params=None):
|
def get_items(parent_id, item_type=None, basic=False, params=None):
|
||||||
|
|
||||||
query = {
|
query = {
|
||||||
|
@ -191,57 +210,24 @@ def get_items(parent_id, item_type=None, basic=False, params=None):
|
||||||
|
|
||||||
def get_artists(parent_id=None):
|
def get_artists(parent_id=None):
|
||||||
|
|
||||||
url = "Artists"
|
query = {
|
||||||
|
'url': 'Artists',
|
||||||
params = {
|
'params': {
|
||||||
'UserId': "{UserId}",
|
'UserId': "{UserId}",
|
||||||
'ParentId': parent_id,
|
'ParentId': parent_id,
|
||||||
'SortBy': "SortName",
|
'SortBy': "SortName",
|
||||||
'SortOrder': "Ascending",
|
'SortOrder': "Ascending",
|
||||||
'Fields': api.music_info(),
|
'Fields': api.music_info(),
|
||||||
'CollapseBoxSetItems': False,
|
'CollapseBoxSetItems': False,
|
||||||
'IsVirtualUnaired': False,
|
'IsVirtualUnaired': False,
|
||||||
'EnableTotalRecordCount': False,
|
'EnableTotalRecordCount': False,
|
||||||
'LocationTypes': "FileSystem,Remote,Offline",
|
'LocationTypes': "FileSystem,Remote,Offline",
|
||||||
'IsMissing': False,
|
'IsMissing': False,
|
||||||
'Recursive': True
|
'Recursive': True
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return _get(url, params)
|
for items in _get_items(query):
|
||||||
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
|
|
||||||
return _get(url, params)
|
|
||||||
|
|
||||||
|
|
||||||
def get_albums_by_artist(artist_id, basic=False):
|
|
||||||
|
|
||||||
params = {
|
|
||||||
'SortBy': "DateCreated",
|
|
||||||
'ArtistIds': artist_id
|
|
||||||
}
|
|
||||||
for items in get_items(None, "MusicAlbum", basic, params):
|
|
||||||
yield items
|
|
||||||
|
|
||||||
|
|
||||||
def get_songs_by_artist(artist_id, basic=False):
|
|
||||||
|
|
||||||
params = {
|
|
||||||
'SortBy': "DateCreated",
|
|
||||||
'ArtistIds': artist_id
|
|
||||||
}
|
|
||||||
for items in get_items(None, "Audio", basic, params):
|
|
||||||
yield items
|
yield items
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -417,37 +417,43 @@ class FullSync(object):
|
||||||
|
|
||||||
library_id = library['Id']
|
library_id = library['Id']
|
||||||
|
|
||||||
# Get all items in the library to process locally
|
total_items = server.get_item_count(library_id, 'MusicArtist,MusicAlbum,Audio')
|
||||||
artists_data = server.get_artists(library_id)
|
count = 0
|
||||||
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']
|
|
||||||
|
|
||||||
for index, artist in enumerate(artists):
|
'''
|
||||||
artist_name = artist.get('Name')
|
Music database syncing. Artists must be in the database
|
||||||
|
before albums, albums before songs. Pulls batches of items
|
||||||
|
in sizes of setting "Paging - Max items". 'artists',
|
||||||
|
'albums', and 'songs' are generators containing a dict of
|
||||||
|
api responses
|
||||||
|
'''
|
||||||
|
artists = server.get_artists(library_id)
|
||||||
|
for batch in artists:
|
||||||
|
for item in batch['Items']:
|
||||||
|
LOG.debug('Artist: {}'.format(item.get('Name')))
|
||||||
|
percent = int((float(count) / float(total_items)) * 100)
|
||||||
|
dialog.update(percent, message='Artist: {}'.format(item.get('Name')))
|
||||||
|
obj.artist(item)
|
||||||
|
count += 1
|
||||||
|
|
||||||
# Update percentage dialog
|
albums = server.get_items(library_id, item_type='MusicAlbum', params={'SortBy': 'AlbumArtist'})
|
||||||
percent = int((float(index) / float(num_artists)) * 100)
|
for batch in albums:
|
||||||
dialog.update(percent, heading="%s: %s" % (translate('addon_name'), library['Name']), message=artist_name)
|
for item in batch['Items']:
|
||||||
|
LOG.debug('Album: {}'.format(item.get('Name')))
|
||||||
|
percent = int((float(count) / float(total_items)) * 100)
|
||||||
|
dialog.update(percent, message='Album: {} - {}'.format(item.get('AlbumArtist', ''), item.get('Name')))
|
||||||
|
obj.album(item)
|
||||||
|
count += 1
|
||||||
|
|
||||||
# Add artist to database
|
songs = server.get_items(library_id, item_type='Audio', params={'SortBy': 'AlbumArtist'})
|
||||||
obj.artist(artist)
|
for batch in songs:
|
||||||
|
for item in batch['Items']:
|
||||||
|
LOG.debug('Song: {}'.format(item.get('Name')))
|
||||||
|
percent = int((float(count) / float(total_items)) * 100)
|
||||||
|
dialog.update(percent, message='Track: {} - {}'.format(item.get('AlbumArtist', ''), item.get('Name')))
|
||||||
|
obj.song(item)
|
||||||
|
count += 1
|
||||||
|
|
||||||
# Get all albums for each artist
|
|
||||||
artist_albums = [album for album in albums if artist_name in album.get('Artists')]
|
|
||||||
for album in artist_albums:
|
|
||||||
# Add album to database
|
|
||||||
obj.album(album)
|
|
||||||
album_id = album.get('Id')
|
|
||||||
# Get all songs in each album
|
|
||||||
album_songs = [song for song in songs if album_id == song.get('AlbumId')]
|
|
||||||
for song in album_songs:
|
|
||||||
dialog.update(percent,
|
|
||||||
message="%s/%s/%s" % (artist_name, album['Name'][:7], song['Name'][:7]))
|
|
||||||
# Add song to database
|
|
||||||
obj.song(song)
|
|
||||||
#
|
|
||||||
if self.update_library:
|
if self.update_library:
|
||||||
self.music_compare(library, obj, jellyfindb)
|
self.music_compare(library, obj, jellyfindb)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue