You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.6 KiB

14 years ago
from couchpotato.core.downloaders.base import Downloader
from couchpotato.core.helpers.encoding import tryUrlencode
14 years ago
from couchpotato.core.helpers.variable import cleanHost
from couchpotato.core.logger import CPLog
import traceback
import urllib2
import json
14 years ago
log = CPLog(__name__)
class Sabnzbd(Downloader):
type = ['nzb']
def download(self, data = {}, movie = {}, manual = False, filedata = None):
14 years ago
if self.isDisabled(manual) or not self.isCorrectType(data.get('type')):
14 years ago
return
log.info('Sending "%s" to SABnzbd.', data.get('name'))
14 years ago
params = {
'apikey': self.conf('api_key'),
14 years ago
'cat': self.conf('category'),
'mode': 'addurl',
'nzbname': self.createNzbName(data, movie),
14 years ago
}
if filedata:
if len(filedata) < 50:
log.error('No proper nzb available!')
return False
# If it's a .rar, it adds the .rar extension, otherwise it stays .nzb
nzb_filename = self.createFileName(data, filedata, movie)
params['mode'] = 'addfile'
else:
params['name'] = data.get('url')
url = cleanHost(self.conf('host')) + "api?" + tryUrlencode(params)
14 years ago
try:
if params.get('mode') is 'addfile':
data = self.urlopen(url, timeout = 60, params = {"nzbfile": (nzb_filename, filedata)}, multipart = True, show_error = False)
else:
data = self.urlopen(url, timeout = 60, show_error = False)
except:
log.error(traceback.format_exc())
14 years ago
return False
14 years ago
result = data.strip()
14 years ago
if not result:
log.error("SABnzbd didn't return anything.")
return False
log.debug("Result text from SAB: " + result[:40])
14 years ago
if result == "ok":
log.info("NZB sent to SAB successfully.")
return True
elif result == "Missing authentication":
log.error("Incorrect username/password.")
return False
else:
log.error("Unknown error: " + result[:40])
14 years ago
return False
def getdownloadfailed(self, data = {}, movie = {}, manual = False):
if self.isDisabled(manual) or not self.isCorrectType(data.get('type')):
return
log.info('Checking download status of "%s" at SABnzbd.', data.get('name'))
params = {
'apikey': self.conf('api_key'),
'mode': 'history',
'ouput': 'json'
}
url = cleanHost(self.conf('host')) + "api?" + tryUrlencode(params)
log.debug('Opening: %s', url)
history = json.load(urllib2.urlopen(url))
nzbname = self.createNzbName(data, movie)
# Go through history items
for slot in history['history']['slots']:
log.debug('Found %s in SabNZBd history, which has %s', (slot['name'], slot['status']))
if slot['name'] == nzbname and slot['status'] == 'Failed':
log.debug('%s failed downloading, deleting...', slot['name'])
# Delete failed download
params = {
'apikey': self.conf('api_key'),
'mode': 'history',
'name': 'delete',
'value' : slot['id']
}
url = cleanHost(self.conf('host')) + "api?" + tryUrlencode(params)
try:
data = self.urlopen(url, timeout = 60, show_error = False)
except:
log.error(traceback.format_exc())
# Return failed
return True
return False