diff --git a/README.md b/README.md index dfdeef1..9d65269 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,14 @@ The config file is in the scripts folder. Before first run of the script, pleas * web_path - Path on the domain to the web page * web_delete_previous_images - True to delete all .jpg images in the web image folder prior to copying over current images +#####Image Upload - If this option is enabled, image hosting will be used for web and email + +##Cloudinary - Sign up for a free account at: http://cloudinary.com/ +* upload_use_cloudinary - Use Cloudinary image hosting +* upload_cloudinary_cloud_name - Cloudinary cloud name +* upload_cloudinary_api_key - Cloudinary api key +* upload_cloudinary_api_secret - Cloudinary api secret + #####Email * email_enabled - Enable the creation and sending of an email * email_individually - True to send out emails individually to each address in the email_to setting diff --git a/scripts/config.conf b/scripts/config.conf index 377a7b7..59e06d2 100644 --- a/scripts/config.conf +++ b/scripts/config.conf @@ -10,6 +10,13 @@ web_folder = '' date_format = '%m/%d/%y' days_back_to_search = 7 +##Image Upload - If this option is enabled, image hosting will be used for web and email +#Cloudinary - Sign up for a free account at: http://cloudinary.com/ +upload_use_cloudinary = False +upload_cloudinary_cloud_name = '' +upload_cloudinary_api_key = '' +upload_cloudinary_api_secret = '' + ##Web web_enabled = True web_domain = '' @@ -27,7 +34,7 @@ email_smtp_address = 'smtp.gmail.com' email_smtp_port = 587 email_username = '' email_password = '' -# Only valid if web_enabled = True +# Only valid if web_enabled = True and upload_use_cloudinary = False email_use_web_images = True ##Messages diff --git a/scripts/plexEmail.py b/scripts/plexEmail.py index 1a562d2..4d316d5 100644 --- a/scripts/plexEmail.py +++ b/scripts/plexEmail.py @@ -5,6 +5,12 @@ import operator import shutil import smtplib +import requests +import base64 +import cloudinary +import cloudinary.uploader +import cloudinary.api +from base64 import b64encode from collections import OrderedDict from datetime import date, timedelta from email.mime.multipart import MIMEMultipart @@ -110,14 +116,22 @@ def processImage(imageHash, thumb, mediaType, seasonIndex, episodeIndex): imgLocation = config['plex_data_folder'] + 'Plex Media Server' + os.path.sep + 'Metadata' + os.path.sep + 'TV Shows' + os.path.sep + imageHash[0] + os.path.sep + imageHash[1:len(imageHash)] + '.bundle' + os.path.sep + 'Contents' + os.path.sep + indexer + os.path.sep + 'seasons' + os.path.sep + str(seasonIndex) + os.path.sep + 'episodes' + os.path.sep + str(episodeIndex) + os.path.sep + 'thumbs' + os.path.sep + imgName webImgFullPath = config['web_domain'] + config['web_path'] + '/images/' + imgName + '.jpg' img = config['web_folder'] + config['web_path'] + os.path.sep + 'images' + os.path.sep + imgName + '.jpg' - - if (config['web_enabled'] and config['email_use_web_images']): + + cloudinaryURL = '' + if ('upload_use_cloudinary' in config and config['upload_use_cloudinary']): + thumbObj['emailImgPath'] = webImgFullPath + #imgurURL = uploadToImgur(imgLocation, imgName) + cloudinaryURL = uploadToCloudinary(imgLocation) + elif (config['web_enabled'] and config['email_use_web_images']): thumbObj['emailImgPath'] = webImgFullPath elif (os.path.isfile(imgLocation)): imgNames['Image_' + imgName] = imgLocation thumbObj['emailImgPath'] = 'cid:Image_' + imgName - if (os.path.isfile(imgLocation)): + if (cloudinaryURL != ''): + thumbObj['webImgPath'] = cloudinaryURL + thumbObj['emailImgPath'] = cloudinaryURL + elif (os.path.isfile(imgLocation)): shutil.copy(imgLocation, img) thumbObj['webImgPath'] = 'images/' + imgName + '.jpg' else: @@ -125,7 +139,36 @@ def processImage(imageHash, thumb, mediaType, seasonIndex, episodeIndex): thumbObj['emailImgPath'] = '' return thumbObj - + +def uploadToImgur(imgToUpload, nameOfUpload): + client_id = 'a0c7b089b62b220' + headers = {"Authorization": "Client-ID " + client_id} + url = "https://api.imgur.com/3/upload.json" + if (os.path.isfile(imgToUpload)): + response = requests.post( + url, + headers = headers, + data = { + 'image': b64encode(open(imgToUpload, 'rb').read()), + 'type': 'base64', + 'name': nameOfUpload + '.jpg', + 'title': nameOfUpload + } + ) + print response + print json.dumps(response.text) + data = json.loads(response.text)['data']; + return data['link'] + else: + return '' + +def uploadToCloudinary(imgToUpload): + if (os.path.isfile(imgToUpload)): + response = cloudinary.uploader.upload(imgToUpload) + return response['url'] + else: + return '' + def containsnonasciicharacters(str): return not all(ord(c) < 128 for c in str) @@ -190,6 +233,13 @@ def sendMail(email): execfile(os.path.dirname(os.path.realpath(sys.argv[0])) + os.path.sep + 'config.conf', config) replaceConfigTokens() +if ('upload_use_cloudinary' in config and config['upload_use_cloudinary']): + cloudinary.config( + cloud_name = config['upload_cloudinary_cloud_name'], + api_key = config['upload_cloudinary_api_key'], + api_secret = config['upload_cloudinary_api_secret'] + ) + DATABASE_FILE = config['plex_data_folder'] + 'Plex Media Server' + os.path.sep + 'Plug-in Support' + os.path.sep + 'Databases' + os.path.sep + 'com.plexapp.plugins.library.db' con = sqlite3.connect(DATABASE_FILE) @@ -403,12 +453,12 @@ def sendMail(email): emailMovies += '
' - emailMovies += '' + emailMovies += '' emailMovies += ' | ' - emailMovies += '' + title + '' + emailMovies += ' | ' + title.decode('utf-8') + '' if (movies[movie]['tagline'] != ''): - emailMovies += '' + movies[movie]['tagline'] + ' ' - emailMovies += '' + movies[movie]['summary'] + ' ' + emailMovies += '' + movies[movie]['tagline'].decode('utf-8') + ' ' + emailMovies += '' + movies[movie]['summary'].decode('utf-8') + ' ' if (movies[movie]['duration']): emailMovies += 'Runtime: ' + str(movies[movie]['duration'] // 1000 // 60) + ' minutes ' if (movies[movie]['year']): @@ -418,11 +468,11 @@ def sendMail(email): emailMovies += ' |
' + movies[movie]['tagline'] + '
' - htmlMovies += '' + movies[movie]['summary'] + '
' + htmlMovies += '' + movies[movie]['tagline'].decode('utf-8') + '
' + htmlMovies += '' + movies[movie]['summary'].decode('utf-8') + '
' if (movies[movie]['duration']): htmlMovies += 'Runtime: ' + str(movies[movie]['duration'] // 1000 // 60) + ' minutes
' if (movies[movie]['year']): @@ -449,23 +499,23 @@ def sendMail(email): imageInfo = processImage(hash, imageInfo['thumb'], 'show', 0, 0) emailTVShows += '' - emailTVShows += ' | ' + title + '' + emailTVShows += ' | ' + emailTVShows += ' | ' + title.decode('utf-8') + '' if (tvShows[show]['tagline'] != ''): - emailTVShows += '' + tvShows[show]['tagline'] + ' ' - emailTVShows += '' + tvShows[show]['summary'] + ' ' + emailTVShows += '' + tvShows[show]['tagline'].decode('utf-8') + ' ' + emailTVShows += '' + tvShows[show]['summary'].decode('utf-8') + ' ' if (tvShows[show]['studio'] != ''): - emailTVShows += 'Network: ' + tvShows[show]['studio'] + ' ' + emailTVShows += 'Network: ' + tvShows[show]['studio'].decode('utf-8') + ' ' emailTVShows += ' |
' + tvShows[show]['tagline'] + '
' - htmlTVShows += '' + tvShows[show]['summary'] + '
' + htmlTVShows += '' + tvShows[show]['tagline'].decode('utf-8') + '
' + htmlTVShows += '' + tvShows[show]['summary'].decode('utf-8') + '
' if (tvShows[show]['studio'] != ''): - htmlTVShows += 'Network: ' + tvShows[show]['studio'] + '
' + htmlTVShows += 'Network: ' + tvShows[show]['studio'].decode('utf-8') + '
' htmlTVShows += '' - emailTVSeasons += ' | ' + title + '' + emailTVSeasons += ' | ' + emailTVSeasons += ' | ' + title.decode('utf-8') + '' emailTVSeasons += 'Season ' + str(tvSeasons[season]['index']) + ' ' if (tvSeasons[season]['tagline'] != ''): - emailTVSeasons += '' + tvSeasons[season]['tagline'] + ' ' - emailTVSeasons += '' + tvSeasons[season]['summary'] + ' ' + emailTVSeasons += '' + tvSeasons[season]['tagline'].decode('utf-8') + ' ' + emailTVSeasons += '' + tvSeasons[season]['summary'].decode('utf-8') + ' ' if (tvSeasons[season]['studio'] != ''): - emailTVSeasons += 'Network: ' + tvSeasons[season]['studio'] + ' ' + emailTVSeasons += 'Network: ' + tvSeasons[season]['studio'].decode('utf-8') + ' ' emailTVSeasons += ' |
Season ' + str(tvSeasons[season]['index']) + '
' if (tvSeasons[season]['tagline'] != ''): - htmlTVSeasons += '' + tvSeasons[season]['tagline'] + '
' - htmlTVSeasons += '' + tvSeasons[season]['summary'] + '
' + htmlTVSeasons += '' + tvSeasons[season]['tagline'].decode('utf-8') + '
' + htmlTVSeasons += '' + tvSeasons[season]['summary'].decode('utf-8') + '
' if (tvSeasons[season]['studio'] != ''): - htmlTVSeasons += 'Network: ' + tvSeasons[season]['studio'] + '
' + htmlTVSeasons += 'Network: ' + tvSeasons[season]['studio'].decode('utf-8') + '
' htmlTVSeasons += '' - emailTVEpisodes += ' | ' + showTitle + '' + emailTVEpisodes += ' | ' + emailTVEpisodes += ' | ' + showTitle.decode('utf-8') + '' emailTVEpisodes += 'S' + str(tvEpisodes[episode]['season_index']) + ' E' + str(tvEpisodes[episode]['index']) + ': ' + title + ' ' if (tvEpisodes[episode]['tagline'] != ''): - emailTVEpisodes += '' + tvEpisodes[episode]['tagline'] + ' ' - emailTVEpisodes += '' + tvEpisodes[episode]['summary'] + ' ' + emailTVEpisodes += '' + tvEpisodes[episode]['tagline'].decode('utf-8') + ' ' + emailTVEpisodes += '' + tvEpisodes[episode]['summary'].decode('utf-8') + ' ' if (tvEpisodes[episode]['studio'] != ''): - emailTVEpisodes += 'Network: ' + tvEpisodes[episode]['studio'] + ' ' + emailTVEpisodes += 'Network: ' + tvEpisodes[episode]['studio'].decode('utf-8') + ' ' emailTVEpisodes += ' |
S' + str(tvEpisodes[episode]['season_index']) + ' E' + str(tvEpisodes[episode]['index']) + ': ' + title + '
' if (tvEpisodes[episode]['tagline'] != ''): - htmlTVEpisodes += '' + tvEpisodes[episode]['tagline'] + '
' - htmlTVEpisodes += '' + tvEpisodes[episode]['summary'] + '
' + htmlTVEpisodes += '' + tvEpisodes[episode]['tagline'].decode('utf-8') + '
' + htmlTVEpisodes += '' + tvEpisodes[episode]['summary'].decode('utf-8') + '
' if (tvEpisodes[episode]['studio'] != ''): - htmlTVEpisodes += 'Network: ' + tvEpisodes[episode]['studio'] + '
' + htmlTVEpisodes += 'Network: ' + tvEpisodes[episode]['studio'].decode('utf-8') + '
' htmlTVEpisodes += '