def handle_msg(title: str, msg: str) -> None: try: notification_json = json.loads(msg) notify(notification_json["title"], notification_json["text"]) except json.JSONDecodeError: print("Error while decoding JSON!") except: pass
def process(self): if self.is_valid_session(): ap = activity_processor.ActivityProcessor() db_session = ap.get_session_by_key(session_key=self.get_session_key()) this_state = self.timeline['state'] this_key = str(self.timeline['ratingKey']) # If we already have this session in the temp table, check for state changes if db_session: last_state = db_session['state'] last_key = str(db_session['rating_key']) # Make sure the same item is being played if this_key == last_key: # Update the session state and viewOffset if this_state == 'playing': ap.set_session_state(session_key=self.get_session_key(), state=this_state, view_offset=self.timeline['viewOffset']) # Start our state checks if this_state != last_state: if this_state == 'paused': self.on_pause() elif last_state == 'paused' and this_state == 'playing': self.on_resume() elif this_state == 'stopped': self.on_stop() elif this_state == 'buffering': self.on_buffer() # If a client doesn't register stop events (I'm looking at you PHT!) check if the ratingKey has changed else: # Manually stop and start # Set force_stop so that we don't overwrite our last viewOffset self.on_stop(force_stop=True) self.on_start() # Monitor if the stream has reached the watch percentage for notifications # The only purpose of this is for notifications # Check if any notification agents have notifications enabled notify_agents = [d['id'] for d in notifiers.available_notification_agents() if d['on_watched']] # Get the current states for notifications from our db notified_agents = [d['agent_id'] for d in notification_handler.get_notify_state(session=db_session) if d['notify_action'] == 'watched'] if notify_agents else [] if any(a not in notified_agents for a in notify_agents): progress_percent = helpers.get_percent(self.timeline['viewOffset'], db_session['duration']) if progress_percent >= plexpy.CONFIG.NOTIFY_WATCHED_PERCENT and this_state != 'buffering': # Rather not put this on it's own thread so we know it completes before our next event. notification_handler.notify(stream_data=db_session, notify_action='watched') else: # We don't have this session in our table yet, start a new one. if this_state != 'buffering': self.on_start()
def notify(self, notifier_id='', subject='Tautulli', body='Test notification', **kwargs): """ Send a notification using Tautulli. ``` Required parameters: notifier_id (int): The ID number of the notification agent subject (str): The subject of the message body (str): The body of the message Optional parameters: headers (str): The JSON headers for webhook notifications script_args (str): The arguments for script notifications Returns: None ``` """ if not notifier_id: self._api_msg = 'Notification failed: no notifier id provided.' self._api_result_type = 'error' return notifier = notifiers.get_notifier_config(notifier_id=notifier_id) if not notifier: self._api_msg = 'Notification failed: invalid notifier_id provided %s.' % notifier_id self._api_result_type = 'error' return logger.api_debug(u'Tautulli APIv2 :: Sending notification.') success = notification_handler.notify(notifier_id=notifier_id, notify_action='api', subject=subject, body=body, **kwargs) if success: self._api_msg = 'Notification sent.' self._api_result_type = 'success' else: self._api_msg = 'Notification failed.' self._api_result_type = 'error' return