Skip to content

Commit

Permalink
New Feature and Bug Fix
Browse files Browse the repository at this point in the history
Added an option to skip sending emails if there is no new content
Added an option to skip creating a web page if there is no new content
Fixed a bug where we were using date instead of datetime which made
issue for daily reports
  • Loading branch information
jakewaldron committed Mar 12, 2015
1 parent ebf67a1 commit 375253b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ The config file is in the scripts folder. Before first run of the script, pleas
* web_domain - Domain name of the web page
* 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
* web_skip_if_no_additions - True to skip creating a web page if there are no new additions

#####Image Upload - If this option is enabled, image hosting will be used for web and email

Expand All @@ -122,6 +123,7 @@ The config file is in the scripts folder. Before first run of the script, pleas
* email_username - SMTP authentication username
* email_password - SMTP authentication password
* email_use_web_images - Use images from the web server instead of attaching them directly to the email
* email_skip_if_no_additions - True to skip sending emails if there are no new additions

#####Messages
* msg_email_teaser - Teaser text on the email
Expand All @@ -140,6 +142,7 @@ The config file is in the scripts folder. Before first run of the script, pleas
* msg_new_seasons_header - Section header text for new seasons
* msg_new_episodes_header - Section header text for new episodes
* msg_footer - Footer text at the bottom of the page
* msg_no_new_content - Message to be displayed if no new content has been added

#####Sorting
* movie_sort_1 - Highest priority sort
Expand Down
3 changes: 3 additions & 0 deletions scripts/config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ web_enabled = True
web_domain = ''
web_path = 'plexemail'
web_delete_previous_images = False
web_skip_if_no_additions = False

##Email
email_enabled = True
Expand All @@ -38,6 +39,7 @@ email_username = ''
email_password = ''
# Only valid if web_enabled = True and upload_use_cloudinary = False
email_use_web_images = True
email_skip_if_no_additions = False

##Messages
msg_email_teaser = 'Check out this week\'s releases from Plex: {website} - {past_day} - {current_day}'
Expand All @@ -56,6 +58,7 @@ msg_new_shows_header = 'New TV Shows'
msg_new_seasons_header = 'New TV Seasons'
msg_new_episodes_header = 'New TV Episodes'
msg_footer = ''
msg_no_new_content = 'There have been no new additions to Plex from {past_day} through {current_day}.'

##Sorting
# Possible values = id, title, title_sort, original_title, rating, tagline,
Expand Down
52 changes: 44 additions & 8 deletions scripts/plexEmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@
from email.utils import formataddr

def replaceConfigTokens():
## The below code is for backwards compatibility
## The below code is for backwards compatibility
if ('msg_no_new_content' not in config):
config['msg_no_new_content'] = 'There have been no new additions to Plex from {past_day} through {current_day}.'

if ('email_skip_if_no_additions' not in config):
config['email_skip_if_no_additions'] = False

if ('web_skip_if_no_additions' not in config):
config['web_skip_if_no_additions'] = False

if ('date_days_back_to_search' not in config and 'days_back_to_search' in config):
config['date_days_back_to_search'] = config['days_back_to_search']

Expand Down Expand Up @@ -72,6 +81,7 @@ def replaceConfigTokens():
if ('episode_sort_3_reverse' not in config.keys() or config['episode_sort_3_reverse'] == ""):
config['episode_sort_3_reverse'] = False

# The below code is replacing tokens
for value in config:
if (isinstance(config[value], str)):
if ('date_use_hours' in config and config['date_use_hours']):
Expand Down Expand Up @@ -291,7 +301,13 @@ def createEmailHTML():
if (seasonCount > 0):
emailText += emailTVSeasons + '<br/>&nbsp;'
if (episodeCount > 0):
emailText += emailTVEpisodes
emailText += emailTVEpisodes

if(not hasNewContent):
emailText += """<div class="headline" style="background: #FFF !important; padding-top: 50px !important; padding-bottom: 50px !important;">
<h2 style="width: 100%; text-align: center; background: #FFF !important;">""" + config['msg_no_new_content'] + """</h2>
</div>"""

emailText += """
<!-- Footer -->
<footer>
Expand Down Expand Up @@ -420,6 +436,11 @@ def createWebHTML():
if (episodeCount > 0):
htmlText += htmlTVEpisodes

if(not hasNewContent):
htmlText += """<div class="headline" style="background: #FFF !important; padding-top: 50px !important;">
<h2 style="width: 100%; text-align: center; background: #FFF !important;"><font style="color: #000000;">""" + config['msg_no_new_content'] + """</font></h2>
</div>"""

htmlText += """<hr class="featurette-divider">
<!-- Footer -->
<footer>
Expand Down Expand Up @@ -464,9 +485,10 @@ def createWebHTML():
with con:

if ('date_use_hours' in config and config['date_use_hours']):
dateSearch = 'date(\'now\', \'-' + str(config['date_hours_back_to_search']) + ' hours\')'
dateSearch = 'datetime(\'now\', \'localtime\', \'-' + str(config['date_hours_back_to_search']) + ' hours\')'
else:
dateSearch = 'date(\'now\', \'-' + str(config['date_days_back_to_search']) + ' days\')'
dateSearch = 'datetime(\'now\', \'localtime\', \'-' + str(config['date_days_back_to_search']) + ' days\')'

cur = con.cursor()
cur.execute("SELECT id, parent_id, metadata_type, title, title_sort, original_title, rating, tagline, summary, content_rating, duration, user_thumb_url, tags_genre, tags_director, tags_star, year, hash, [index], studio FROM metadata_items WHERE added_at >= " + dateSearch + " AND metadata_type >= 1 AND metadata_type <= 4 ORDER BY title_sort;")

Expand Down Expand Up @@ -753,15 +775,25 @@ def createWebHTML():
if (tvEpisodes[episode]['studio'] != ''):
htmlTVEpisodes += '<p class="lead">Network: ' + tvEpisodes[episode]['studio'].decode('utf-8') + '</p>'
htmlTVEpisodes += '</div></div><br/>&nbsp;<br/>&nbsp;'

if (movieCount > 0 or showCount > 0 or seasonCount > 0 or episodeCount > 0):
hasNewContent = True
else:
hasNewContent = False

emailHTML = createEmailHTML()
webHTML = createWebHTML()

if (config['web_enabled']):
if (config['web_enabled'] and (not config['web_skip_if_no_additions'] or hasNewContent)):
with open(config['web_folder'] + config['web_path'] + os.path.sep + 'index.html', 'w') as text_file:
text_file.write(webHTML.encode('utf-8'))
print 'Web page created successfully'
elif (not config['web_enabled']):
print 'Web page was not created because the option is disabled in the config file.'
else:
print 'Web page was not created because there were no new additions in the timeframe specified.'

if (config['email_enabled']):
if (config['email_enabled'] and (not config['email_skip_if_no_additions'] or hasNewContent)):
try:
emailCount = 0
if (config['email_individually']):
Expand All @@ -772,6 +804,10 @@ def createWebHTML():
else:
sendMail('')
emailCount += 1
print 'successfully sent %s email(s)' % emailCount
print 'Successfully sent %s email(s)' % emailCount
except:
print "failed to send email"
print "Failed to send email(s)"
elif (config['email_enabled']):
print 'Emails were not sent because the option is disabled in the config file.'
else:
print 'Emails were not sent because there were no new additions in the timeframe specified.'

0 comments on commit 375253b

Please sign in to comment.