-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnotify.py
101 lines (77 loc) · 3.13 KB
/
notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from typing import List
from resources.server import getPlexServer
from resources.log import getLogger
from resources.settings import Settings
from argparse import ArgumentParser
import os
import sys
import requests
###########################################################################################################################
# Credit to https://gist.github.com/liamcottle/86180844b81fcf8085e2c9182daa278c for the original script
# Requires "New Content Added to Library" notification to be enabled
###########################################################################################################################
def csv(arg: str) -> List:
return [x.lower().strip() for x in arg.split(",") if x]
log = getLogger(__name__)
parser = ArgumentParser(description="Plex Autoskip Notification Sender")
parser.add_argument('-c', '--config', help='Specify an alternate configuration file location')
parser.add_argument('-au', '--allowedusers', help="Users to send message to, leave back to send to all users", type=csv)
parser.add_argument('-bu', '--blockedusers', help="Users to exlude sending the message to", type=csv)
parser.add_argument('-u', '--url', help="URL to direct users to when clicked", default="https://github.com/mdhiggins/PlexAutoSkip")
parser.add_argument('message', help='Message to send to users')
args = vars(parser.parse_args())
if args['config'] and os.path.exists(args['config']):
settings = Settings(args['config'], loadCustom=False, logger=log)
elif args['config'] and os.path.exists(os.path.join(os.path.dirname(sys.argv[0]), args['config'])):
settings = Settings(os.path.join(os.path.dirname(sys.argv[0]), args['config']), loadCustom=False, logger=log)
else:
settings = Settings(loadCustom=False, logger=log)
server, _ = getPlexServer(settings, log)
message = args['message']
if not message:
log.warning("No message included, aborting")
sys.exit(1)
users = args['allowedusers']
blocked = args['blockedusers']
myPlexAccount = server.myPlexAccount()
if not myPlexAccount:
log.warning("No myPlex account found, aborting")
sys.exit(1)
myPlexUsers = myPlexAccount.users() + [myPlexAccount]
if users:
myPlexUsers = [u for u in myPlexUsers if u.username.lower() in users]
if blocked:
myPlexUsers = [u for u in myPlexUsers if u.username.lower() not in blocked]
uids = [u.id for u in myPlexUsers]
if not uids:
log.warning("No valid users to notify, aborting")
sys.exit(1)
log.info("Sending message to %d users" % len(uids))
headers = {
"X-Plex-Token": server._token,
}
data = {
"group": 'media',
"identifier": 'tv.plex.notification.library.new',
"to": uids,
"play": False,
"data": {
"provider": {
"identifier": server.machineIdentifier,
"title": server.friendlyName,
}
},
"metadata": {
"title": message,
},
"uri": args['url'],
}
url = 'https://notifications.plex.tv/api/v1/notifications'
log.debug(data)
x = requests.post(url, json=data, headers=headers)
log.debug(x.text)
log.info("Response received with status code %s" % (x.status_code))
if x.status_code in [200, 201]:
sys.exit(0)
else:
sys.exit(1)