diff --git a/resources/lib/app/libsync.py b/resources/lib/app/libsync.py index cc0e9da9..88c1237f 100644 --- a/resources/lib/app/libsync.py +++ b/resources/lib/app/libsync.py @@ -48,8 +48,7 @@ class Sync(object): self.remapSMBphotoOrg = utils.settings('remapSMBphotoOrg') self.remapSMBphotoNew = utils.settings('remapSMBphotoNew') # Shall we replace custom user ratings with the number of versions available? - self.indicate_media_versions = True \ - if utils.settings('indicate_media_versions') == "true" else False + self.indicate_media_versions = utils.settings('indicate_media_versions') == "true" # Will sync movie trailer differently: either play trailer directly or show # all the Plex extras for the user to choose self.show_extras_instead_of_playing_trailer = utils.settings('showExtrasInsteadOfTrailer') == 'true' diff --git a/resources/lib/itemtypes/common.py b/resources/lib/itemtypes/common.py index f2db7b27..d02ad0ba 100644 --- a/resources/lib/itemtypes/common.py +++ b/resources/lib/itemtypes/common.py @@ -95,30 +95,6 @@ class ItemBase(object): kodi_id, kodi_type) - def update_userdata(self, xml_element, plex_type): - """ - Updates the Kodi watched state of the item from PMS. Also retrieves - Plex resume points for movies in progress. - """ - api = API(xml_element) - # Get key and db entry on the Kodi db side - db_item = self.plexdb.item_by_id(api.plex_id(), plex_type) - if not db_item: - LOG.error('Item not yet synced: %s', xml_element.attrib) - return - # Grab the user's viewcount, resume points etc. from PMS' answer - userdata = api.userdata() - # Write to Kodi DB - self.kodidb.set_resume(db_item['kodi_fileid'], - userdata['Resume'], - userdata['Runtime'], - userdata['PlayCount'], - userdata['LastPlayedDate'], - plex_type) - self.kodidb.update_userrating(db_item['kodi_id'], - db_item['kodi_type'], - userdata['UserRating']) - def update_playstate(self, mark_played, view_count, resume, duration, kodi_fileid, lastViewedAt, plex_type): """ diff --git a/resources/lib/itemtypes/movies.py b/resources/lib/itemtypes/movies.py index 1a49da2f..7f52ae2f 100644 --- a/resources/lib/itemtypes/movies.py +++ b/resources/lib/itemtypes/movies.py @@ -126,7 +126,7 @@ class Movie(ItemBase): rating, api.votecount()) if api.provider('imdb') is not None: - uniqueid = self.kodidb.add_uniqueid() + uniqueid = self.kodidb.add_uniqueid_id() self.kodidb.add_uniqueid(uniqueid, kodi_id, v.KODI_TYPE_MOVIE, @@ -253,3 +253,27 @@ class Movie(ItemBase): self.kodidb.remove_uniqueid(kodi_id, kodi_type) self.kodidb.remove_ratings(kodi_id, kodi_type) LOG.debug('Deleted movie %s from kodi database', plex_id) + + def update_userdata(self, xml_element, plex_type): + """ + Updates the Kodi watched state of the item from PMS. Also retrieves + Plex resume points for movies in progress. + """ + api = API(xml_element) + # Get key and db entry on the Kodi db side + db_item = self.plexdb.item_by_id(api.plex_id(), plex_type) + if not db_item: + LOG.error('Item not yet synced: %s', xml_element.attrib) + return + # Grab the user's viewcount, resume points etc. from PMS' answer + userdata = api.userdata() + # Write to Kodi DB + self.kodidb.set_resume(db_item['kodi_fileid'], + userdata['Resume'], + userdata['Runtime'], + userdata['PlayCount'], + userdata['LastPlayedDate'], + plex_type) + self.kodidb.update_userrating(db_item['kodi_id'], + db_item['kodi_type'], + userdata['UserRating']) diff --git a/resources/lib/itemtypes/music.py b/resources/lib/itemtypes/music.py index 41d83081..63c1e514 100644 --- a/resources/lib/itemtypes/music.py +++ b/resources/lib/itemtypes/music.py @@ -29,6 +29,30 @@ class MusicMixin(object): artcursor=self.artcursor) return self + def update_userdata(self, xml_element, plex_type): + """ + Updates the Kodi watched state of the item from PMS. Also retrieves + Plex resume points for movies in progress. + """ + api = API(xml_element) + # Get key and db entry on the Kodi db side + db_item = self.plexdb.item_by_id(api.plex_id(), plex_type) + if not db_item: + LOG.error('Item not yet synced: %s', xml_element.attrib) + return + # Grab the user's viewcount, resume points etc. from PMS' answer + userdata = api.userdata() + self.kodidb.update_userrating(db_item['kodi_id'], + db_item['kodi_type'], + userdata['UserRating']) + if plex_type == v.PLEX_TYPE_SONG: + self.kodidb.set_resume(db_item['kodi_fileid'], + userdata['Resume'], + userdata['Runtime'], + userdata['PlayCount'], + userdata['LastPlayedDate'], + plex_type) + def remove(self, plex_id, plex_type=None): """ Remove the entire music object, including all associated entries from diff --git a/resources/lib/itemtypes/tvshows.py b/resources/lib/itemtypes/tvshows.py index 283438cd..216541f8 100644 --- a/resources/lib/itemtypes/tvshows.py +++ b/resources/lib/itemtypes/tvshows.py @@ -11,6 +11,30 @@ LOG = getLogger('PLEX.tvshows') class TvShowMixin(object): + def update_userdata(self, xml_element, plex_type): + """ + Updates the Kodi watched state of the item from PMS. Also retrieves + Plex resume points for movies in progress. + """ + api = API(xml_element) + # Get key and db entry on the Kodi db side + db_item = self.plexdb.item_by_id(api.plex_id(), plex_type) + if not db_item: + LOG.error('Item not yet synced: %s', xml_element.attrib) + return + # Grab the user's viewcount, resume points etc. from PMS' answer + userdata = api.userdata() + self.kodidb.update_userrating(db_item['kodi_id'], + db_item['kodi_type'], + userdata['UserRating']) + if plex_type == v.PLEX_TYPE_EPISODE: + self.kodidb.set_resume(db_item['kodi_fileid'], + userdata['Resume'], + userdata['Runtime'], + userdata['PlayCount'], + userdata['LastPlayedDate'], + plex_type) + def remove(self, plex_id, plex_type=None): """ Remove the entire TV shows object (show, season or episode) including @@ -167,7 +191,6 @@ class Show(ItemBase, TvShowMixin): api.audience_rating(), api.votecount(), rating_id) - # update new uniqueid Kodi 17 if api.provider('tvdb') is not None: uniqueid = self.kodidb.get_uniqueid(kodi_id, v.KODI_TYPE_SHOW) @@ -209,9 +232,8 @@ class Show(ItemBase, TvShowMixin): "default", api.audience_rating(), api.votecount()) - if api.provider('tvdb') is not None: - uniqueid = self.kodidb.get_uniqueid(kodi_id, - v.KODI_TYPE_SHOW) + if api.provider('tvdb'): + uniqueid = self.kodidb.add_uniqueid_id() self.kodidb.add_uniqueid(uniqueid, kodi_id, v.KODI_TYPE_SHOW, @@ -423,14 +445,17 @@ class Episode(ItemBase, TvShowMixin): userdata['Rating'], api.votecount(), ratingid) - # update new uniqueid Kodi 17 - uniqueid = self.kodidb.get_uniqueid(kodi_id, - v.KODI_TYPE_EPISODE) - self.kodidb.update_uniqueid(kodi_id, - v.KODI_TYPE_EPISODE, - api.provider('tvdb'), - "tvdb", - uniqueid) + if api.provider('tvdb'): + uniqueid = self.kodidb.get_uniqueid(kodi_id, + v.KODI_TYPE_EPISODE) + self.kodidb.update_uniqueid(kodi_id, + v.KODI_TYPE_EPISODE, + api.provider('tvdb'), + "tvdb", + uniqueid) + else: + self.kodidb.remove_uniqueid(kodi_id, v.KODI_TYPE_EPISODE) + uniqueid = -1 self.kodidb.modify_people(kodi_id, v.KODI_TYPE_EPISODE, api.people_list()) @@ -471,14 +496,13 @@ class Episode(ItemBase, TvShowMixin): "default", userdata['Rating'], api.votecount()) - # add new uniqueid Kodi 17 - uniqueid = self.kodidb.get_uniqueid(kodi_id, - v.KODI_TYPE_EPISODE) - self.kodidb.add_uniqueid(uniqueid, - kodi_id, - v.KODI_TYPE_EPISODE, - api.provider('tvdb'), - "tvdb") + if api.provider('tvdb'): + uniqueid = self.kodidb.add_uniqueid_id() + self.kodidb.add_uniqueid(uniqueid, + kodi_id, + v.KODI_TYPE_EPISODE, + api.provider('tvdb'), + "tvdb") self.kodidb.add_people(kodi_id, v.KODI_TYPE_EPISODE, api.people_list()) diff --git a/resources/lib/kodi_db/music.py b/resources/lib/kodi_db/music.py index 1f598022..9315f95a 100644 --- a/resources/lib/kodi_db/music.py +++ b/resources/lib/kodi_db/music.py @@ -468,6 +468,17 @@ class KodiMusicDB(common.KodiDBBase): VALUES (?, ?, ?, ?, ?) ''', (song_id, album_id, track_no, track_title, runtime)) + def update_userrating(self, kodi_id, kodi_type, userrating): + """ + Updates userrating for songs and albums + """ + if kodi_type == v.KODI_TYPE_SONG: + identifier = 'idSong' + elif kodi_type == v.KODI_TYPE_ALBUM: + identifier = 'idAlbum' + self.cursor.execute('''UPDATE %s SET userrating = ? WHERE ? = ?''' % kodi_type, + (userrating, identifier, kodi_id)) + def remove_albuminfosong(self, kodi_id): """ Kodi 17 only diff --git a/resources/lib/kodi_db/video.py b/resources/lib/kodi_db/video.py index b7277ea8..f8e6f141 100644 --- a/resources/lib/kodi_db/video.py +++ b/resources/lib/kodi_db/video.py @@ -755,7 +755,7 @@ class KodiVideoDB(common.KodiDBBase): VALUES (?, ?, ?, ?, ?) ''', (args)) - def add_uniqueid(self): + def add_uniqueid_id(self): self.cursor.execute('SELECT COALESCE(MAX(uniqueid_id), 0) FROM uniqueid') return self.cursor.fetchone()[0] + 1 @@ -768,7 +768,7 @@ class KodiVideoDB(common.KodiDBBase): try: return self.cursor.fetchone()[0] except TypeError: - return self.add_uniqueid() + return self.add_uniqueid_id() def update_uniqueid(self, *args): """ @@ -994,10 +994,16 @@ class KodiVideoDB(common.KodiDBBase): Updates userrating """ if kodi_type == v.KODI_TYPE_MOVIE: + table = kodi_type identifier = 'idMovie' elif kodi_type == v.KODI_TYPE_EPISODE: + table = kodi_type identifier = 'idEpisode' - elif kodi_type == v.KODI_TYPE_SONG: - identifier = 'idSong' - self.cursor.execute('''UPDATE %s SET userrating = ? WHERE ? = ?''' % kodi_type, + elif kodi_type == v.KODI_TYPE_SEASON: + table = 'seasons' + identifier = 'idSeason' + elif kodi_type == v.KODI_TYPE_SHOW: + table = kodi_type + identifier = 'idShow' + self.cursor.execute('''UPDATE %s SET userrating = ? WHERE ? = ?''' % table, (userrating, identifier, kodi_id)) diff --git a/resources/lib/library_sync/full_sync.py b/resources/lib/library_sync/full_sync.py index c7f117e5..25aacdac 100644 --- a/resources/lib/library_sync/full_sync.py +++ b/resources/lib/library_sync/full_sync.py @@ -136,7 +136,8 @@ class FullSync(common.libsync_mixin): # Ensure that the DB connection is closed to commit the # changes above - avoids "Item not yet synced" error self.queue.join() - self.process_playstate(iterator) + if self.plex_type != v.PLEX_TYPE_ARTIST: + self.process_playstate(iterator) except RuntimeError: LOG.error('Could not process playstate for section %s', section) successful = False diff --git a/resources/lib/plex_api.py b/resources/lib/plex_api.py index 1f1bb190..f125a061 100644 --- a/resources/lib/plex_api.py +++ b/resources/lib/plex_api.py @@ -303,7 +303,8 @@ class API(object): except (KeyError, ValueError): last_played = None - if app.SYNC.indicate_media_versions is True: + if (app.SYNC.indicate_media_versions is True and + self.plex_type() in (v.PLEX_TYPE_MOVIE, v.PLEX_TYPE_EPISODE)): userrating = 0 for _ in self.item.findall('./Media'): userrating += 1