Last active
November 18, 2019 07:05
-
-
Save CraftyCanine/101fb1f6d8930585ab3d6adcde50ecfb to your computer and use it in GitHub Desktop.
PlexPy alert script that checks Sonarr after a watched episode to see if the next episode in the series aired in the past and is downloaded. Alerts the user if new episode should be downloaded.
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
#!/usr/bin/env python | |
#---------------------------------------------------------- | |
# | |
# New Episodes Needed | |
# PlexPy Notification Script | |
# CraftyCanine | |
# | |
# Description | |
# | |
# Checks Sonarr after a watched episode to see if the next | |
# episode in the series aired in the past and is downloaded. | |
# Alerts the user if new episode should be downloaded. | |
# | |
# Instructions | |
# | |
# 1. Install the requests module for python. | |
# pip install requests | |
# 2. PlexPy Settings -> Notifications -> Set script arguments to: | |
# {user} {action} {rating_key} | |
# 3. Edit .py file to add your PlexPy / Sonarr URLs and API keys. | |
# 4. Put the .py script in your scripts folder. Scripts folder can be found here: PlexPy Settings -> Notifications Agents -> Cog next to Scripts -> Script folder | |
# 5. Select the script in notification agents settings: | |
# PlexPy Settings -> Notifications Agents -> Cog next to Scripts -> Watched | |
# 6. Enable the script under notification agents: | |
# PlexPy Settings -> Notifications Agents -> Bell icon next to scripts -> Check "notify on watched" | |
# 7. If you don't want alerts for your user, you can turn off notifications for your user under Users -> User Name -> Pencil -> Uncheck enable notifications | |
# | |
# -------------------------------------------------------- | |
import requests | |
import sys | |
import json | |
import re | |
import datetime | |
user = sys.argv[1] | |
action = sys.argv[2] | |
rating_key = sys.argv[3] | |
debug = False | |
## EDIT THESE SETTINGS ## | |
PLEXPY_URL = 'http://localhost:8117/' # Your PlexPy URL | |
PLEXPY_APIKEY = '' # Your PlexPy API Key | |
SONARR_URL = 'http://localhost:8989/' # Your Sonarr URL | |
SONARR_APIKEY = '' # Your Sonarr API Key | |
AGENT_ID = 7 # The PlexPy notifier agent id found here: https://github.com/drzoidberg33/plexpy/blob/master/plexpy/notifiers.py#L43 | |
NOTIFY_SUBJECT = 'Plex User Finished' # The notification subject | |
NOTIFY_BODY = '%s has finished all currently downloaded episodes of %s' # The notification body | |
## CODE BELOW ## | |
payload = {'apikey' : PLEXPY_APIKEY, | |
'cmd' : 'get_metadata', | |
'rating_key' : rating_key, #regular episode: 69838, last episode no new: 69899, last episode with new: 70240 | |
'media_info' : True} | |
r = requests.post(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) | |
j = r.json() | |
metadata = j['response']['data']['metadata'] | |
mtype = metadata['media_type'] | |
# Only for Episodes | |
if mtype == 'episode': | |
# Parse info | |
series_name = metadata['grandparent_title'] | |
season_num = int(metadata['parent_media_index']) | |
episode_num = int(metadata['media_index']) | |
guid = metadata['guid'] | |
tvdbid = int(re.findall('\/\/([0-9]+?)\/',guid)[0]) | |
# Get series info from Sonarr | |
payload = {'apikey':SONARR_APIKEY} | |
r = requests.get(SONARR_URL.rstrip('/') + '/api/series', params=payload) | |
series_info = r.json() | |
# Find info on current + next season | |
for series in series_info: | |
if series['tvdbId'] == tvdbid: | |
sonarr_id = series['id'] | |
for season in series['seasons']: | |
if season['seasonNumber'] == season_num: | |
sonarr_season = season | |
if season['seasonNumber'] == season_num + 1: | |
sonarr_next_season = season | |
# Get info on episodes | |
payload['seriesId'] = sonarr_id | |
r = requests.get(SONARR_URL.rstrip('/') + '/api/episode', params=payload) | |
episodes = r.json() | |
# Find next episode if exists | |
nextep = None | |
for episode in episodes: | |
if episode['seasonNumber'] == season_num and episode['episodeNumber'] == episode_num + 1: | |
print 'Next episode is in current season' | |
nextep = episode | |
break | |
elif episode['seasonNumber'] == season_num+1 and episode['episodeNumber'] == 1: | |
print 'Next episode is the first ep of the next season' | |
nextep = episode | |
break | |
# Next episode doesn't exist? | |
if nextep == None: | |
print 'There are no more episodes in this series.' | |
exit(0) | |
else: | |
print 'Next Ep Info:' | |
print 'Season: %d' % nextep['seasonNumber'] | |
print 'Episode: %d' % nextep['episodeNumber'] | |
tdy = datetime.date.today() | |
airs = nextep['airDate'] | |
air = datetime.datetime.strptime(airs,'%Y-%m-%d').date() | |
# Is next ep downloaded? | |
if nextep['hasFile'] == True: | |
print 'Good news! Next episode is already downloaded.' | |
elif air >= tdy: | |
print "Next episode hasn't aired yet." | |
else: | |
print 'Uh oh. Need to download more episodes for this show.' | |
# Send notification to PlexPy using the API | |
payload = {'apikey': PLEXPY_APIKEY, | |
'cmd': 'notify', | |
'agent_id': AGENT_ID, | |
'subject': NOTIFY_SUBJECT, | |
'body': NOTIFY_BODY % (user,series_name), | |
'notify_action': action.lower()} | |
r = requests.post(PLEXPY_URL.rstrip('/') + '/api/v2', params=payload) | |
else: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment