Exemple #1
0
def build_server_notify_text(state=None):
    # Get the server name
    server_name = plexcs.CONFIG.PMS_NAME

    # Get the server uptime
    plex_tv = plextv.PlexTV()
    server_times = plex_tv.get_server_times()

    if server_times:
        updated_at = server_times[0]['updated_at']
        server_uptime = helpers.human_duration(
            int(time.time() - helpers.cast_to_float(updated_at)))
    else:
        logger.error(u"Plex:CS Notifier :: Unable to retrieve server uptime.")
        server_uptime = 'N/A'

    on_extdown_subject = plexcs.CONFIG.NOTIFY_ON_EXTDOWN_SUBJECT_TEXT
    on_extdown_body = plexcs.CONFIG.NOTIFY_ON_EXTDOWN_BODY_TEXT
    on_intdown_subject = plexcs.CONFIG.NOTIFY_ON_INTDOWN_SUBJECT_TEXT
    on_intdown_body = plexcs.CONFIG.NOTIFY_ON_INTDOWN_BODY_TEXT

    available_params = {
        'server_name': server_name,
        'server_uptime': server_uptime
    }

    # Default text
    subject_text = 'Plex:CS (%s)' % server_name

    if state == 'extdown':
        # Default body text
        body_text = 'The Plex Media Server remote access is down.'

        if on_extdown_subject and on_extdown_body:
            try:
                subject_text = unicode(on_extdown_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_extdown_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == 'intdown':
        # Default body text
        body_text = 'The Plex Media Server is down.'

        if on_intdown_subject and on_intdown_body:
            try:
                subject_text = unicode(on_intdown_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_intdown_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    else:
        return None
def build_server_notify_text(state=None):
    # Get the server name
    server_name = plexcs.CONFIG.PMS_NAME

    # Get the server uptime
    plex_tv = plextv.PlexTV()
    server_times = plex_tv.get_server_times()

    if server_times:
        updated_at = server_times[0]["updated_at"]
        server_uptime = helpers.human_duration(int(time.time() - helpers.cast_to_float(updated_at)))
    else:
        logger.error(u"Plex:CS Notifier :: Unable to retrieve server uptime.")
        server_uptime = "N/A"

    on_extdown_subject = plexcs.CONFIG.NOTIFY_ON_EXTDOWN_SUBJECT_TEXT
    on_extdown_body = plexcs.CONFIG.NOTIFY_ON_EXTDOWN_BODY_TEXT
    on_intdown_subject = plexcs.CONFIG.NOTIFY_ON_INTDOWN_SUBJECT_TEXT
    on_intdown_body = plexcs.CONFIG.NOTIFY_ON_INTDOWN_BODY_TEXT

    available_params = {"server_name": server_name, "server_uptime": server_uptime}

    # Default text
    subject_text = "Plex:CS (%s)" % server_name

    if state == "extdown":
        # Default body text
        body_text = "The Plex Media Server remote access is down."

        if on_extdown_subject and on_extdown_body:
            try:
                subject_text = unicode(on_extdown_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_extdown_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == "intdown":
        # Default body text
        body_text = "The Plex Media Server is down."

        if on_intdown_subject and on_intdown_body:
            try:
                subject_text = unicode(on_intdown_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_intdown_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    else:
        return None
Exemple #3
0
def build_notify_text(session=None, timeline=None, state=None):
    import re

    # Get the server name
    server_name = plexcs.CONFIG.PMS_NAME

    # Get the server uptime
    plex_tv = plextv.PlexTV()
    server_times = plex_tv.get_server_times()

    if server_times:
        updated_at = server_times[0]['updated_at']
        server_uptime = helpers.human_duration(
            int(time.time() - helpers.cast_to_float(updated_at)))
    else:
        logger.error(u"Plex:CS Notifier :: Unable to retrieve server uptime.")
        server_uptime = 'N/A'

    # Get metadata feed for item
    if session:
        rating_key = session['rating_key']
    elif timeline:
        rating_key = timeline['rating_key']

    pms_connect = pmsconnect.PmsConnect()
    metadata_list = pms_connect.get_metadata_details(rating_key=rating_key)

    if metadata_list:
        metadata = metadata_list['metadata']
    else:
        logger.error(
            u"Plex:CS Notifier :: Unable to retrieve metadata for rating_key %s"
            % str(rating_key))
        return []

    # Check for exclusion tags
    if metadata['media_type'] == 'movie':
        # Regex pattern to remove the text in the tags we don't want
        pattern = re.compile(
            '\n*<tv>[^>]+.</tv>\n*|\n*<music>[^>]+.</music>\n*',
            re.IGNORECASE | re.DOTALL)
    elif metadata['media_type'] == 'show' or metadata[
            'media_type'] == 'episode':
        # Regex pattern to remove the text in the tags we don't want
        pattern = re.compile(
            '\n*<movie>[^>]+.</movie>\n*|\n*?<music>[^>]+.</music>\n*',
            re.IGNORECASE | re.DOTALL)
    elif metadata['media_type'] == 'artist' or metadata[
            'media_type'] == 'track':
        # Regex pattern to remove the text in the tags we don't want
        pattern = re.compile(
            '\n*<tv>[^>]+.</tv>\n*|\n*<movie>[^>]+.</movie>\n*',
            re.IGNORECASE | re.DOTALL)
    else:
        pattern = None

    if metadata['media_type'] == 'movie' \
        or metadata['media_type'] == 'show' or metadata['media_type'] == 'episode' \
        or metadata['media_type'] == 'artist' or metadata['media_type'] == 'track' \
        and pattern:
        # Remove the unwanted tags and strip any unmatch tags too.
        on_start_subject = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_START_SUBJECT_TEXT))
        on_start_body = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_START_BODY_TEXT))
        on_stop_subject = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_STOP_SUBJECT_TEXT))
        on_stop_body = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_STOP_BODY_TEXT))
        on_pause_subject = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_PAUSE_SUBJECT_TEXT))
        on_pause_body = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_PAUSE_BODY_TEXT))
        on_resume_subject = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_RESUME_SUBJECT_TEXT))
        on_resume_body = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_RESUME_BODY_TEXT))
        on_buffer_subject = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_BUFFER_SUBJECT_TEXT))
        on_buffer_body = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_BUFFER_BODY_TEXT))
        on_watched_subject = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_WATCHED_SUBJECT_TEXT))
        on_watched_body = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_WATCHED_BODY_TEXT))
        on_created_subject = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_CREATED_SUBJECT_TEXT))
        on_created_body = strip_tag(
            re.sub(pattern, '', plexcs.CONFIG.NOTIFY_ON_CREATED_BODY_TEXT))
    else:
        on_start_subject = plexcs.CONFIG.NOTIFY_ON_START_SUBJECT_TEXT
        on_start_body = plexcs.CONFIG.NOTIFY_ON_START_BODY_TEXT
        on_stop_subject = plexcs.CONFIG.NOTIFY_ON_STOP_SUBJECT_TEXT
        on_stop_body = plexcs.CONFIG.NOTIFY_ON_STOP_BODY_TEXT
        on_pause_subject = plexcs.CONFIG.NOTIFY_ON_PAUSE_SUBJECT_TEXT
        on_pause_body = plexcs.CONFIG.NOTIFY_ON_PAUSE_BODY_TEXT
        on_resume_subject = plexcs.CONFIG.NOTIFY_ON_RESUME_SUBJECT_TEXT
        on_resume_body = plexcs.CONFIG.NOTIFY_ON_RESUME_BODY_TEXT
        on_buffer_subject = plexcs.CONFIG.NOTIFY_ON_BUFFER_SUBJECT_TEXT
        on_buffer_body = plexcs.CONFIG.NOTIFY_ON_BUFFER_BODY_TEXT
        on_watched_subject = plexcs.CONFIG.NOTIFY_ON_WATCHED_SUBJECT_TEXT
        on_watched_body = plexcs.CONFIG.NOTIFY_ON_WATCHED_BODY_TEXT
        on_created_subject = plexcs.CONFIG.NOTIFY_ON_CREATED_SUBJECT_TEXT
        on_created_body = plexcs.CONFIG.NOTIFY_ON_CREATED_BODY_TEXT

    # Create a title
    if metadata['media_type'] == 'episode' or metadata['media_type'] == 'track':
        full_title = '%s - %s' % (metadata['grandparent_title'],
                                  metadata['title'])
    else:
        full_title = metadata['title']

    duration = helpers.convert_milliseconds_to_minutes(metadata['duration'])

    # Default values
    video_decision = ''
    audio_decision = ''
    transcode_decision = ''
    stream_duration = 0
    view_offset = 0
    user = ''
    platform = ''
    player = ''
    ip_address = 'N/A'

    # Session values
    if session:
        # Generate a combined transcode decision value
        video_decision = session['video_decision'].title()
        audio_decision = session['audio_decision'].title()

        if session['video_decision'] == 'transcode' or session[
                'audio_decision'] == 'transcode':
            transcode_decision = 'Transcode'
        elif session['video_decision'] == 'copy' or session[
                'audio_decision'] == 'copy':
            transcode_decision = 'Direct Stream'
        else:
            transcode_decision = 'Direct Play'

        if state != 'play':
            if session['paused_counter']:
                stream_duration = int(
                    (time.time() - helpers.cast_to_float(session['started']) -
                     helpers.cast_to_float(session['paused_counter'])) / 60)
            else:
                stream_duration = int(
                    (time.time() - helpers.cast_to_float(session['started'])) /
                    60)

        view_offset = helpers.convert_milliseconds_to_minutes(
            session['view_offset'])
        user = session['friendly_name']
        platform = session['platform']
        player = session['player']
        ip_address = session['ip_address'] if session['ip_address'] else 'N/A'

    progress_percent = helpers.get_percent(view_offset, duration)

    available_params = {
        'server_name': server_name,
        'server_uptime': server_uptime,
        'user': user,
        'platform': platform,
        'player': player,
        'ip_address': ip_address,
        'media_type': metadata['media_type'],
        'title': full_title,
        'show_name': metadata['grandparent_title'],
        'episode_name': metadata['title'],
        'artist_name': metadata['grandparent_title'],
        'album_name': metadata['parent_title'],
        'track_name': metadata['title'],
        'season_num': metadata['parent_index'].zfill(1),
        'season_num00': metadata['parent_index'].zfill(2),
        'episode_num': metadata['index'].zfill(1),
        'episode_num00': metadata['index'].zfill(2),
        'video_decision': video_decision,
        'audio_decision': audio_decision,
        'transcode_decision': transcode_decision,
        'year': metadata['year'],
        'studio': metadata['studio'],
        'content_rating': metadata['content_rating'],
        'directors': ', '.join(metadata['directors']),
        'writers': ', '.join(metadata['writers']),
        'actors': ', '.join(metadata['actors']),
        'genres': ', '.join(metadata['genres']),
        'summary': metadata['summary'],
        'tagline': metadata['tagline'],
        'rating': metadata['rating'],
        'duration': duration,
        'stream_duration': stream_duration,
        'remaining_duration': duration - view_offset,
        'progress': view_offset,
        'progress_percent': progress_percent
    }

    # Default subject text
    subject_text = 'Plex:CS (%s)' % server_name

    if state == 'play':
        # Default body text
        body_text = '%s (%s) is watching %s' % (session['friendly_name'],
                                                session['player'], full_title)

        if on_start_subject and on_start_body:
            try:
                subject_text = unicode(on_start_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_start_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == 'stop':
        # Default body text
        body_text = '%s (%s) has stopped %s' % (session['friendly_name'],
                                                session['player'], full_title)

        if on_stop_subject and on_stop_body:
            try:
                subject_text = unicode(on_stop_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_stop_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == 'pause':
        # Default body text
        body_text = '%s (%s) has paused %s' % (session['friendly_name'],
                                               session['player'], full_title)

        if on_pause_subject and on_pause_body:
            try:
                subject_text = unicode(on_pause_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_pause_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == 'resume':
        # Default body text
        body_text = '%s (%s) has resumed %s' % (session['friendly_name'],
                                                session['player'], full_title)

        if on_resume_subject and on_resume_body:
            try:
                subject_text = unicode(on_resume_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_resume_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == 'buffer':
        # Default body text
        body_text = '%s (%s) is buffering %s' % (session['friendly_name'],
                                                 session['player'], full_title)

        if on_buffer_subject and on_buffer_body:
            try:
                subject_text = unicode(on_buffer_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_buffer_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == 'watched':
        # Default body text
        body_text = '%s (%s) has watched %s' % (session['friendly_name'],
                                                session['player'], full_title)

        if on_watched_subject and on_watched_body:
            try:
                subject_text = unicode(on_watched_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_watched_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == 'created':
        # Default body text
        body_text = '%s was recently added to Plex.' % full_title

        if on_created_subject and on_created_body:
            try:
                subject_text = unicode(on_created_subject).format(
                    **available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback."
                )

            try:
                body_text = unicode(on_created_body).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback."
                    % e)
            except:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback."
                )

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    else:
        return None
def build_notify_text(session=None, timeline=None, state=None):
    import re

    # Get the server name
    server_name = plexcs.CONFIG.PMS_NAME

    # Get the server uptime
    plex_tv = plextv.PlexTV()
    server_times = plex_tv.get_server_times()

    if server_times:
        updated_at = server_times[0]["updated_at"]
        server_uptime = helpers.human_duration(int(time.time() - helpers.cast_to_float(updated_at)))
    else:
        logger.error(u"Plex:CS Notifier :: Unable to retrieve server uptime.")
        server_uptime = "N/A"

    # Get metadata feed for item
    if session:
        rating_key = session["rating_key"]
    elif timeline:
        rating_key = timeline["rating_key"]

    pms_connect = pmsconnect.PmsConnect()
    metadata_list = pms_connect.get_metadata_details(rating_key=rating_key)

    if metadata_list:
        metadata = metadata_list["metadata"]
    else:
        logger.error(u"Plex:CS Notifier :: Unable to retrieve metadata for rating_key %s" % str(rating_key))
        return []

    # Check for exclusion tags
    if metadata["media_type"] == "movie":
        # Regex pattern to remove the text in the tags we don't want
        pattern = re.compile("\n*<tv>[^>]+.</tv>\n*|\n*<music>[^>]+.</music>\n*", re.IGNORECASE | re.DOTALL)
    elif metadata["media_type"] == "show" or metadata["media_type"] == "episode":
        # Regex pattern to remove the text in the tags we don't want
        pattern = re.compile("\n*<movie>[^>]+.</movie>\n*|\n*?<music>[^>]+.</music>\n*", re.IGNORECASE | re.DOTALL)
    elif metadata["media_type"] == "artist" or metadata["media_type"] == "track":
        # Regex pattern to remove the text in the tags we don't want
        pattern = re.compile("\n*<tv>[^>]+.</tv>\n*|\n*<movie>[^>]+.</movie>\n*", re.IGNORECASE | re.DOTALL)
    else:
        pattern = None

    if (
        metadata["media_type"] == "movie"
        or metadata["media_type"] == "show"
        or metadata["media_type"] == "episode"
        or metadata["media_type"] == "artist"
        or metadata["media_type"] == "track"
        and pattern
    ):
        # Remove the unwanted tags and strip any unmatch tags too.
        on_start_subject = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_START_SUBJECT_TEXT))
        on_start_body = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_START_BODY_TEXT))
        on_stop_subject = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_STOP_SUBJECT_TEXT))
        on_stop_body = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_STOP_BODY_TEXT))
        on_pause_subject = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_PAUSE_SUBJECT_TEXT))
        on_pause_body = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_PAUSE_BODY_TEXT))
        on_resume_subject = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_RESUME_SUBJECT_TEXT))
        on_resume_body = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_RESUME_BODY_TEXT))
        on_buffer_subject = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_BUFFER_SUBJECT_TEXT))
        on_buffer_body = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_BUFFER_BODY_TEXT))
        on_watched_subject = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_WATCHED_SUBJECT_TEXT))
        on_watched_body = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_WATCHED_BODY_TEXT))
        on_created_subject = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_CREATED_SUBJECT_TEXT))
        on_created_body = strip_tag(re.sub(pattern, "", plexcs.CONFIG.NOTIFY_ON_CREATED_BODY_TEXT))
    else:
        on_start_subject = plexcs.CONFIG.NOTIFY_ON_START_SUBJECT_TEXT
        on_start_body = plexcs.CONFIG.NOTIFY_ON_START_BODY_TEXT
        on_stop_subject = plexcs.CONFIG.NOTIFY_ON_STOP_SUBJECT_TEXT
        on_stop_body = plexcs.CONFIG.NOTIFY_ON_STOP_BODY_TEXT
        on_pause_subject = plexcs.CONFIG.NOTIFY_ON_PAUSE_SUBJECT_TEXT
        on_pause_body = plexcs.CONFIG.NOTIFY_ON_PAUSE_BODY_TEXT
        on_resume_subject = plexcs.CONFIG.NOTIFY_ON_RESUME_SUBJECT_TEXT
        on_resume_body = plexcs.CONFIG.NOTIFY_ON_RESUME_BODY_TEXT
        on_buffer_subject = plexcs.CONFIG.NOTIFY_ON_BUFFER_SUBJECT_TEXT
        on_buffer_body = plexcs.CONFIG.NOTIFY_ON_BUFFER_BODY_TEXT
        on_watched_subject = plexcs.CONFIG.NOTIFY_ON_WATCHED_SUBJECT_TEXT
        on_watched_body = plexcs.CONFIG.NOTIFY_ON_WATCHED_BODY_TEXT
        on_created_subject = plexcs.CONFIG.NOTIFY_ON_CREATED_SUBJECT_TEXT
        on_created_body = plexcs.CONFIG.NOTIFY_ON_CREATED_BODY_TEXT

    # Create a title
    if metadata["media_type"] == "episode" or metadata["media_type"] == "track":
        full_title = "%s - %s" % (metadata["grandparent_title"], metadata["title"])
    else:
        full_title = metadata["title"]

    duration = helpers.convert_milliseconds_to_minutes(metadata["duration"])

    # Default values
    video_decision = ""
    audio_decision = ""
    transcode_decision = ""
    stream_duration = 0
    view_offset = 0
    user = ""
    platform = ""
    player = ""
    ip_address = "N/A"

    # Session values
    if session:
        # Generate a combined transcode decision value
        video_decision = session["video_decision"].title()
        audio_decision = session["audio_decision"].title()

        if session["video_decision"] == "transcode" or session["audio_decision"] == "transcode":
            transcode_decision = "Transcode"
        elif session["video_decision"] == "copy" or session["audio_decision"] == "copy":
            transcode_decision = "Direct Stream"
        else:
            transcode_decision = "Direct Play"

        if state != "play":
            if session["paused_counter"]:
                stream_duration = int(
                    (
                        time.time()
                        - helpers.cast_to_float(session["started"])
                        - helpers.cast_to_float(session["paused_counter"])
                    )
                    / 60
                )
            else:
                stream_duration = int((time.time() - helpers.cast_to_float(session["started"])) / 60)

        view_offset = helpers.convert_milliseconds_to_minutes(session["view_offset"])
        user = session["friendly_name"]
        platform = session["platform"]
        player = session["player"]
        ip_address = session["ip_address"] if session["ip_address"] else "N/A"

    progress_percent = helpers.get_percent(view_offset, duration)

    available_params = {
        "server_name": server_name,
        "server_uptime": server_uptime,
        "user": user,
        "platform": platform,
        "player": player,
        "ip_address": ip_address,
        "media_type": metadata["media_type"],
        "title": full_title,
        "show_name": metadata["grandparent_title"],
        "episode_name": metadata["title"],
        "artist_name": metadata["grandparent_title"],
        "album_name": metadata["parent_title"],
        "track_name": metadata["title"],
        "season_num": metadata["parent_index"].zfill(1),
        "season_num00": metadata["parent_index"].zfill(2),
        "episode_num": metadata["index"].zfill(1),
        "episode_num00": metadata["index"].zfill(2),
        "video_decision": video_decision,
        "audio_decision": audio_decision,
        "transcode_decision": transcode_decision,
        "year": metadata["year"],
        "studio": metadata["studio"],
        "content_rating": metadata["content_rating"],
        "directors": ", ".join(metadata["directors"]),
        "writers": ", ".join(metadata["writers"]),
        "actors": ", ".join(metadata["actors"]),
        "genres": ", ".join(metadata["genres"]),
        "summary": metadata["summary"],
        "tagline": metadata["tagline"],
        "rating": metadata["rating"],
        "duration": duration,
        "stream_duration": stream_duration,
        "remaining_duration": duration - view_offset,
        "progress": view_offset,
        "progress_percent": progress_percent,
    }

    # Default subject text
    subject_text = "Plex:CS (%s)" % server_name

    if state == "play":
        # Default body text
        body_text = "%s (%s) is watching %s" % (session["friendly_name"], session["player"], full_title)

        if on_start_subject and on_start_body:
            try:
                subject_text = unicode(on_start_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_start_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == "stop":
        # Default body text
        body_text = "%s (%s) has stopped %s" % (session["friendly_name"], session["player"], full_title)

        if on_stop_subject and on_stop_body:
            try:
                subject_text = unicode(on_stop_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_stop_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == "pause":
        # Default body text
        body_text = "%s (%s) has paused %s" % (session["friendly_name"], session["player"], full_title)

        if on_pause_subject and on_pause_body:
            try:
                subject_text = unicode(on_pause_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_pause_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == "resume":
        # Default body text
        body_text = "%s (%s) has resumed %s" % (session["friendly_name"], session["player"], full_title)

        if on_resume_subject and on_resume_body:
            try:
                subject_text = unicode(on_resume_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_resume_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == "buffer":
        # Default body text
        body_text = "%s (%s) is buffering %s" % (session["friendly_name"], session["player"], full_title)

        if on_buffer_subject and on_buffer_body:
            try:
                subject_text = unicode(on_buffer_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_buffer_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == "watched":
        # Default body text
        body_text = "%s (%s) has watched %s" % (session["friendly_name"], session["player"], full_title)

        if on_watched_subject and on_watched_body:
            try:
                subject_text = unicode(on_watched_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_watched_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    elif state == "created":
        # Default body text
        body_text = "%s was recently added to Plex." % full_title

        if on_created_subject and on_created_body:
            try:
                subject_text = unicode(on_created_subject).format(**available_params)
            except LookupError as e:
                logger.error(
                    u"Plex:CS Notifier :: Unable to parse field %s in notification subject. Using fallback." % e
                )
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification subject. Using fallback.")

            try:
                body_text = unicode(on_created_body).format(**available_params)
            except LookupError as e:
                logger.error(u"Plex:CS Notifier :: Unable to parse field %s in notification body. Using fallback." % e)
            except:
                logger.error(u"Plex:CS Notifier :: Unable to parse custom notification body. Using fallback.")

            return [subject_text, body_text]
        else:
            return [subject_text, body_text]
    else:
        return None