From 89f581f63ec4579bfd341f6ec740320830cd10bf Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Thu, 3 Dec 2015 18:40:18 -0800 Subject: [PATCH 1/3] Only schedule job for recently added or monitor remote access if setting enabled --- data/interfaces/default/settings.html | 5 +++++ plexpy/__init__.py | 18 ++++++++++++---- plexpy/activity_pinger.py | 31 +++++++++++++-------------- plexpy/config.py | 1 + plexpy/webserve.py | 11 +++++++++- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index fd3dd1c5..e8d3dfa4 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -499,6 +499,11 @@ available_notification_agents = sorted(notifiers.available_notification_agents() Enable Music Notifications +
+ +

Current Activity Notifications

diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 88fb73f7..bbd234da 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -285,10 +285,20 @@ def initialize_scheduler(): hours=12, minutes=0, seconds=0) schedule_job(pmsconnect.get_server_friendly_name, 'Refresh Plex Server Name', hours=12, minutes=0, seconds=0) - schedule_job(activity_pinger.check_recently_added, 'Check for recently added items', - hours=0, minutes=0, seconds=seconds) - schedule_job(activity_pinger.check_server_response, 'Check for server response', - hours=0, minutes=0, seconds=seconds) + + if CONFIG.NOTIFY_RECENTLY_ADDED: + schedule_job(activity_pinger.check_recently_added, 'Check for recently added items', + hours=0, minutes=0, seconds=seconds) + else: + schedule_job(activity_pinger.check_recently_added, 'Check for recently added items', + hours=0, minutes=0, seconds=0) + + if CONFIG.MONITOR_REMOTE_ACCESS: + schedule_job(activity_pinger.check_server_response, 'Check for server response', + hours=0, minutes=0, seconds=seconds) + else: + schedule_job(activity_pinger.check_server_response, 'Check for server response', + hours=0, minutes=0, seconds=0) # If we're not using websockets then fall back to polling if not CONFIG.MONITORING_USE_WEBSOCKET or POLLING_FAILOVER: diff --git a/plexpy/activity_pinger.py b/plexpy/activity_pinger.py index 4063a7dd..28a340e7 100644 --- a/plexpy/activity_pinger.py +++ b/plexpy/activity_pinger.py @@ -33,7 +33,11 @@ def check_active_sessions(ws_request=False): monitor_process = activity_processor.ActivityProcessor() # logger.debug(u"PlexPy Monitor :: Checking for active streams.") + global int_ping_count + if session_list: + int_ping_count = 0 + media_container = session_list['sessions'] # Check our temp table for what we must do with the new streams @@ -165,6 +169,16 @@ def check_active_sessions(ws_request=False): else: logger.debug(u"PlexPy Monitor :: Unable to read session list.") + int_ping_count += 1 + logger.warn(u"PlexPy Monitor :: Unable to get an internal response from the server, ping attempt %s." \ + % str(int_ping_count)) + + if int_ping_count == 3: + # Fire off notifications + threading.Thread(target=notification_handler.notify_timeline, + kwargs=dict(notify_action='intdown')).start() + + def check_recently_added(): with monitor_lock: @@ -231,20 +245,10 @@ def check_server_response(): pms_connect = pmsconnect.PmsConnect() server_response = pms_connect.get_server_response() - global int_ping_count global ext_ping_count - # Check for internal server response - if not server_response: - int_ping_count += 1 - logger.warn(u"PlexPy Monitor :: Unable to get an internal response from the server, ping attempt %s." \ - % str(int_ping_count)) - # Reset internal ping counter - else: - int_ping_count = 0 - # Check for remote access - if server_response and plexpy.CONFIG.MONITOR_REMOTE_ACCESS: + if server_response: mapping_state = server_response['mapping_state'] mapping_error = server_response['mapping_error'] @@ -263,11 +267,6 @@ def check_server_response(): else: ext_ping_count = 0 - if int_ping_count == 3: - # Fire off notifications - threading.Thread(target=notification_handler.notify_timeline, - kwargs=dict(notify_action='intdown')).start() - if ext_ping_count == 3: # Fire off notifications threading.Thread(target=notification_handler.notify_timeline, diff --git a/plexpy/config.py b/plexpy/config.py index 381fe064..491f2081 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -151,6 +151,7 @@ _CONFIG_DEFINITIONS = { 'NMA_ON_EXTDOWN': (int, 'NMA', 0), 'NMA_ON_INTDOWN': (int, 'NMA', 0), 'NOTIFY_CONSECUTIVE': (int, 'Monitoring', 1), + 'NOTIFY_RECENTLY_ADDED': (int, 'Monitoring', 0), 'NOTIFY_RECENTLY_ADDED_GRANDPARENT': (int, 'Monitoring', 0), 'NOTIFY_RECENTLY_ADDED_DELAY': (int, 'Monitoring', 60), 'NOTIFY_WATCHED_PERCENT': (int, 'Monitoring', 85), diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 85be241f..89065bf5 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -448,6 +448,7 @@ class WebInterface(object): "logging_ignore_interval": plexpy.CONFIG.LOGGING_IGNORE_INTERVAL, "pms_is_remote": checked(plexpy.CONFIG.PMS_IS_REMOTE), "notify_consecutive": checked(plexpy.CONFIG.NOTIFY_CONSECUTIVE), + "notify_recently_added": checked(plexpy.CONFIG.NOTIFY_RECENTLY_ADDED), "notify_recently_added_grandparent": checked(plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_GRANDPARENT), "notify_recently_added_delay": plexpy.CONFIG.NOTIFY_RECENTLY_ADDED_DELAY, "notify_watched_percent": plexpy.CONFIG.NOTIFY_WATCHED_PERCENT, @@ -494,7 +495,7 @@ class WebInterface(object): "tv_notify_on_pause", "movie_notify_on_pause", "music_notify_on_pause", "refresh_users_on_startup", "ip_logging_enable", "movie_logging_enable", "tv_logging_enable", "music_logging_enable", "pms_is_remote", "home_stats_type", "group_history_tables", "notify_consecutive", - "notify_recently_added_grandparent", "monitor_remote_access" + "notify_recently_added", "notify_recently_added_grandparent", "monitor_remote_access" ] for checked_config in checked_configs: if checked_config not in kwargs: @@ -519,6 +520,14 @@ class WebInterface(object): if (kwargs['monitoring_interval'] != str(plexpy.CONFIG.MONITORING_INTERVAL)) or \ (kwargs['refresh_users_interval'] != str(plexpy.CONFIG.REFRESH_USERS_INTERVAL)): reschedule = True + + if 'notify_recently_added' in kwargs and \ + (kwargs['notify_recently_added'] != plexpy.CONFIG.NOTIFY_RECENTLY_ADDED): + reschedule = True + + if 'monitor_remote_access' in kwargs and \ + (kwargs['monitor_remote_access'] != plexpy.CONFIG.MONITOR_REMOTE_ACCESS): + reschedule = True if 'pms_ip' in kwargs: if kwargs['pms_ip'] != plexpy.CONFIG.PMS_IP: From ef6ef9854131dd6d0a0aac143b0921dd304a230d Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Thu, 3 Dec 2015 19:07:24 -0800 Subject: [PATCH 2/3] Clean up settings help text and wording --- data/interfaces/default/settings.html | 15 +++++++-------- data/interfaces/default/welcome.html | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index e8d3dfa4..6cef6983 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -313,7 +313,8 @@ available_notification_agents = sorted(notifiers.available_notification_agents()
-

