From e56d23b12df77b592a2e784209cb096750766a90 Mon Sep 17 00:00:00 2001 From: Landon Abney Date: Sat, 16 Jun 2018 21:01:21 -0700 Subject: [PATCH] Refactor get_activity into two functions Create a new function get_user_activity that returns the same data get_activity originally did and use that in main. get_activity now actually follows the name and gets all activity on the server. --- killstream/kill_stream.py | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/killstream/kill_stream.py b/killstream/kill_stream.py index 7549898..a8f94d4 100644 --- a/killstream/kill_stream.py +++ b/killstream/kill_stream.py @@ -95,8 +95,14 @@ def send_notification(subject_text, body_text, notifier_id): return None -def get_activity(user_id): - # Get the current activity on the PMS. +def get_activity(): + """Get the current activity on the PMS. + + Returns + ------- + list + The current active sessions on the Plex server. + """ payload = {'apikey': TAUTULLI_APIKEY, 'cmd': 'get_activity'} @@ -106,9 +112,7 @@ def get_activity(user_id): response = req.json() res_data = response['response']['data']['sessions'] - user_streams = [d['session_id'] - for d in res_data if d['user_id'] == user_id] - return user_streams + return res_data except Exception as e: sys.stderr.write( @@ -116,6 +120,26 @@ def get_activity(user_id): pass +def get_user_activity(user_id): + """Get current sessions for a specific user. + + Parameters + ---------- + user_id : int + The ID of the user to grab sessions for. + + Returns + ------- + list + The active sessions for the specific user ID. + + """ + sessions = get_activity() + user_streams = [s['session_id'] + for s in sessions if s['user_id'] == user_id] + return user_streams + + def terminate_session(session_id, message): # Stop a streaming session. payload = {'apikey': TAUTULLI_APIKEY, @@ -165,7 +189,7 @@ if __name__ == "__main__": if opts.jbop == 'stream': terminate_session(opts.sessionId, message) elif opts.jbop == 'allStreams': - streams = get_activity(opts.userId) + streams = get_user_activity(opts.userId) for session_id in streams: terminate_session(session_id, message)