diff --git a/data/interfaces/default/library.html b/data/interfaces/default/library.html index ee7162b9..3333b53c 100644 --- a/data/interfaces/default/library.html +++ b/data/interfaces/default/library.html @@ -37,7 +37,9 @@ DOCUMENTATION :: END % if data:
+ % if data['library_art']:
+ % endif
@@ -52,7 +54,7 @@ DOCUMENTATION :: END
% else: -
Unable to retrieve data from database. +
No stats to show.

% endif \ No newline at end of file diff --git a/plexpy/libraries.py b/plexpy/libraries.py index 771f3c7a..4b6f47f5 100644 --- a/plexpy/libraries.py +++ b/plexpy/libraries.py @@ -540,51 +540,27 @@ class Libraries(object): def get_details(self, section_id=None): from plexpy import pmsconnect - monitor_db = database.MonitorDatabase() + default_return = {'section_id': None, + 'section_name': 'Local', + 'section_type': '', + 'library_thumb': common.DEFAULT_COVER_THUMB, + 'library_art': '', + 'count': 0, + 'parent_count': 0, + 'child_count': 0, + 'do_notify': 0, + 'do_notify_created': 0, + 'keep_history': 0 + } - try: - if section_id: - query = 'SELECT section_id, section_name, section_type, count, parent_count, child_count, ' \ - 'thumb AS library_thumb, custom_thumb_url AS custom_thumb, art, ' \ - 'do_notify, do_notify_created, keep_history ' \ - 'FROM library_sections ' \ - 'WHERE section_id = ? ' - result = monitor_db.select(query, args=[section_id]) - else: - result = [] - except Exception as e: - logger.warn(u"PlexPy Libraries :: Unable to execute database query for get_details: %s." % e) - result = [] + if not section_id: + return default_return - if result: - library_details = {} - for item in result: - if item['custom_thumb'] and item['custom_thumb'] != item['library_thumb']: - library_thumb = item['custom_thumb'] - elif item['library_thumb']: - library_thumb = item['library_thumb'] - else: - library_thumb = common.DEFAULT_COVER_THUMB + def get_library_details(section_id=section_id): + monitor_db = database.MonitorDatabase() - library_details = {'section_id': item['section_id'], - 'section_name': item['section_name'], - 'section_type': item['section_type'], - 'library_thumb': library_thumb, - 'library_art': item['art'], - 'count': item['count'], - 'parent_count': item['parent_count'], - 'child_count': item['child_count'], - 'do_notify': item['do_notify'], - 'do_notify_created': item['do_notify_created'], - 'keep_history': item['keep_history'] - } - return library_details - else: - logger.warn(u"PlexPy Libraries :: Unable to retrieve library from local database. Requesting library list refresh.") - # Let's first refresh the libraries list to make sure the library isn't newly added and not in the db yet - pmsconnect.refresh_libraries() try: - if section_id: + if str(section_id).isdigit(): query = 'SELECT section_id, section_name, section_type, count, parent_count, child_count, ' \ 'thumb AS library_thumb, custom_thumb_url AS custom_thumb, art, ' \ 'do_notify, do_notify_created, keep_history ' \ @@ -593,12 +569,12 @@ class Libraries(object): result = monitor_db.select(query, args=[section_id]) else: result = [] - except: + except Exception as e: logger.warn(u"PlexPy Libraries :: Unable to execute database query for get_details: %s." % e) result = [] + library_details = {} if result: - library_details = {} for item in result: if item['custom_thumb'] and item['custom_thumb'] != item['library_thumb']: library_thumb = item['custom_thumb'] @@ -619,22 +595,28 @@ class Libraries(object): 'do_notify_created': item['do_notify_created'], 'keep_history': item['keep_history'] } + return library_details + + library_details = get_library_details(section_id=section_id) + + if library_details: + return library_details + + else: + logger.warn(u"PlexPy Libraries :: Unable to retrieve library from local database. Requesting library list refresh.") + # Let's first refresh the libraries list to make sure the library isn't newly added and not in the db yet + pmsconnect.refresh_libraries() + + library_details = get_library_details(section_id=section_id) + + if library_details: return library_details + else: + logger.warn(u"PlexPy Users :: Unable to retrieve user from local database. Returning 'Local' library.") # If there is no library data we must return something - # Use "Local" user to retain compatibility with PlexWatch database value - return {'section_id': None, - 'section_name': 'Local', - 'section_type': '', - 'library_thumb': common.DEFAULT_COVER_THUMB, - 'library_art': '', - 'count': 0, - 'parent_count': 0, - 'child_count': 0, - 'do_notify': 0, - 'do_notify_created': 0, - 'keep_history': 0 - } + # Use "Local" library to retain compatibility with PlexWatch database value + return default_return def get_watch_time_stats(self, section_id=None): monitor_db = database.MonitorDatabase() diff --git a/plexpy/users.py b/plexpy/users.py index 8f5c5d9c..e4e7ee3c 100644 --- a/plexpy/users.py +++ b/plexpy/users.py @@ -245,58 +245,24 @@ class Users(object): def get_details(self, user_id=None, user=None): from plexpy import plextv - monitor_db = database.MonitorDatabase() - - try: - if str(user_id).isdigit(): - query = 'SELECT user_id, username, friendly_name, thumb AS user_thumb, custom_avatar_url AS custom_thumb, ' \ - 'email, is_home_user, is_allow_sync, is_restricted, do_notify, keep_history ' \ - 'FROM users ' \ - 'WHERE user_id = ? ' - result = monitor_db.select(query, args=[user_id]) - elif user: - query = 'SELECT user_id, username, friendly_name, thumb AS user_thumb, custom_avatar_url AS custom_thumb, ' \ - 'email, is_home_user, is_allow_sync, is_restricted, do_notify, keep_history ' \ - 'FROM users ' \ - 'WHERE username = ? ' - result = monitor_db.select(query, args=[user]) - else: - result = [] - except Exception as e: - logger.warn(u"PlexPy Users :: Unable to execute database query for get_details: %s." % e) - result = [] + default_return = {'user_id': None, + 'username': 'Local', + 'friendly_name': 'Local', + 'user_thumb': common.DEFAULT_USER_THUMB, + 'email': '', + 'is_home_user': 0, + 'is_allow_sync': 0, + 'is_restricted': 0, + 'do_notify': 0, + 'keep_history': 0 + } - if result: - user_details = {} - for item in result: - if item['friendly_name']: - friendly_name = item['friendly_name'] - else: - friendly_name = item['username'] + if not user_id and not user: + return default_return - if item['custom_thumb'] and item['custom_thumb'] != item['user_thumb']: - user_thumb = item['custom_thumb'] - elif item['user_thumb']: - user_thumb = item['user_thumb'] - else: - user_thumb = common.DEFAULT_USER_THUMB + def get_user_details(user_id=user_id, user=user): + monitor_db = database.MonitorDatabase() - user_details = {'user_id': item['user_id'], - 'username': item['username'], - 'friendly_name': friendly_name, - 'user_thumb': user_thumb, - 'email': item['email'], - 'is_home_user': item['is_home_user'], - 'is_allow_sync': item['is_allow_sync'], - 'is_restricted': item['is_restricted'], - 'do_notify': item['do_notify'], - 'keep_history': item['keep_history'] - } - return user_details - else: - logger.warn(u"PlexPy Users :: Unable to retrieve user from local database. Requesting user list refresh.") - # Let's first refresh the user list to make sure the user isn't newly added and not in the db yet - plextv.refresh_users() try: if str(user_id).isdigit(): query = 'SELECT user_id, username, friendly_name, thumb AS user_thumb, custom_avatar_url AS custom_thumb, ' \ @@ -316,8 +282,8 @@ class Users(object): logger.warn(u"PlexPy Users :: Unable to execute database query for get_details: %s." % e) result = [] + user_details = {} if result: - user_details = {} for item in result: if item['friendly_name']: friendly_name = item['friendly_name'] @@ -342,21 +308,28 @@ class Users(object): 'do_notify': item['do_notify'], 'keep_history': item['keep_history'] } + return user_details + + user_details = get_user_details(user_id=user_id, user=user) + + if user_details: + return user_details + + else: + logger.warn(u"PlexPy Users :: Unable to retrieve user from local database. Requesting user list refresh.") + # Let's first refresh the user list to make sure the user isn't newly added and not in the db yet + plextv.refresh_users() + + user_details = get_user_details(user_id=user_id, user=user) + + if user_details: return user_details + else: + logger.warn(u"PlexPy Users :: Unable to retrieve user from local database. Returning 'Local' user.") # If there is no user data we must return something # Use "Local" user to retain compatibility with PlexWatch database value - return {'user_id': None, - 'username': 'Local', - 'friendly_name': 'Local', - 'user_thumb': common.DEFAULT_USER_THUMB, - 'email': '', - 'is_home_user': 0, - 'is_allow_sync': 0, - 'is_restricted': 0, - 'do_notify': 0, - 'keep_history': 0 - } + return default_return def get_watch_time_stats(self, user_id=None): monitor_db = database.MonitorDatabase() diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 64e80e74..aad13469 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -363,9 +363,11 @@ class WebInterface(object): @cherrypy.expose def get_library_watch_time_stats(self, section_id=None, **kwargs): - - library_data = libraries.Libraries() - result = library_data.get_watch_time_stats(section_id=section_id) + if section_id: + library_data = libraries.Libraries() + result = library_data.get_watch_time_stats(section_id=section_id) + else: + result = None if result: return serve_template(templatename="user_watch_time_stats.html", data=result, title="Watch Stats") @@ -375,9 +377,11 @@ class WebInterface(object): @cherrypy.expose def get_library_user_stats(self, section_id=None, **kwargs): - - library_data = libraries.Libraries() - result = library_data.get_user_stats(section_id=section_id) + if section_id: + library_data = libraries.Libraries() + result = library_data.get_user_stats(section_id=section_id) + else: + result = None if result: return serve_template(templatename="library_user_stats.html", data=result, title="Player Stats") @@ -387,9 +391,11 @@ class WebInterface(object): @cherrypy.expose def get_library_recently_watched(self, section_id=None, limit='10', **kwargs): - - library_data = libraries.Libraries() - result = library_data.get_recently_watched(section_id=section_id, limit=limit) + if section_id: + library_data = libraries.Libraries() + result = library_data.get_recently_watched(section_id=section_id, limit=limit) + else: + result = None if result: return serve_template(templatename="user_recently_watched.html", data=result, title="Recently Watched") @@ -399,9 +405,11 @@ class WebInterface(object): @cherrypy.expose def get_library_recently_added(self, section_id=None, limit='10', **kwargs): - - pms_connect = pmsconnect.PmsConnect() - result = pms_connect.get_recently_added_details(section_id=section_id, count=limit) + if section_id: + pms_connect = pmsconnect.PmsConnect() + result = pms_connect.get_recently_added_details(section_id=section_id, count=limit) + else: + result = None if result: return serve_template(templatename="library_recently_added.html", data=result['recently_added'], title="Recently Added") @@ -628,9 +636,11 @@ class WebInterface(object): @cherrypy.expose def get_user_watch_time_stats(self, user=None, user_id=None, **kwargs): - - user_data = users.Users() - result = user_data.get_watch_time_stats(user_id=user_id) + if users_id or user: + user_data = users.Users() + result = user_data.get_watch_time_stats(user_id=user_id) + else: + result = None if result: return serve_template(templatename="user_watch_time_stats.html", data=result, title="Watch Stats") @@ -640,9 +650,11 @@ class WebInterface(object): @cherrypy.expose def get_user_player_stats(self, user=None, user_id=None, **kwargs): - - user_data = users.Users() - result = user_data.get_player_stats(user_id=user_id) + if users_id or user: + user_data = users.Users() + result = user_data.get_player_stats(user_id=user_id) + else: + result = None if result: return serve_template(templatename="user_player_stats.html", data=result, title="Player Stats") @@ -652,9 +664,11 @@ class WebInterface(object): @cherrypy.expose def get_user_recently_watched(self, user=None, user_id=None, limit='10', **kwargs): - - user_data = users.Users() - result = user_data.get_recently_watched(user_id=user_id, limit=limit) + if users_id or user: + user_data = users.Users() + result = user_data.get_recently_watched(user_id=user_id, limit=limit) + else: + result = None if result: return serve_template(templatename="user_recently_watched.html", data=result, title="Recently Watched")