def on_start(self): if self.is_valid_session() and self.get_live_session(): logger.debug(u"PlexPy ActivityHandler :: Session %s has started." % str(self.get_session_key())) session = self.get_live_session() # Check if any notification agents have notifications enabled if any(d['on_play'] for d in notifiers.available_notification_agents()): # Fire off notifications threading.Thread(target=notification_handler.notify, kwargs=dict(stream_data=session, notify_action='play')).start() # Write the new session to our temp session table self.update_db_session(session=session) # Check if any notification agents have notifications enabled if any(d['on_concurrent'] for d in notifiers.available_notification_agents()): # Check if any concurrent streams by the user ip = True if plexpy.CONFIG.NOTIFY_CONCURRENT_BY_IP else None ap = activity_processor.ActivityProcessor() user_sessions = ap.get_session_by_user_id(user_id=session['user_id'], ip_address=ip) if len(user_sessions) >= plexpy.CONFIG.NOTIFY_CONCURRENT_THRESHOLD: # Push any notifications - Push it on it's own thread so we don't hold up our db actions threading.Thread(target=notification_handler.notify, kwargs=dict(stream_data=session, notify_action='concurrent')).start() # Check if any notification agents have notifications enabled if any(d['on_newdevice'] for d in notifiers.available_notification_agents()): # Check if any concurrent streams by the user data_factory = datafactory.DataFactory() user_devices = data_factory.get_user_devices(user_id=session['user_id']) if session['machine_id'] not in user_devices: # Push any notifications - Push it on it's own thread so we don't hold up our db actions threading.Thread(target=notification_handler.notify, kwargs=dict(stream_data=session, notify_action='newdevice')).start()
def on_created(rating_key, **kwargs): pms_connect = pmsconnect.PmsConnect() metadata = pms_connect.get_metadata_details(rating_key) logger.debug( "Tautulli TimelineHandler :: Library item '%s' (%s) added to Plex.", metadata['full_title'], str(rating_key)) if metadata: notify = True # now = helpers.timestamp() # # if helpers.cast_to_int(metadata['added_at']) < now - 86400: # Updated more than 24 hours ago # logger.debug("Tautulli TimelineHandler :: Library item %s added more than 24 hours ago. Not notifying." # % str(rating_key)) # notify = False data_factory = datafactory.DataFactory() if 'child_keys' not in kwargs: if data_factory.get_recently_added_item(rating_key): logger.debug( "Tautulli TimelineHandler :: Library item %s added already. Not notifying again." % str(rating_key)) notify = False if notify: data = {'timeline_data': metadata, 'notify_action': 'on_created'} data.update(kwargs) plexpy.NOTIFY_QUEUE.put(data) all_keys = [rating_key] if 'child_keys' in kwargs: all_keys.extend(kwargs['child_keys']) for key in all_keys: data_factory.set_recently_added_item(key) logger.debug("Added %s items to the recently_added database table." % str(len(all_keys))) else: logger.error( "Tautulli TimelineHandler :: Unable to retrieve metadata for rating_key %s" % str(rating_key))
def _getHistory(self, user=None, user_id=None, rating_key='', parent_rating_key='', grandparent_rating_key='', start_date='', **kwargs): custom_where = [] if user_id: custom_where = [['user_id', user_id]] elif user: custom_where = [['user', user]] if 'rating_key' in kwargs: rating_key = kwargs.get('rating_key', "") custom_where = [['rating_key', rating_key]] if 'parent_rating_key' in kwargs: rating_key = kwargs.get('parent_rating_key', "") custom_where = [['parent_rating_key', rating_key]] if 'grandparent_rating_key' in kwargs: rating_key = kwargs.get('grandparent_rating_key', "") custom_where = [['grandparent_rating_key', rating_key]] if 'start_date' in kwargs: start_date = kwargs.get('start_date', "") custom_where = [[ 'strftime("%Y-%m-%d", datetime(date, "unixepoch", "localtime"))', start_date ]] data_factory = datafactory.DataFactory() history = data_factory.get_datatables_history( kwargs=kwargs, custom_where=custom_where) self.data = history return self.data
def write_session(self, session=None, notify=True): if session: values = {'session_key': session['session_key'], 'transcode_key': session['transcode_key'], 'section_id': session['section_id'], 'rating_key': session['rating_key'], 'media_type': session['media_type'], 'state': session['state'], 'user_id': session['user_id'], 'user': session['user'], 'machine_id': session['machine_id'], 'title': session['title'], 'parent_title': session['parent_title'], 'grandparent_title': session['grandparent_title'], 'full_title': session['full_title'], 'media_index': session['media_index'], 'parent_media_index': session['parent_media_index'], 'thumb': session['thumb'], 'parent_thumb': session['parent_thumb'], 'grandparent_thumb': session['grandparent_thumb'], 'year': session['year'], 'friendly_name': session['friendly_name'], #'ip_address': session['ip_address'], 'player': session['player'], 'platform': session['platform'], 'parent_rating_key': session['parent_rating_key'], 'grandparent_rating_key': session['grandparent_rating_key'], 'view_offset': session['view_offset'], 'duration': session['duration'], 'video_decision': session['video_decision'], 'audio_decision': session['audio_decision'], 'transcode_decision': session['transcode_decision'], 'width': session['width'], 'height': session['height'], 'container': session['container'], 'video_codec': session['video_codec'], 'audio_codec': session['audio_codec'], 'bitrate': session['bitrate'], 'video_resolution': session['video_resolution'], 'video_framerate': session['video_framerate'], 'aspect_ratio': session['aspect_ratio'], 'audio_channels': session['audio_channels'], 'transcode_protocol': session['transcode_protocol'], 'transcode_container': session['transcode_container'], 'transcode_video_codec': session['transcode_video_codec'], 'transcode_audio_codec': session['transcode_audio_codec'], 'transcode_audio_channels': session['transcode_audio_channels'], 'transcode_width': session['transcode_width'], 'transcode_height': session['transcode_height'] } # Add ip_address back into values if session['ip_address']: values.update({'ip_address': session['ip_address']}) keys = {'session_key': session['session_key'], 'rating_key': session['rating_key']} result = self.db.upsert('sessions', values, keys) if result == 'insert': # Check if any notification agents have notifications enabled if notify and any(d['on_play'] for d in notifiers.available_notification_agents()): values.update({'ip_address': session['ip_address']}) # Push any notifications - Push it on it's own thread so we don't hold up our db actions threading.Thread(target=notification_handler.notify, kwargs=dict(stream_data=values, notify_action='play')).start() # If it's our first write then time stamp it. started = int(time.time()) timestamp = {'started': started} self.db.upsert('sessions', timestamp, keys) # Try and grab IP address from logs (fallback if not on PMS 0.9.14 and above) if not session['ip_address']: if plexpy.CONFIG.IP_LOGGING_ENABLE and plexpy.CONFIG.PMS_LOGS_FOLDER: ip_address = self.find_session_ip(rating_key=session['rating_key'], machine_id=session['machine_id']) ip_address = {'ip_address': ip_address} self.db.upsert('sessions', ip_address, keys) # Check if any notification agents have notifications enabled if notify and any(d['on_concurrent'] for d in notifiers.available_notification_agents()): # Check if any concurrent streams by the user user_sessions = self.get_session_by_user_id(user_id=session['user_id'], ip_address=plexpy.CONFIG.NOTIFY_CONCURRENT_BY_IP) if len(user_sessions) >= plexpy.CONFIG.NOTIFY_CONCURRENT_THRESHOLD: # Push any notifications - Push it on it's own thread so we don't hold up our db actions threading.Thread(target=notification_handler.notify, kwargs=dict(stream_data=values, notify_action='concurrent')).start() # Check if any notification agents have notifications enabled if notify and any(d['on_newdevice'] for d in notifiers.available_notification_agents()): # Check if any concurrent streams by the user data_factory = datafactory.DataFactory() user_devices = data_factory.get_user_devices(user_id=session['user_id']) if session['machine_id'] not in user_devices: # Push any notifications - Push it on it's own thread so we don't hold up our db actions threading.Thread(target=notification_handler.notify, kwargs=dict(stream_data=values, notify_action='newdevice')).start() return True