示例#1
0
    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_rating_key = str(self.timeline['ratingKey'])
            this_key = self.timeline['key']
            this_transcode_key = self.timeline.get('transcodeSession', '')

            # Get the live tv session uuid
            this_live_uuid = this_key.split('/')[-1] if this_key.startswith(
                '/livetv/sessions') else None

            # If we already have this session in the temp table, check for state changes
            if db_session:
                # Re-schedule the callback to reset the 5 minutes timer
                schedule_callback(
                    'session_key-{}'.format(self.get_session_key()),
                    func=force_stop_stream,
                    args=[
                        self.get_session_key(), db_session['full_title'],
                        db_session['user']
                    ],
                    minutes=5)

                last_state = db_session['state']
                last_rating_key = str(db_session['rating_key'])
                last_live_uuid = db_session['live_uuid']
                last_transcode_key = db_session['transcode_key'].split('/')[-1]
                last_paused = db_session['last_paused']
                last_rating_key_websocket = db_session['rating_key_websocket']
                last_guid = db_session['guid']

                this_guid = last_guid
                # Check guid for live TV metadata every 60 seconds
                if db_session['live'] and helpers.timestamp(
                ) - db_session['stopped'] > 60:
                    metadata = self.get_metadata(skip_cache=True)
                    if metadata:
                        this_guid = metadata['guid']

                # Make sure the same item is being played
                if (this_rating_key == last_rating_key
                        or this_rating_key == last_rating_key_websocket
                        or this_live_uuid == last_live_uuid) \
                        and this_guid == last_guid:
                    # Update the session state and viewOffset
                    if this_state == 'playing':
                        # Update the session in our temp session table
                        # if the last set temporary stopped time exceeds 60 seconds
                        if helpers.timestamp() - db_session['stopped'] > 60:
                            self.update_db_session()

                    # Start our state checks
                    if this_state != last_state:
                        if this_state == 'paused':
                            self.on_pause()
                        elif last_paused and this_state == 'playing':
                            self.on_resume()
                        elif this_state == 'stopped':
                            self.on_stop()
                        elif this_state == 'error':
                            self.on_error()

                    elif this_state == 'paused':
                        # Update the session last_paused timestamp
                        self.on_pause(still_paused=True)

                    if this_state == 'buffering':
                        self.on_buffer()

                    if this_transcode_key != last_transcode_key and this_state != 'stopped':
                        self.on_change()

                # 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
                if not db_session['watched'] and this_state != 'buffering':
                    progress_percent = helpers.get_percent(
                        self.timeline['viewOffset'], db_session['duration'])
                    watched_percent = {
                        'movie': plexpy.CONFIG.MOVIE_WATCHED_PERCENT,
                        'episode': plexpy.CONFIG.TV_WATCHED_PERCENT,
                        'track': plexpy.CONFIG.MUSIC_WATCHED_PERCENT,
                        'clip': plexpy.CONFIG.TV_WATCHED_PERCENT
                    }

                    if progress_percent >= watched_percent.get(
                            db_session['media_type'], 101):
                        logger.debug(
                            "Tautulli ActivityHandler :: Session %s watched." %
                            str(self.get_session_key()))
                        ap.set_watched(session_key=self.get_session_key())

                        watched_notifiers = notification_handler.get_notify_state_enabled(
                            session=db_session,
                            notify_action='on_watched',
                            notified=False)

                        for d in watched_notifiers:
                            plexpy.NOTIFY_QUEUE.put({
                                'stream_data':
                                db_session.copy(),
                                'notifier_id':
                                d['notifier_id'],
                                'notify_action':
                                'on_watched'
                            })

            else:
                # We don't have this session in our table yet, start a new one.
                if this_state != 'buffering':
                    self.on_start()
示例#2
0
    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:
                # Re-schedule the callback to reset the 5 minutes timer
                schedule_callback('session_key-{}'.format(
                    self.get_session_key()),
                                  func=force_stop_stream,
                                  args=[self.get_session_key()],
                                  minutes=5)

                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':
                        # Update the session in our temp session table
                        # if the last set temporary stopped time exceeds 15 seconds
                        if int(time.time()) - db_session['stopped'] > 60:
                            self.update_db_session()

                    # 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()

                    elif this_state == 'paused':
                        # Update the session last_paused timestamp
                        self.on_pause(still_paused=True)

                # 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
                if not db_session['watched'] and this_state != 'buffering':
                    progress_percent = helpers.get_percent(
                        self.timeline['viewOffset'], db_session['duration'])
                    watched_percent = {
                        'movie': plexpy.CONFIG.MOVIE_WATCHED_PERCENT,
                        'episode': plexpy.CONFIG.TV_WATCHED_PERCENT,
                        'track': plexpy.CONFIG.MUSIC_WATCHED_PERCENT,
                        'clip': plexpy.CONFIG.TV_WATCHED_PERCENT
                    }

                    if progress_percent >= watched_percent.get(
                            db_session['media_type'], 101):
                        logger.debug(
                            u"Tautulli ActivityHandler :: Session %s watched."
                            % str(self.get_session_key()))
                        ap.set_watched(session_key=self.get_session_key())

                        watched_notifiers = notification_handler.get_notify_state_enabled(
                            session=db_session,
                            notify_action='on_watched',
                            notified=False)

                        for d in watched_notifiers:
                            plexpy.NOTIFY_QUEUE.put({
                                'stream_data':
                                db_session.copy(),
                                'notifier_id':
                                d['notifier_id'],
                                'notify_action':
                                'on_watched'
                            })

            else:
                # We don't have this session in our table yet, start a new one.
                if this_state != 'buffering':
                    self.on_start()

                    # Schedule a callback to force stop a stale stream 5 minutes later
                    schedule_callback('session_key-{}'.format(
                        self.get_session_key()),
                                      func=force_stop_stream,
                                      args=[self.get_session_key()],
                                      minutes=5)