Skip to content

Instantly share code, notes, and snippets.

@daktak
Last active August 29, 2015 14:21
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 daktak/8f261d9852d5fc0bcfe8 to your computer and use it in GitHub Desktop.
Save daktak/8f261d9852d5fc0bcfe8 to your computer and use it in GitHub Desktop.
Modified to support conversion of files before sending
#!/usr/bin/env python2
# GistID: 8f261d9852d5fc0bcfe8
#
# Send files post-processing script for NZBGet
#
# Copyright (C) 2013 Andrey Prygunkov <hugbug@users.sourceforge.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Revision: 660 $
# $Date: 2013-05-02 22:40:36 +0200 (Do, 02 Mai 2013) $
#
##############################################################################
### NZBGET POST-PROCESSING SCRIPT ###
# Send files via e-mail.
#
# This script sends downloaded files with specific extensions via e-mail.
#
# NOTE: This script requires Python to be installed on your system.
##############################################################################
### OPTIONS ###
# Email address you want the emails to be sent from.
#From="NZBGet" <myaccount@gmail.com>
# Email address you want the emails to be sent to.
#To=myaccount@gmail.com
# SMTP server host.
#Server=smtp.gmail.com
# SMTP server port (1-65535).
#Port=25
# Secure communication using TLS/SSL (yes, no).
#Encryption=yes
# SMTP server user name, if required.
#Username=myaccount
# SMTP server password, if required.
#Password=mypass
# Extensions of files to be send.
#
# Only files with these extensions are processed. Extensions must
# be separated with commas.
# Example=.mobi
#Extensions=.txt
# Convert files before sending (yes, no).
#convert_files=yes
#convert=.epub
# Convert command
#convert_command=xvfb-run.py ebook-convert
# calibre output profile
#Profile=kindle_pw
### NZBGET POST-PROCESSING SCRIPT ###
##############################################################################
import os
import sys
import datetime
import smtplib
import subprocess
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import Encoders
try:
from xmlrpclib import ServerProxy # python 2
except ImportError:
from xmlrpc.client import ServerProxy # python 3
# Exit codes used by NZBGet
POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94
POSTPROCESS_NONE=95
# Check if the script is called from nzbget 11.0 or later
if not 'NZBOP_SCRIPTDIR' in os.environ:
print('*** NZBGet post-processing script ***')
print('This script is supposed to be called from nzbget (11.0 or later).')
sys.exit(POSTPROCESS_ERROR)
if not os.path.exists(os.environ['NZBPP_DIRECTORY']):
print('Destination directory doesn\'t exist, exiting')
sys.exit(POSTPROCESS_NONE)
# Check par and unpack status for errors
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4' or os.environ['NZBPP_UNPACKSTATUS'] == '1':
print('[WARNING] Download of "%s" has failed, exiting' % (os.environ['NZBPP_NZBNAME']))
sys.exit(POSTPROCESS_NONE)
required_options = ('NZBPO_FROM', 'NZBPO_TO', 'NZBPO_SERVER', 'NZBPO_PORT', 'NZBPO_ENCRYPTION',
'NZBPO_USERNAME', 'NZBPO_PASSWORD', 'NZBPO_EXTENSIONS')
for optname in required_options:
if (not optname in os.environ):
print('[ERROR] Option %s is missing in configuration file. Please check script settings' % optname[6:])
sys.exit(POSTPROCESS_ERROR)
def sendfile(filename):
# Create message
msg = MIMEMultipart()
msg['Subject'] = 'Downloaded file %s ' % os.path.splitext(os.path.basename(filename))[0]
msg['From'] = os.environ['NZBPO_FROM']
msg['To'] = os.environ['NZBPO_TO']
text = 'Downloaded file %s\n\n' % os.path.basename(filename)
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(filename, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filename))
msg.attach(part)
# Send message
print('[INFO] Sending %s' % os.path.basename(filename))
sys.stdout.flush()
try:
smtp = smtplib.SMTP(os.environ['NZBPO_SERVER'], os.environ['NZBPO_PORT'])
if os.environ['NZBPO_ENCRYPTION'] == 'yes':
smtp.starttls()
if os.environ['NZBPO_USERNAME'] != '' and os.environ['NZBPO_PASSWORD'] != '':
smtp.login(os.environ['NZBPO_USERNAME'], os.environ['NZBPO_PASSWORD'])
smtp.sendmail(os.environ['NZBPO_FROM'], os.environ['NZBPO_TO'], msg.as_string())
smtp.quit()
except Exception as err:
print('[ERROR] %s' % err)
sys.exit(POSTPROCESS_ERROR)
# search files
Extensions = os.environ['NZBPO_EXTENSIONS']
Extensions = Extensions.split(',')
Convert = os.environ['NZBPO_CONVERT']
c_cmd = os.environ['NZBPO_CONVERT_COMMAND']
profile = os.environ['NZBPO_PROFILE']
cnt = 0
if os.environ['NZBPO_CONVERT_FILES'] == 'yes':
for dirname, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']):
for filename in filenames:
extension = os.path.splitext(filename)[1]
if extension.lower() in Convert:
name = os.path.join(dirname, filename)
run = c_cmd.split(" ")
run.append(name)
run.append(name+'.mobi')
run.append('--output-profile')
run.append(profile)
subprocess.call(run)
for dirname, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']):
for filename in filenames:
extension = os.path.splitext(filename)[1]
if extension.lower() in Extensions:
name = os.path.join(dirname, filename)
sendfile(unicode(name))
cnt += 1
if cnt > 0:
print('Sent %i file(s)' % cnt)
sys.exit(POSTPROCESS_SUCCESS)
else:
print('No suitable files found')
sys.exit(POSTPROCESS_NONE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment