2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# 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
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import datetime
|
2016-06-23 19:58:26 +00:00
|
|
|
import re
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import sickbeard
|
2016-06-23 19:58:26 +00:00
|
|
|
from lib.six import moves
|
2014-03-10 05:18:05 +00:00
|
|
|
from base64 import standard_b64encode
|
|
|
|
from common import Quality
|
2016-06-23 19:58:26 +00:00
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard.helpers import tryInt
|
|
|
|
from sickbeard.providers.generic import GenericProvider
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
2016-06-23 19:58:26 +00:00
|
|
|
def test_nzbget(host, use_https, username, password):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-06-23 19:58:26 +00:00
|
|
|
result = False
|
|
|
|
if not host:
|
|
|
|
msg = 'No NZBget host found. Please configure it'
|
|
|
|
logger.log(msg, logger.ERROR)
|
|
|
|
return result, msg, None
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-06-23 19:58:26 +00:00
|
|
|
url = 'http%(scheme)s://%(username)s:%(password)s@%(host)s/xmlrpc' % {
|
|
|
|
'scheme': ('', 's')[use_https], 'host': re.sub('(?:https?://)?(.*?)/?$', r'\1', host),
|
|
|
|
'username': username, 'password': password}
|
|
|
|
rpc_client = moves.xmlrpc_client.ServerProxy(url)
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
2016-06-23 19:58:26 +00:00
|
|
|
msg = 'Success. Connected'
|
|
|
|
if rpc_client.writelog('INFO', 'SickGear connected as a test'):
|
|
|
|
logger.log(msg, logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2016-06-23 19:58:26 +00:00
|
|
|
msg += ', but unable to send a message'
|
|
|
|
logger.log(msg, logger.ERROR)
|
|
|
|
result = True
|
|
|
|
logger.log(u'NZBGet URL: %s' % url, logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-14 16:22:02 +00:00
|
|
|
except moves.http_client.socket.error:
|
2016-06-23 19:58:26 +00:00
|
|
|
msg = 'Please check NZBget host and port (if it is running). NZBget is not responding to these values'
|
|
|
|
logger.log(msg, logger.ERROR)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-14 16:22:02 +00:00
|
|
|
except moves.xmlrpc_client.ProtocolError as e:
|
2016-06-23 19:58:26 +00:00
|
|
|
if 'Unauthorized' == e.errmsg:
|
|
|
|
msg = 'NZBget username or password is incorrect'
|
|
|
|
logger.log(msg, logger.ERROR)
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2016-06-23 19:58:26 +00:00
|
|
|
msg = 'Protocol Error: %s' % e.errmsg
|
|
|
|
logger.log(msg, logger.ERROR)
|
|
|
|
|
|
|
|
return result, msg, rpc_client
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2017-09-13 17:18:59 +00:00
|
|
|
def send_nzb(nzb):
|
2016-06-23 19:58:26 +00:00
|
|
|
result = False
|
|
|
|
add_to_top = False
|
|
|
|
nzbget_prio = 0
|
|
|
|
|
|
|
|
authed, auth_msg, rpc_client = test_nzbget(
|
|
|
|
sickbeard.NZBGET_HOST, sickbeard.NZBGET_USE_HTTPS, sickbeard.NZBGET_USERNAME, sickbeard.NZBGET_PASSWORD)
|
|
|
|
|
|
|
|
if not authed:
|
|
|
|
return authed
|
|
|
|
|
|
|
|
dupekey = ''
|
2014-03-10 05:18:05 +00:00
|
|
|
dupescore = 0
|
|
|
|
# if it aired recently make it high priority and generate DupeKey/Score
|
|
|
|
for curEp in nzb.episodes:
|
2016-06-23 19:58:26 +00:00
|
|
|
if '' == dupekey:
|
2017-09-13 17:18:59 +00:00
|
|
|
dupekey = 'SickGear-%s%s' % (
|
2016-09-04 20:00:44 +00:00
|
|
|
sickbeard.indexerApi(curEp.show.indexer).config.get('dupekey', ''), curEp.show.indexerid)
|
2016-06-23 19:58:26 +00:00
|
|
|
dupekey += '-%s.%s' % (curEp.season, curEp.episode)
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if datetime.date.today() - curEp.airdate <= datetime.timedelta(days=7):
|
2016-06-23 19:58:26 +00:00
|
|
|
add_to_top = True
|
|
|
|
nzbget_prio = sickbeard.NZBGET_PRIORITY
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-06-23 19:58:26 +00:00
|
|
|
if Quality.UNKNOWN != nzb.quality:
|
2014-03-10 05:18:05 +00:00
|
|
|
dupescore = nzb.quality * 100
|
2017-09-13 17:18:59 +00:00
|
|
|
|
|
|
|
dupescore += (0, 9 + nzb.properlevel)[0 < nzb.properlevel]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-05-05 22:48:28 +00:00
|
|
|
nzbcontent64 = None
|
2016-06-23 19:58:26 +00:00
|
|
|
if 'nzbdata' == nzb.resultType:
|
2017-02-13 20:00:55 +00:00
|
|
|
data = nzb.get_data()
|
|
|
|
if not data:
|
|
|
|
return False
|
2014-05-05 22:48:28 +00:00
|
|
|
nzbcontent64 = standard_b64encode(data)
|
2016-07-02 23:18:19 +00:00
|
|
|
elif 'Anizb' == nzb.provider.name and 'nzb' == nzb.resultType:
|
|
|
|
gen_provider = GenericProvider('')
|
|
|
|
data = gen_provider.get_url(nzb.url)
|
|
|
|
if None is data:
|
2016-06-23 19:58:26 +00:00
|
|
|
return result
|
2016-07-02 23:18:19 +00:00
|
|
|
nzbcontent64 = standard_b64encode(data)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-06-23 19:58:26 +00:00
|
|
|
logger.log(u'Sending NZB to NZBGet: %s' % nzb.name)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
try:
|
2016-06-23 19:58:26 +00:00
|
|
|
# Find out if nzbget supports priority (Version 9.0+), old versions beginning with a 0.x will use the old cmd
|
|
|
|
nzbget_version_str = rpc_client.version()
|
|
|
|
nzbget_version = tryInt(nzbget_version_str[:nzbget_version_str.find('.')])
|
|
|
|
|
|
|
|
# v13+ has a combined append method that accepts both (url and content)
|
|
|
|
# also the return value has changed from boolean to integer
|
|
|
|
# (Positive number representing NZBID of the queue item. 0 and negative numbers represent error codes.)
|
|
|
|
if 13 <= nzbget_version:
|
|
|
|
nzbget_result = rpc_client.append(
|
|
|
|
'%s.nzb' % nzb.name, (nzbcontent64, nzb.url)[None is nzbcontent64], sickbeard.NZBGET_CATEGORY,
|
|
|
|
nzbget_prio, False, False, dupekey, dupescore, 'score') > 0
|
|
|
|
|
|
|
|
elif 12 == nzbget_version:
|
2014-05-05 22:48:28 +00:00
|
|
|
if nzbcontent64 is not None:
|
2016-06-23 19:58:26 +00:00
|
|
|
nzbget_result = rpc_client.append('%s.nzb' % nzb.name, sickbeard.NZBGET_CATEGORY,
|
|
|
|
nzbget_prio, False, nzbcontent64, False, dupekey, dupescore, 'score')
|
2014-05-05 22:48:28 +00:00
|
|
|
else:
|
2016-06-23 19:58:26 +00:00
|
|
|
nzbget_result = rpc_client.appendurl('%s.nzb' % nzb.name, sickbeard.NZBGET_CATEGORY,
|
|
|
|
nzbget_prio, False, nzb.url, False, dupekey, dupescore, 'score')
|
|
|
|
elif 0 == nzbget_version:
|
2014-05-05 22:48:28 +00:00
|
|
|
if nzbcontent64 is not None:
|
2016-06-23 19:58:26 +00:00
|
|
|
nzbget_result = rpc_client.append('%s.nzb' % nzb.name, sickbeard.NZBGET_CATEGORY,
|
|
|
|
add_to_top, nzbcontent64)
|
2014-05-05 22:48:28 +00:00
|
|
|
else:
|
2016-06-23 19:58:26 +00:00
|
|
|
if 'nzb' == nzb.resultType:
|
|
|
|
gen_provider = GenericProvider('')
|
|
|
|
data = gen_provider.get_url(nzb.url)
|
|
|
|
if None is data:
|
|
|
|
return result
|
|
|
|
|
|
|
|
nzbcontent64 = standard_b64encode(data)
|
|
|
|
nzbget_result = rpc_client.append('%s.nzb' % nzb.name, sickbeard.NZBGET_CATEGORY,
|
|
|
|
add_to_top, nzbcontent64)
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2014-05-05 22:48:28 +00:00
|
|
|
if nzbcontent64 is not None:
|
2016-06-23 19:58:26 +00:00
|
|
|
nzbget_result = rpc_client.append('%s.nzb' % nzb.name, sickbeard.NZBGET_CATEGORY,
|
|
|
|
nzbget_prio, False, nzbcontent64)
|
2014-05-05 22:48:28 +00:00
|
|
|
else:
|
2016-06-23 19:58:26 +00:00
|
|
|
nzbget_result = rpc_client.appendurl('%s.nzb' % nzb.name, sickbeard.NZBGET_CATEGORY,
|
|
|
|
nzbget_prio, False, nzb.url)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if nzbget_result:
|
2016-06-23 19:58:26 +00:00
|
|
|
logger.log(u'NZB sent to NZBget successfully', logger.DEBUG)
|
|
|
|
result = True
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2016-06-23 19:58:26 +00:00
|
|
|
logger.log(u'NZBget could not add %s to the queue' % ('%s.nzb' % nzb.name), logger.ERROR)
|
2017-09-13 17:18:59 +00:00
|
|
|
except(StandardError, Exception):
|
2016-06-23 19:58:26 +00:00
|
|
|
logger.log(u'Connect Error to NZBget: could not add %s to the queue' % ('%s.nzb' % nzb.name), logger.ERROR)
|
|
|
|
|
|
|
|
return result
|