Last active
April 28, 2017 13:46
-
-
Save jk0/fb8ef85fc57d6b5d92c18140ea863d86 to your computer and use it in GitHub Desktop.
Get notified when someone watches something on Plex.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import json | |
import requests | |
APP = flask.Flask(__name__) | |
def send_notification(description): | |
# endpoint = "https://www.notifymyandroid.com/publicapi/notify?" | |
endpoint = "https://api.prowlapp.com/publicapi/add?" | |
params = { | |
"apikey": "<API KEY>", | |
"application": "Plex", | |
"event": "Now Playing", | |
"description": description | |
} | |
requests.post(endpoint, params=params) | |
@APP.route("/", methods=["POST"]) | |
def index(): | |
request = json.loads(dict(flask.request.form)["payload"][0]) | |
event = request["event"] | |
media_type = request["Metadata"]["type"] | |
if event != "media.play" or media_type == "track": | |
return "", 204 | |
account = request["Account"]["title"] | |
title = request["Metadata"]["title"] | |
year = request["Metadata"]["year"] | |
if "grandparentTitle" in request["Metadata"]: | |
title = "%s - %s" % (request["Metadata"]["grandparentTitle"], title) | |
send_notification("%s is watching %s (%s)" % (account, title, year)) | |
return "", 204 | |
if __name__ == "__main__": | |
kwargs = { | |
"host": "0.0.0.0", | |
"port": 8080, | |
"threaded": True | |
} | |
ssl_crt = "/path/to/ssl.crt" | |
ssl_key = "/path/to/ssl.key" | |
if os.path.exists(ssl_crt) and os.path.exists(ssl_key): | |
kwargs["ssl_context"] = (ssl_crt, ssl_key) | |
APP.run(**kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment