""" Send an email with what was added to Plex in the past week using Tautulli. Email includes title (TV: Show Name: Episode Name; Movie: Movie Title), time added, image, and summary. Uses: notify_added_lastweek.py -t poster -d 1 -u all -i user1 user2 -s 250 100 # email all users expect user1 & user2 what was added in the last day using posters that are 250x100 notify_added_lastweek.py -t poster -d 7 -u all # email all users what was added in the last 7 days(week) using posters that are default sized notify_added_lastweek.py -t poster -d 7 -u all -s 1000 500 # email all users what was added in the last 7 days(week) using posters that are 1000x500 notify_added_lastweek.py -t art -d 7 -u user1 # email user1 & self what was added in the last 7 days(week) using artwork that is default sized notify_added_lastweek.py -t art -d 7 # email self what was added in the last 7 days(week) using artwork that is default sized """ import requests import sys import time import os from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage import email.utils import smtplib import urllib import cgi import uuid import argparse ## EDIT THESE SETTINGS ## TAUTULLI_APIKEY = '' # Your Tautulli API key TAUTULLI_URL = 'http://localhost:8181/' # Your Tautulli URL LIBRARY_NAMES = ['Movies', 'TV Shows'] # Name of libraries you want to check. # Email settings name = '' # Your name sender = '' # From email address to = [sender] # Whoever you want to email [sender, 'name@example.com'] # Emails will be sent as BCC. email_server = 'smtp.gmail.com' # Email server (Gmail: smtp.gmail.com) email_port = 587 # Email port (Gmail: 587) email_username = '' # Your email username email_password = '' # Your email password email_subject = 'Tautulli Added Last {} day(s) Notification' #The email subject # Default sizing for pictures # Poster poster_h = 205 poster_w = 100 # Artwork art_h = 100 art_w = 205 ## /EDIT THESE SETTINGS ## class METAINFO(object): def __init__(self, data=None): d = data or {} self.added_at = d['added_at'] self.parent_rating_key = d['parent_rating_key'] self.title = d['title'] self.rating_key = d['rating_key'] self.media_type = d['media_type'] self.grandparent_title = d['grandparent_title'] self.thumb = d['art'] self.summary = d['summary'] def get_recent(section_id, start, count): # Get the metadata for a media item. Count matters! payload = {'apikey': TAUTULLI_APIKEY, 'start': str(start), 'count': str(count), 'section_id': section_id, 'cmd': 'get_recently_added'} try: r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload) response = r.json() if response['response']['result'] == 'success': res_data = response['response']['data']['recently_added'] return res_data except Exception as e: sys.stderr.write("Tautulli API 'get_recently_added' request failed: {0}.".format(e)) def get_metadata(rating_key): # Get the metadata for a media item. payload = {'apikey': TAUTULLI_APIKEY, 'rating_key': rating_key, 'cmd': 'get_metadata', 'media_info': True} try: r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload) response = r.json() if response['response']['result'] == 'success': res_data = response['response']['data'] return METAINFO(data=res_data) except Exception as e: sys.stderr.write("Tautulli API 'get_metadata' request failed: {0}.".format(e)) def get_libraries_table(): # Get the data on the Tautulli libraries table. payload = {'apikey': TAUTULLI_APIKEY, 'cmd': 'get_libraries_table'} try: r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload) response = r.json() res_data = response['response']['data']['data'] return [d['section_id'] for d in res_data if d['section_name'] in LIBRARY_NAMES] except Exception as e: sys.stderr.write("Tautulli API 'get_libraries_table' request failed: {0}.".format(e)) def update_library_media_info(section_id): # Get the data on the Tautulli media info tables. payload = {'apikey': TAUTULLI_APIKEY, 'cmd': 'get_library_media_info', 'section_id': section_id, 'refresh': True} try: r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload) response = r.status_code if response != 200: print(r.content) except Exception as e: sys.stderr.write("Tautulli API 'update_library_media_info' request failed: {0}.".format(e)) def get_pms_image_proxy(thumb): # Gets an image from the PMS and saves it to the image cache directory. payload = {'apikey': TAUTULLI_APIKEY, 'cmd': 'pms_image_proxy', 'img': thumb} try: r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload, stream=True) return r.url except Exception as e: sys.stderr.write("Tautulli API 'get_users_tables' request failed: {0}.".format(e)) def get_users(): # Get the user list from Tautulli. payload = {'apikey': TAUTULLI_APIKEY, 'cmd': 'get_users'} try: r = requests.get(TAUTULLI_URL.rstrip('/') + '/api/v2', params=payload) response = r.json() res_data = response['response']['data'] return [d for d in res_data] except Exception as e: sys.stderr.write("Tautulli API 'get_user' request failed: {0}.".format(e)) def get_rating_keys(TODAY, LASTDATE): recent_lst = [] # Get the rating_key for what was recently added count = 25 for section_id in glt: start = 0 while True: # Assume all items will be returned in descending order of added_at recent_items = get_recent(section_id, start, count) if all([recent_items]): start += count for item in recent_items: if LASTDATE <= int(item['added_at']) <= TODAY: recent_lst.append(item['rating_key']) continue elif not all([recent_items]): break start += count if recent_lst: return recent_lst sys.stderr.write("Recently Added list: {0}.".format(recent_lst)) exit() def build_html(rating_key, height, width, pic_type): meta = get_metadata(str(rating_key)) added = time.ctime(float(meta.added_at)) # Pull image url thumb_url = "{}.jpeg".format(get_pms_image_proxy(meta.thumb)) if pic_type == 'poster': thumb_url = thumb_url.replace('%2Fart%', '%2Fposter%') image_name = "{}.jpg".format(str(rating_key)) # Saving image in current path urllib.urlretrieve(thumb_url, image_name) image = dict(title=meta.rating_key, path=image_name, cid=str(uuid.uuid4())) if meta.grandparent_title == '' or meta.media_type == 'movie': # Movies notify = u"
" \ ' | ' \ u"{x.summary} |
---|
" \ ' | ' \ u"{x.summary} |
---|
Hi!
Below is the list of content added to Plex's {LIBRARY_NAMES} this week.