· 6 years ago · Feb 29, 2020, 03:00 AM
1# -*- coding: utf-8 -*-
2
3'''
4Receive session_id, IP, username, title and player from PlexPy when playback starts.
5Use IP to check against blacklist and player name to block specific devices
6If it is in IP blacklist or it is in player blacklist and is not specified user, use session_id to kill stream.
7
8PlexPy > Settings > Notification Agents > Scripts > Bell icon:
9 [X] Notify on playback starts
10PlexPy > Settings > Notification Agents > Scripts > Gear icon:
11 Playback Start: kill_stream_blockedip.py
12PlexPy > Settings > Notifications > Script > Script Arguments:
13 {session_id} {ip_address} {username} {title} {player}
14
15'''
16
17import sys
18import requests
19
20
21## EDIT THESE SETTINGS ##
22PLEXPY_APIKEY = '##' # Your PlexPy API key
23PLEXPY_URL = '##' # Your PlexPy URL
24
25# List of blocked IPs. Add here as you add to the array just so we know who/what the IP block is for
26ADAM = '##'
27BOB = '##'
28BRIAN = '##'
29CHAD = '##'
30DAN = '##'
31DAVEY = '##'
32EMMANUEL = '##'
33JOSH = '##'
34JUAN = '##'
35JUSTIN = '##'
36KYLE = '##'
37NICK = '##'
38RONNIE = '##'
39
40IP_WHITELIST = [ADAM, BOB, BRIAN, CHAD, DAN, DAVEY, EMMANUEL, JOSH, JUAN, JUSTIN, KYLE, NICK, RONNIE] # List IP addresses.
41# PLAYER_BLACKLIST = ['Bob\'s iPhone'] # List of Player names.
42
43REASON = 'Unknown internet IP, closing connection.'
44##/EDIT THESE SETTINGS ##
45
46session_id = sys.argv[1]
47ip_address = sys.argv[2]
48username = sys.argv[3]
49title = sys.argv[4]
50player = sys.argv[5]
51
52if (ip_address not in IP_WHITELIST) and (username != "##"):
53 sys.stdout.write("Killing {user}'s stream of {title}. IP: {ip} is unknown and not approved. ".format(
54 user=username, title=title, ip=ip_address))
55 try:
56 payload = {'apikey': PLEXPY_APIKEY,
57 'cmd': 'terminate_session',
58 'session_id': session_id,
59 'message': REASON}
60 r = requests.post(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
61 response = r.json()
62
63 if response['response']['result'] == 'success':
64 sys.stdout.write("Successfully sent PlexPy notification.")
65 else:
66 raise Exception(response['response']['message'])
67 except Exception as e:
68 sys.stderr.write("Plex API request failed: {0}.".format(e))