Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active February 25, 2020 08:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/48d6362884b5edbf5e6d78859035183a to your computer and use it in GitHub Desktop.
Save JonnyWong16/48d6362884b5edbf5e6d78859035183a to your computer and use it in GitHub Desktop.
Send a PlexPy notification with geolocation data
# 1. Install the requests module for python.
# pip install requests
# 2. Add script arguments in PlexPy. The following script arguments are available by default. More can be added below.
# -ip {ip_address} -u {user} -mt {media_type} -t {title} -pf {platform} -pl {player} -da {datestamp} -ti {timestamp}
import argparse
import requests
import sys
## EDIT THESE SETTINGS ##
PLEXPY_APIKEY = 'XXXX' # Your PlexPy API key
PLEXPY_URL = 'http://localhost:8181/' # Your PlexPy URL
AGENT_ID = 7 # The notification agent ID for PlexPy
# Replace LAN IP addresses that start with the LAN_SUBNET with a WAN IP address
# to retrieve geolocation data. Leave REPLACEMENT_WAN_IP blank for no replacement.
LAN_SUBNET = '192.168.0'
REPLACEMENT_WAN_IP = ''
# The notification subject and body
# - Use "{p.argument}" for script arguments
# - Use "{g.value}" for geolocation data
SUBJECT_TEXT = "PlexPy Notification"
BODY_TEXT = "User <b>{p.user}</b> has watched {p.media_type} " \
"<font color='blue'><b>'{p.title}'</b></font> on " \
"<font color='green'>'{p.platform}'</font> " \
"[<font color='green'>'{p.player}'</font>] in {g.city}, {g.country} " \
"||| {p.datestamp}-{p.timestamp}"
## CODE BELOW ##
class GeoData(object):
def __init__(self, data=None):
data = data or {}
self.continent = data.get('continent', 'N/A')
self.country = data.get('country', 'N/A')
self.region = data.get('region', 'N/A')
self.city = data.get('city', 'N/A')
self.postal_code = data.get('postal_code', 'N/A')
self.timezone = data.get('timezone', 'N/A')
self.latitude = data.get('latitude', 'N/A')
self.longitude = data.get('longitude', 'N/A')
self.longitude = data.get('longitude', 'N/A')
self.accuracy = data.get('accuracy', 'N/A')
def get_geoip_info(ip_address=''):
# Get the geo IP lookup from PlexPy
payload = {'apikey': PLEXPY_APIKEY,
'cmd': 'get_geoip_lookup',
'ip_address': ip_address}
try:
r = requests.get(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
if response['response']['result'] == 'success':
data = response['response']['data']
if data.get('error'):
raise Exception(data['error'])
else:
sys.stdout.write("Successfully retrieved geolocation data.")
return GeoData(data=data)
else:
raise Exception(response['response']['message'])
except Exception as e:
sys.stderr.write("PlexPy API 'get_geoip_lookup' request failed: {0}.".format(e))
return GeoData()
def send_notification(arguments=None, geodata=None):
# Format notification text
try:
subject = SUBJECT_TEXT.format(p=arguments, g=geodata)
body = BODY_TEXT.format(p=arguments, g=geodata)
except LookupError as e:
sys.stderr.write("Unable to substitute '{0}' in the notification subject or body".format(e))
return None
# Send the notification through PlexPy
payload = {'apikey': PLEXPY_APIKEY,
'cmd': 'notify',
'agent_id': AGENT_ID,
'subject': subject,
'body': body}
try:
r = requests.post(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload)
response = r.json()
if response['response']['result'] == 'success':
sys.stdout.write("Successfully sent PlexPy notification.")
else:
raise Exception(response['response']['message'])
except Exception as e:
sys.stderr.write("PlexPy API 'notify' request failed: {0}.".format(e))
return None
if __name__ == '__main__':
# Parse arguments from PlexPy
parser = argparse.ArgumentParser()
parser.add_argument('-ip', '--ip_address', action='store', default='',
help='The IP address of the stream')
parser.add_argument('-u', '--user', action='store', default='',
help='Username of the person watching the stream')
parser.add_argument('-mt', '--media_type', action='store', default='',
help='The media type of the stream')
parser.add_argument('-t', '--title', action='store', default='',
help='The title of the media')
parser.add_argument('-pf', '--platform', action='store', default='',
help='The platform of the stream')
parser.add_argument('-pl', '--player', action='store', default='',
help='The player of the stream')
parser.add_argument('-da', '--datestamp', action='store', default='',
help='The date of the stream')
parser.add_argument('-ti', '--timestamp', action='store', default='',
help='The time of the stream')
# Add more arguments as needed
p = parser.parse_args()
# Check to make sure there is an IP address before proceeding
if p.ip_address:
if p.ip_address.startswith(LAN_SUBNET) and REPLACEMENT_WAN_IP:
ip_address = REPLACEMENT_WAN_IP
else:
ip_address = p.ip_address
geodata = get_geoip_info(ip_address=ip_address)
send_notification(arguments=p, geodata=geodata)
else:
sys.stdout.write("No IP address passed from PlexPy.")
@nesrine19992
Copy link

hi
i wat to ask if i can add a new script that calculate a bill for user (consommation * price minute) i PlexPy

@seanvree
Copy link

@JonnyWong16 ,

We can't use this script IF we have custom augments that are being passed to use with other scripts right? I get this error when trying to run this script:


Nov 11 22:52:21 PlexPY plexpy.log: 2017-11-11 22:52:20 - DEBUG   :: Thread-922 : PlexPy Notifiers :: Full script is: ['python', 'C:\\JBOPS\\notify\\notify_geodata.py', '110', 'NBC Nightly News', '315', '2017', '4', '22:52', '32']
Nov 11 22:52:21 PlexPY plexpy.log: 2017-11-11 22:52:20 - DEBUG   :: Thread-922 : PlexPy Notifiers :: Executing script in a new thread.
Nov 11 22:52:22 PlexPY plexpy.log: 2017-11-11 22:52:20 - ERROR   :: Thread-923 : PlexPy Notifiers :: Script error: 
Nov 11 22:52:22 PlexPY plexpy.log:   usage: notify_geodata.py [-h] [-ip IP_ADDRESS] [-u USER] [-mt MEDIA_TYPE]
Nov 11 22:52:22 PlexPY plexpy.log:                            [-t TITLE] [-pf PLATFORM] [-pl PLAYER]
Nov 11 22:52:22 PlexPY plexpy.log:                            [-da DATESTAMP] [-ti TIMESTAMP]
Nov 11 22:52:22 PlexPY plexpy.log:   notify_geodata.py: error: unrecognized arguments: 110 NBC Nightly News 315 2017 4 22:52 32

I already have the following already in my script arguments for use in other scripts.

{session_key} {show_name} {episode_num00} {season_num00} {streams} {timestamp} {remaining_duration}

Is there a way to use something like below?

def main():
    try:
      session_key= sys.argv[1]
      show_name = sys.argv[2]
      next_ep_num = int(sys.argv[3])
      season_num = int(sys.argv[4])
      streams = int(sys.argv[5])
      timestamp = sys.argv[6]
      remaining_duration = sys.argv[7]

@samip5
Copy link

samip5 commented Mar 23, 2018

@nesrine19992,

That's possible, but you have to code it yourself most likely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment