Skip to content

Instantly share code, notes, and snippets.

@blacktwin
Last active February 22, 2017 14:17
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 blacktwin/a327055da54d7feb3eef10e64a8b661a to your computer and use it in GitHub Desktop.
Save blacktwin/a327055da54d7feb3eef10e64a8b661a to your computer and use it in GitHub Desktop.
Send an Email notification when a specific show is added to Plex. Add shows to list that you want notifications for.
"""
PlexPy > Settings > Notification Agents > Scripts > Bell icon:
[X] Notify on Recently Added
PlexPy > Settings > Notification Agents > Scripts > Gear icon:
Recently Added: notify_on_added.py
PlexPy > Settings > Notifications > Script > Script Arguments:
-sn {show_name} -ena {episode_name} -ssn {season_num00} -enu {episode_num00} -srv {server_name} -med {media_type} -pos {poster_url} -tt {title} -sum {summary} -lbn {library_name}
You can add more arguments if you want more details in the email body
"""
from email.mime.text import MIMEText
import email.utils
import smtplib
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-sn', '--show_name', action='store', default='',
help='The name of the TV show')
parser.add_argument('-ena', '--episode_name', action='store', default='',
help='The name of the episode')
parser.add_argument('-ssn', '--season_num', action='store', default='',
help='The season number of the TV show')
parser.add_argument('-enu', '--episode_num', action='store', default='',
help='The episode number of the TV show')
parser.add_argument('-srv', '--plex_server', action='store', default='',
help='The name of the Plex server')
parser.add_argument('-med', '--show_type', action='store', default='',
help='The type of media')
parser.add_argument('-pos', '--poster', action='store', default='',
help='The poster url')
parser.add_argument('-tt', '--title', action='store', default='',
help='The title of the TV show')
parser.add_argument('-sum', '--summary', action='store', default='',
help='The summary of the TV show')
parser.add_argument('-lbn', '--library_name', action='store', default='',
help='The name of the TV show')
p = parser.parse_args()
# Edit user@email.com and shows
users = [{'email': 'user1@gmail.com',
'shows': ('show1', 'show2')
},
{'email': 'user2@gmail.com',
'shows': ('show1', 'show2', 'show3')
},
{'email': 'user3@gmail.com',
'shows': ('show1', 'show2', 'show3', 'show4')
}]
# Kill script now if show_name is not in lists
too = list('Match' for u in users if p.show_name in u['shows'])
if not too:
print 'Kill script now show_name is not in lists'
exit()
# Join email addresses
to = list([u['email'] for u in users if p.show_name in u['shows']])
# Email settings
name = 'PlexPy' # Your name
sender = 'sender' # From email address
email_server = 'smtp.gmail.com' # Email server (Gmail: smtp.gmail.com)
email_port = 587 # Email port (Gmail: 587)
email_username = 'email' # Your email username
email_password = 'password' # Your email password
email_subject = 'New episode for ' + p.show_name + ' is available on ' + p.plex_server # The email subject
# Detailed body for tv shows
show_html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
{p.show_name} S{p.season_num} - E{p.episode_num} -- {p.episode_name} -- was recently added to {p.library_name} on PLEX
<br><br>
<br> {p.summary} <br>
<br><img src="{p.poster}" alt="Poster unavailable" height="150" width="102"><br>
</p>
</body>
</html>
""".format(p=p)
### Do not edit below ###
# Check to see whether it is a tv show
if p.show_type.lower() == 'show' or p.show_type.lower() == 'episode':
message = MIMEText(show_html, 'html')
message['Subject'] = email_subject
message['From'] = email.utils.formataddr((name, sender))
mailserver = smtplib.SMTP(email_server, email_port)
mailserver.starttls()
mailserver.ehlo()
mailserver.login(email_username, email_password)
mailserver.sendmail(sender, to, message.as_string())
mailserver.quit()
else:
exit()
@blacktwin
Copy link
Author

This is based on the work from JonnyWong16/notify_on_show.py that mp998 modified in their PlexPy_email_notifiation.py

@Jborn4christ
Copy link

Im getting an error in line 9
show_name = sys.argv[1]
IndexError: list index out of range

Any help would be appreciated

@blacktwin
Copy link
Author

@Jborn4christ did you copy the Arguments into the Notification > Script > Script Arguments?

@JonnyWong16
Copy link

Small improvement. No need to check the shows twice.
https://gist.github.com/blacktwin/a327055da54d7feb3eef10e64a8b661a#file-notify_on_added-py-L45-L52

# Join email addresses
to = [u['email'] for u in users if p.show_name in u['shows']]

if not to:
    print 'Kill script now show_name is not in lists'
    return

@oliver-cfc
Copy link

I'm trying to test this at the command line (outside of plexpy) - when it gets to line 80 it hits the 'else' clause so no email is sent. I'm calling the script with:
./notify_on_added.py -sn 'Seinfeld'
and Seinfeld is in my list of shows (line 36)

Is my understanding off on how to run this? How does the script know if a show was just added? (is it something sent as an argument by the 'recently added' part of plexpy?

@blacktwin
Copy link
Author

blacktwin commented Feb 21, 2017

@oliver-cfc in order test like that you need to include all the arguments listed in line 8. Sorry I forgot the instructions for PlexPy. I'll add them when I get to a PC. PlexPy can be set to trigger a custom script like this whenever something is added, watched, paused, started, stopped, etc.

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