From 6be4ffca3de455210c86bc7f1805ba5dad33b555 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Wed, 4 Oct 2017 17:34:00 -0500 Subject: [PATCH] Offline mode for views --- resources/lib/embydb_functions.py | 20 +++++++++++++++++- resources/lib/librarysync.py | 6 ++++++ resources/lib/service_entry.py | 3 +++ resources/lib/userclient.py | 4 ++-- resources/lib/views.py | 35 +++++++++++++++++++++++++++---- 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/resources/lib/embydb_functions.py b/resources/lib/embydb_functions.py index 9c581428..27546f8c 100644 --- a/resources/lib/embydb_functions.py +++ b/resources/lib/embydb_functions.py @@ -122,9 +122,27 @@ class Embydb_Functions(): views.append({ 'id': row[0], - 'name': row[1] + 'name': row[1], + 'mediatype': mediatype }) + if mediatype in ('tvshows', 'movies'): + query = ' '.join(( + "SELECT view_id, view_name", + "FROM view", + "WHERE media_type = ?" + )) + + self.embycursor.execute(query, ("mixed",)) + rows = self.embycursor.fetchall() + for row in rows: + views.append({ + + 'id': row[0], + 'name': row[1], + 'mediatype': "mixed" + }) + return views def getView_byName(self, tagname): diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index 5166e6a3..97470035 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -372,6 +372,12 @@ class LibrarySync(threading.Thread): # Compare views, assign correct tags to items views.Views(cursor_emby, cursor_video).maintain() + def offline_mode_views(self): + + with database.DatabaseConn('emby') as cursor_emby: + with database.DatabaseConn() as cursor_video: + views.Views(cursor_emby, cursor_video).offline_mode() + def movies(self, embycursor, kodicursor, pdialog): # Get movies from emby diff --git a/resources/lib/service_entry.py b/resources/lib/service_entry.py index ac52ce21..74ca2f5a 100644 --- a/resources/lib/service_entry.py +++ b/resources/lib/service_entry.py @@ -79,6 +79,9 @@ class Service(object): # Clear video nodes properties VideoNodes().clearProperties() + # assume offline mode + log.info("Loading existing views...") + librarysync.LibrarySync().offline_mode_views() # Set the minimum database version window('emby_minDBVersion', value="1.1.63") diff --git a/resources/lib/userclient.py b/resources/lib/userclient.py index c9ab7547..878dda15 100644 --- a/resources/lib/userclient.py +++ b/resources/lib/userclient.py @@ -77,7 +77,7 @@ class UserClient(threading.Thread): def verify_server(self): try: - url = "%s/emby/system/info/public?format=json" % self.get_server() + url = "%s/emby/system/info/public?format=json" % self.get_server() # tried system/public but can't get reliable response from the server? self.doutils.downloadUrl(url, authenticate=False) return True except Exception as error: @@ -147,7 +147,7 @@ class UserClient(threading.Thread): user = self.doutils.downloadUrl("{server}/emby/Users/{UserId}?format=json") settings('username', value=user['Name']) self._user = user - + window('emby.userinfo.json', user) if "PrimaryImageTag" in self._user: window('EmbyUserImage', value=artwork.Artwork().get_user_artwork(self._user['Id'], 'Primary')) diff --git a/resources/lib/views.py b/resources/lib/views.py index 9f3e62d5..bc120c66 100644 --- a/resources/lib/views.py +++ b/resources/lib/views.py @@ -151,10 +151,14 @@ class Views(object): self.emby_db.addView(view_id, view_name, view_type, tag_id, group_series) def is_grouped_series(self, view_id, view_type): - try: - return self.emby.get_view_options(view_id)['EnableAutomaticSeriesGrouping'] if view_type == "tvshows" else None - except Exception as error: # Currently admin only api entrypoint - log.error(error) + + if window('emby.userinfo.json')['Policy']['IsAdministrator']: + try: + return self.emby.get_view_options(view_id)['EnableAutomaticSeriesGrouping'] if view_type == "tvshows" else None + except Exception as error: # Currently admin only api entrypoint + log.error(error) + return None + else: return None def compare_view(self, media_type, view_id, view_name, view_type): @@ -344,6 +348,29 @@ class Views(object): self.video_nodes.singleNode(index, tag, media_type, view_type) self.total_nodes += 1 + def offline_mode(self): + # Just reads from the db and populate views that way + # total nodes for window properties + self.video_nodes.clearProperties() + + for media_type in ('movies', 'tvshows', 'musicvideos', 'homevideos', 'music', 'photos'): + + self.nodes = list() # Prevent duplicate for nodes of the same type + self.playlists = list() # Prevent duplicate for playlists of the same type + + views = self.emby_db.getView_byType(media_type) + for view in views: + + try: # Make sure the view is in sorted views before proceeding + self.sorted_views.index(view['name']) + except ValueError: + self.sorted_views.append(view['name']) + + self.add_playlist_node(media_type, view['id'], view['name'], view['mediatype']) + + self.add_single_nodes() + window('Emby.nodes.total', str(self.total_nodes)) + class Playlist(object):