Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active May 15, 2020 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/fde21ac211e77cb4f63a4cb62174ae2f to your computer and use it in GitHub Desktop.
Save JonnyWong16/fde21ac211e77cb4f63a4cb62174ae2f to your computer and use it in GitHub Desktop.
Check Plex remote access using CanYouSeeMe.org and send a notification
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Description: Check Plex remote access using CanYouSeeMe.org and send a notification
# Author: /u/SwiftPanda16
# Tautulli script trigger:
# * Plex Remote Access Down
# Tautulli script conditions:
# * None
# Tautulli script arguments:
# * Plex Remote Access Down:
# --ip {remote_access_public_address} --port {remote_access_public_port}
# Optional: --notifierID #
import argparse
import os
import requests
from bs4 import BeautifulSoup
### OPTIONAL SETTINGS ###
NOTIFY_SUBJECT = 'CanYouSeeMe.org remote access check ({opts.ip}:{opts.port})'
NOTIFY_BODY = '{result}'
TAUTULLI_URL = ''
TAUTULLI_APIKEY = ''
TAUTULLI_URL = os.getenv('TAUTULLI_URL', TAUTULLI_URL)
TAUTULLI_APIKEY = os.getenv('TAUTULLI_APIKEY', TAUTULLI_APIKEY)
### END SETTINGS ###
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--ip', required=True)
parser.add_argument('--port', required=True, type=int)
parser.add_argument('--notifierID', type=int)
opts = parser.parse_args()
url = 'https://www.canyouseeme.org'
data = {'IP': opts.ip, 'port': opts.port}
r = requests.post(url, data=data)
soup = BeautifulSoup(r.content.decode('utf-8'), 'html.parser')
soup_divtool = soup.find('div', {'class': 'tool'})
for br in soup_divtool.find_all('br'):
br.replace_with('\n')
try:
soup_result = soup_divtool.find_all('p')[0].get_text().strip()
success = soup_result.startswith('Success')
except:
success = False
print(NOTIFY_SUBJECT.format(opts=opts))
print(NOTIFY_BODY.format(opts=opts, result=soup_result.encode('utf-8')))
if not success and opts.notifierID:
params = {
'apikey': TAUTULLI_APIKEY,
'cmd': 'notify',
'notifier_id': opts.notifierID,
'subject': NOTIFY_SUBJECT.format(opts=opts),
'body': NOTIFY_BODY.format(opts=opts, result=soup_result.encode('utf-8'))
}
r = requests.post(TAUTULLI_URL.rstrip('/') + '/api/v2', params=params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment