<commit_before>#!/usr/bin/env python3

import json
import requests
import os
import os.path


if os.environ.get('SLACK_URL'):
    SLACK_URL = os.environ['SLACK_URL']
else:
    config_file = '{}/.plex_notify_config.json'.format(os.path.expanduser('~'))
    if not os.path.isfile(config_file):
        raise Exception("No config file found in Environment Variables or at {}".format(config_file))
    config = json.load(open(config_file))
    SLACK_URL = config['slack_url']


def send_slack(msg):
    print(json.dumps(msg))
    r = requests.post(SLACK_URL, data=json.dumps(msg))
    print(r.status_code)
    print(r.headers)


def process_event(event):
    if event['event'] == 'media.play':
        msg = gen_msg(format_viewer(event), "Started: {}".format(format_title(event['Metadata'])))
        send_slack(msg)
    elif event['event'] == 'media.scrobble':
        msg = gen_msg(format_viewer(event), "Finished: {}".format(event['Metadata']))
        send_slack(msg)


def format_title(meta):
    if 'grandparentTitle' in meta:
        return "{} S{}E{}: {}".format(meta['grandparentTitle'], meta['parentIndex'], meta['index'], meta['title'])
    else:
        return meta['title']


def format_viewer(event):
    return "{account} on {device} @ {ip}".format(account=event['Account']['title'], device=event['Player']['title'], ip=event['Player']['publicAddress'])


def gen_msg(text, subtext):
    msg = {
        "text": text,
        "icon-emoji": ":plex:",
        "attachments": [<commit_msg>fixed media.scrobble event<commit_after>#!/usr/bin/env python3

import json
import requests
import os
import os.path


if os.environ.get('SLACK_URL'):
    SLACK_URL = os.environ['SLACK_URL']
else:
    config_file = '{}/.plex_notify_config.json'.format(os.path.expanduser('~'))
    if not os.path.isfile(config_file):
        raise Exception("No config file found in Environment Variables or at {}".format(config_file))
    config = json.load(open(config_file))
    SLACK_URL = config['slack_url']


def send_slack(msg):
    print(json.dumps(msg))
    r = requests.post(SLACK_URL, data=json.dumps(msg))
    print(r.status_code)
    print(r.headers)


def process_event(event):
    if event['event'] == 'media.play':
        msg = gen_msg(format_viewer(event), "Started: {}".format(format_title(event['Metadata'])))
        send_slack(msg)
    elif event['event'] == 'media.scrobble':
        msg = gen_msg(format_viewer(event), "Finished: {}".format(format_title(event['Metadata']))
        send_slack(msg)


def format_title(meta):
    if 'grandparentTitle' in meta:
        return "{} S{}E{}: {}".format(meta['grandparentTitle'], meta['parentIndex'], meta['index'], meta['title'])
    else:
        return meta['title']


def format_viewer(event):
    return "{account} on {device} @ {ip}".format(account=event['Account']['title'], device=event['Player']['title'], ip=event['Player']['publicAddress'])


def gen_msg(text, subtext):
    msg = {
        "text": text,
        "icon-emoji": ":plex:",
        "attachments": [
{'fraction_non_alphanumeric': 0.176158940397351, 'fraction_numerical': 0.0006622516556291391, 'mean_word_length': 4.190721649484536, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '453983', 'n_tokens_mistral': 1009, 'n_tokens_neox': 995, 'n_words': 190}