Code: Select all
#!/usr/bin/env python
#
# 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=myuser@mydomain.com
# Email address you want the emails to be sent to.
To=myuser2@gmail.com
# SMTP server host.
Server=smtp.gmail.com
# SMTP server port (1-65535).
Port=587
# Secure communication using TLS/SSL (yes, no).
Encryption=yes
# SMTP server user name, if required.
Username=myuser@mydomain.com
# SMTP server password, if required.
Password=mypassword
# Extensions of files to be send.
#
# Only files with these extensions are processed. Extensions must
# be separated with commas.
# Example=.mobi,.epub
Extensions=.epub
### NZBGET POST-PROCESSING SCRIPT ###
##############################################################################
import os
import sys
import datetime
import smtplib
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(',')
cnt = 0
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)
hugbug wrote:No need to post the script which we already have. Better post the error message.
Code: Select all
ERROR Tue Jun 16 2015 23:08:43 Post-process-script SendFiles.py for [mybook] (epub) failed (terminated with unknown status)
INFO Tue Jun 16 2015 23:08:43 SendFiles: ImportError: cannot import name 'Encoders'
INFO Tue Jun 16 2015 23:08:43 SendFiles: from email import Encoders
INFO Tue Jun 16 2015 23:08:43 SendFiles: File "/data/scripts/SendFiles.py", line 77, in <module>
INFO Tue Jun 16 2015 23:08:43 SendFiles: Traceback (most recent call last):
INFO Tue Jun 16 2015 23:08:43 Executing post-process-script SendFiles.py
I re-did the script as you suggested and get the above error.l2g wrote:thebigslicksuitd: I think if you were to just download the script and not make any changes to it; just copy it straight into you nzbget/scripts folder as you received it. The changes and settings you want to make are done through the NZBGet (web page) configuration screen. You don't directly want to modify the script itself (ever).
Users browsing this forum: No registered users and 21 guests