Skip to content

Commit

Permalink
Added External Image Hosting as an Option
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewaldron committed Mar 11, 2015
1 parent 3f3755d commit 56ff1ff
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 44 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion scripts/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand All @@ -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
Expand Down
136 changes: 93 additions & 43 deletions scripts/plexEmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -110,22 +116,59 @@ 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:
thumbObj['webImgPath'] = ''
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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -403,12 +453,12 @@ def sendMail(email):

emailMovies += '<table><tr width="100%">'
emailMovies += '<td width="200">'
emailMovies += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['emailImgPath'] +'" width="154">'
emailMovies += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['emailImgPath'].decode('utf-8') +'" width="154">'
emailMovies += '</td>'
emailMovies += '<td><h2 class="featurette-heading">' + title + '</h2>'
emailMovies += '<td><h2 class="featurette-heading">' + title.decode('utf-8') + '</h2>'
if (movies[movie]['tagline'] != ''):
emailMovies += '<p class="lead"><i>' + movies[movie]['tagline'] + '</i></p>'
emailMovies += '<p class="lead">' + movies[movie]['summary'] + '</p>'
emailMovies += '<p class="lead"><i>' + movies[movie]['tagline'].decode('utf-8') + '</i></p>'
emailMovies += '<p class="lead">' + movies[movie]['summary'].decode('utf-8') + '</p>'
if (movies[movie]['duration']):
emailMovies += '<p class="lead">Runtime: ' + str(movies[movie]['duration'] // 1000 // 60) + ' minutes</p>'
if (movies[movie]['year']):
Expand All @@ -418,11 +468,11 @@ def sendMail(email):
emailMovies += '</td></tr></table><br/>&nbsp;<br/>&nbsp;'

htmlMovies += '<div class="featurette" id="movies">'
htmlMovies += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['webImgPath'] + '" width="154px" height="218px">'
htmlMovies += '<div style="margin-left: 200px;"><h2 class="featurette-heading">' + title + '</h2>'
htmlMovies += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['webImgPath'].decode('utf-8') + '" width="154px" height="218px">'
htmlMovies += '<div style="margin-left: 200px;"><h2 class="featurette-heading">' + title.decode('utf-8') + '</h2>'
if (movies[movie]['tagline'] != ''):
htmlMovies += '<p class="lead"><i>' + movies[movie]['tagline'] + '</i></p>'
htmlMovies += '<p class="lead">' + movies[movie]['summary'] + '</p>'
htmlMovies += '<p class="lead"><i>' + movies[movie]['tagline'].decode('utf-8') + '</i></p>'
htmlMovies += '<p class="lead">' + movies[movie]['summary'].decode('utf-8') + '</p>'
if (movies[movie]['duration']):
htmlMovies += '<p class="lead">Runtime: ' + str(movies[movie]['duration'] // 1000 // 60) + ' minutes</p>'
if (movies[movie]['year']):
Expand All @@ -449,23 +499,23 @@ def sendMail(email):
imageInfo = processImage(hash, imageInfo['thumb'], 'show', 0, 0)

emailTVShows += '<table><tr width="100%">'
emailTVShows += '<td width="200"><img class="featurette-image img-responsive pull-left" src="' + imageInfo['emailImgPath'] +'" width="154"></td>'
emailTVShows += '<td><h2 class="featurette-heading">' + title + '</h2>'
emailTVShows += '<td width="200"><img class="featurette-image img-responsive pull-left" src="' + imageInfo['emailImgPath'].decode('utf-8') +'" width="154"></td>'
emailTVShows += '<td><h2 class="featurette-heading">' + title.decode('utf-8') + '</h2>'
if (tvShows[show]['tagline'] != ''):
emailTVShows += '<p class="lead"><i>' + tvShows[show]['tagline'] + '</i></p>'
emailTVShows += '<p class="lead">' + tvShows[show]['summary'] + '</p>'
emailTVShows += '<p class="lead"><i>' + tvShows[show]['tagline'].decode('utf-8') + '</i></p>'
emailTVShows += '<p class="lead">' + tvShows[show]['summary'].decode('utf-8') + '</p>'
if (tvShows[show]['studio'] != ''):
emailTVShows += '<p class="lead">Network: ' + tvShows[show]['studio'] + '</p>'
emailTVShows += '<p class="lead">Network: ' + tvShows[show]['studio'].decode('utf-8') + '</p>'
emailTVShows += '</td></tr></table><br/>&nbsp;<br/>&nbsp;'

htmlTVShows += '<div class="featurette" id="shows">'
htmlTVShows += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['webImgPath'] + '" width="154px" height="218px">'
htmlTVShows += '<div style="margin-left: 200px;"><h2 class="featurette-heading">' + title + '</h2>'
htmlTVShows += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['webImgPath'].decode('utf-8') + '" width="154px" height="218px">'
htmlTVShows += '<div style="margin-left: 200px;"><h2 class="featurette-heading">' + title.decode('utf-8') + '</h2>'
if (tvShows[show]['tagline'] != ''):
htmlTVShows += '<p class="lead"><i>' + tvShows[show]['tagline'] + '</i></p>'
htmlTVShows += '<p class="lead">' + tvShows[show]['summary'] + '</p>'
htmlTVShows += '<p class="lead"><i>' + tvShows[show]['tagline'].decode('utf-8') + '</i></p>'
htmlTVShows += '<p class="lead">' + tvShows[show]['summary'].decode('utf-8') + '</p>'
if (tvShows[show]['studio'] != ''):
htmlTVShows += '<p class="lead">Network: ' + tvShows[show]['studio'] + '</p>'
htmlTVShows += '<p class="lead">Network: ' + tvShows[show]['studio'].decode('utf-8') + '</p>'
htmlTVShows += '</div></div><br/>&nbsp;<br/>&nbsp;'

for season in tvSeasons:
Expand Down Expand Up @@ -511,25 +561,25 @@ def sendMail(email):
imageInfo = processImage(hash, imageInfo['thumb'], 'show', 0, 0)

emailTVSeasons += '<table><tr width="100%">'
emailTVSeasons += '<td width="200"><img class="featurette-image img-responsive pull-left" src="' + imageInfo['emailImgPath'] +'" width="154"></td>'
emailTVSeasons += '<td><h2 class="featurette-heading">' + title + '</h2>'
emailTVSeasons += '<td width="200"><img class="featurette-image img-responsive pull-left" src="' + imageInfo['emailImgPath'].decode('utf-8') +'" width="154"></td>'
emailTVSeasons += '<td><h2 class="featurette-heading">' + title.decode('utf-8') + '</h2>'
emailTVSeasons += '<p class="lead"><b>Season ' + str(tvSeasons[season]['index']) + '</b></p>'
if (tvSeasons[season]['tagline'] != ''):
emailTVSeasons += '<p class="lead"><i>' + tvSeasons[season]['tagline'] + '</i></p>'
emailTVSeasons += '<p class="lead">' + tvSeasons[season]['summary'] + '</p>'
emailTVSeasons += '<p class="lead"><i>' + tvSeasons[season]['tagline'].decode('utf-8') + '</i></p>'
emailTVSeasons += '<p class="lead">' + tvSeasons[season]['summary'].decode('utf-8') + '</p>'
if (tvSeasons[season]['studio'] != ''):
emailTVSeasons += '<p class="lead">Network: ' + tvSeasons[season]['studio'] + '</p>'
emailTVSeasons += '<p class="lead">Network: ' + tvSeasons[season]['studio'].decode('utf-8') + '</p>'
emailTVSeasons += '</td></tr></table><br/>&nbsp;<br/>&nbsp;'

htmlTVSeasons += '<div class="featurette" id="shows">'
htmlTVSeasons += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['webImgPath'] + '" width="154px" height="218px">'
htmlTVSeasons += '<div style="margin-left: 200px;"><h2 class="featurette-heading">' + title + '</h2>'
htmlTVSeasons += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['webImgPath'].decode('utf-8') + '" width="154px" height="218px">'
htmlTVSeasons += '<div style="margin-left: 200px;"><h2 class="featurette-heading">' + title.decode('utf-8') + '</h2>'
htmlTVSeasons += '<p class="lead"><b>Season ' + str(tvSeasons[season]['index']) + '</b></p>'
if (tvSeasons[season]['tagline'] != ''):
htmlTVSeasons += '<p class="lead"><i>' + tvSeasons[season]['tagline'] + '</i></p>'
htmlTVSeasons += '<p class="lead">' + tvSeasons[season]['summary'] + '</p>'
htmlTVSeasons += '<p class="lead"><i>' + tvSeasons[season]['tagline'].decode('utf-8') + '</i></p>'
htmlTVSeasons += '<p class="lead">' + tvSeasons[season]['summary'].decode('utf-8') + '</p>'
if (tvSeasons[season]['studio'] != ''):
htmlTVSeasons += '<p class="lead">Network: ' + tvSeasons[season]['studio'] + '</p>'
htmlTVSeasons += '<p class="lead">Network: ' + tvSeasons[season]['studio'].decode('utf-8') + '</p>'
htmlTVSeasons += '</div></div><br/>&nbsp;<br/>&nbsp;'

for episode in tvEpisodes:
Expand Down Expand Up @@ -586,25 +636,25 @@ def sendMail(email):
imageInfo = processImage(hash, imageInfo['thumb'], 'show', 0, 0)

emailTVEpisodes += '<table><tr width="100%">'
emailTVEpisodes += '<td width="200"><img class="featurette-image img-responsive pull-left" src="' + imageInfo['emailImgPath'] +'" width="154"></td>'
emailTVEpisodes += '<td><h2 class="featurette-heading">' + showTitle + '</h2>'
emailTVEpisodes += '<td width="200"><img class="featurette-image img-responsive pull-left" src="' + imageInfo['emailImgPath'].decode('utf-8') +'" width="154"></td>'
emailTVEpisodes += '<td><h2 class="featurette-heading">' + showTitle.decode('utf-8') + '</h2>'
emailTVEpisodes += '<p class="lead"><i>S' + str(tvEpisodes[episode]['season_index']) + ' E' + str(tvEpisodes[episode]['index']) + ': ' + title + '</i></p>'
if (tvEpisodes[episode]['tagline'] != ''):
emailTVEpisodes += '<p class="lead"><i>' + tvEpisodes[episode]['tagline'] + '</i></p>'
emailTVEpisodes += '<p class="lead">' + tvEpisodes[episode]['summary'] + '</p>'
emailTVEpisodes += '<p class="lead"><i>' + tvEpisodes[episode]['tagline'].decode('utf-8') + '</i></p>'
emailTVEpisodes += '<p class="lead">' + tvEpisodes[episode]['summary'].decode('utf-8') + '</p>'
if (tvEpisodes[episode]['studio'] != ''):
emailTVEpisodes += '<p class="lead">Network: ' + tvEpisodes[episode]['studio'] + '</p>'
emailTVEpisodes += '<p class="lead">Network: ' + tvEpisodes[episode]['studio'].decode('utf-8') + '</p>'
emailTVEpisodes += '</td></tr></table><br/>&nbsp;<br/>&nbsp;'

htmlTVEpisodes += '<div class="featurette" id="shows">'
htmlTVEpisodes += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['webImgPath'] + '" width="154px" height="218px">'
htmlTVEpisodes += '<div style="margin-left: 200px;"><h2 class="featurette-heading">' + showTitle + '</h2>'
htmlTVEpisodes += '<img class="featurette-image img-responsive pull-left" src="' + imageInfo['webImgPath'].decode('utf-8') + '" width="154px" height="218px">'
htmlTVEpisodes += '<div style="margin-left: 200px;"><h2 class="featurette-heading">' + showTitle.decode('utf-8') + '</h2>'
htmlTVEpisodes += '<p class="lead"><i>S' + str(tvEpisodes[episode]['season_index']) + ' E' + str(tvEpisodes[episode]['index']) + ': ' + title + '</i></p>'
if (tvEpisodes[episode]['tagline'] != ''):
htmlTVEpisodes += '<p class="lead"><i>' + tvEpisodes[episode]['tagline'] + '</i></p>'
htmlTVEpisodes += '<p class="lead">' + tvEpisodes[episode]['summary'] + '</p>'
htmlTVEpisodes += '<p class="lead"><i>' + tvEpisodes[episode]['tagline'].decode('utf-8') + '</i></p>'
htmlTVEpisodes += '<p class="lead">' + tvEpisodes[episode]['summary'].decode('utf-8') + '</p>'
if (tvEpisodes[episode]['studio'] != ''):
htmlTVEpisodes += '<p class="lead">Network: ' + tvEpisodes[episode]['studio'] + '</p>'
htmlTVEpisodes += '<p class="lead">Network: ' + tvEpisodes[episode]['studio'].decode('utf-8') + '</p>'
htmlTVEpisodes += '</div></div><br/>&nbsp;<br/>&nbsp;'

emailText += emailMovies + '<br/>&nbsp;' + emailTVShows + '<br/>&nbsp;' + emailTVSeasons + '<br/>&nbsp;' + emailTVEpisodes + """
Expand Down Expand Up @@ -656,7 +706,7 @@ def sendMail(email):

if (config['web_enabled']):
with open(config['web_folder'] + config['web_path'] + os.path.sep + 'index.html', 'w') as text_file:
text_file.write(html)
text_file.write(html.encode('utf-8'))

if (config['email_enabled']):
try:
Expand Down

0 comments on commit 56ff1ff

Please sign in to comment.