Set the complete folder path where your Plex Server logs are, shortcuts are not recognized.
Click here for help. This is required if you enable IP logging.

+

Set the complete folder path where your Plex Server logs are, shortcuts are not recognized.
+ Click here for help. This is required if you enable IP logging (for PMS 0.9.12 and below).

@@ -414,23 +415,21 @@ available_notification_agents = sorted(notifiers.available_notification_agents()

History Logging

+

Keep records of all movie, TV show, or music items played from your Plex Media Server.

-

Keep records of all movie items played from your Plex Media Server.

-

Keep records of all TV show items played from your Plex Media Server.

-

Keep records of all music items played from your Plex Media Server.

@@ -534,7 +533,7 @@ available_notification_agents = sorted(notifiers.available_notification_agents() -

Enable to only get one notification for recently added Episodes or Tracks. Movies are unaffected.

+

Enable to only get one TV Show or Artist notification for recently added Episodes or Tracks. Movies are unaffected.

diff --git a/data/interfaces/default/welcome.html b/data/interfaces/default/welcome.html index a841eb92..93a5b518 100644 --- a/data/interfaces/default/welcome.html +++ b/data/interfaces/default/welcome.html @@ -106,13 +106,13 @@ from plexpy import common

Monitoring

Keep records of all movie, TV show, or music items played from your Plex Media Server.

- Log Movies + Enable Movie Logging
- Log TV Shows + Enable TV Show Logging
- Log Music + Enable Music Logging
From bb5aa2be3dfb7eb753ea4322a67fce25c16ade4a Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Thu, 3 Dec 2015 19:50:46 -0800 Subject: [PATCH 3/3] Remember previous recently added items * Only pull metadata if there are new recently added items since the last check --- plexpy/activity_pinger.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plexpy/activity_pinger.py b/plexpy/activity_pinger.py index 28a340e7..daccd60e 100644 --- a/plexpy/activity_pinger.py +++ b/plexpy/activity_pinger.py @@ -22,6 +22,7 @@ import time monitor_lock = threading.Lock() ext_ping_count = 0 int_ping_count = 0 +prev_keys = [0] * 10 def check_active_sessions(ws_request=False): @@ -191,7 +192,12 @@ def check_recently_added(): recently_added_list = pms_connect.get_recently_added_details(count='10') if recently_added_list: - recently_added = recently_added_list['recently_added'] + new_recently_added = recently_added_list['recently_added'] + + global prev_keys + new_keys = [item['rating_key'] for item in new_recently_added] + recently_added = [new_recently_added[i] for i, x in enumerate(new_keys) if x != prev_keys[i]] + prev_keys = new_keys for item in recently_added: metadata = []