def getDomain(self, url=''): forced_domain = self.conf('domain') if forced_domain: return cleanHost(forced_domain).rstrip('/') + url if not self.proxy_domain: for proxy in self.proxy_list: prop_name = 'proxy.%s' % proxy last_check = float(Env.prop(prop_name, default=0)) if last_check > time.time() - 1209600: continue data = '' try: data = self.urlopen(proxy, timeout=3, show_error=False) except: log.debug('Failed %s proxy %s', (self.getName(), proxy)) if self.correctProxy(data): log.debug('Using proxy for %s: %s', (self.getName(), proxy)) self.proxy_domain = proxy break Env.prop(prop_name, time.time()) if not self.proxy_domain: log.error( 'No %s proxies left, please add one in settings, or let us know which one to add on the forum.', self.getName()) return None return cleanHost(self.proxy_domain).rstrip('/') + url
def getDomain(self, url = ''): forced_domain = self.conf('domain') if forced_domain: return cleanHost(forced_domain).rstrip('/') + url if not self.proxy_domain: for proxy in self.proxy_list: prop_name = 'proxy.%s' % proxy last_check = float(Env.prop(prop_name, default = 0)) if last_check > time.time() - 1209600: continue data = '' try: data = self.urlopen(proxy, timeout = 3, show_error = False) except: log.debug('Failed %s proxy %s', (self.getName(), proxy)) if self.correctProxy(data): log.debug('Using proxy for %s: %s', (self.getName(), proxy)) self.proxy_domain = proxy break Env.prop(prop_name, time.time()) if not self.proxy_domain: log.error('No %s proxies left, please add one in settings, or let us know which one to add on the forum.', self.getName()) return None return cleanHost(self.proxy_domain).rstrip('/') + url
def connect(self, reconnect = False): # Already connected? if not reconnect and self.rt is not None: return self.rt url = cleanHost(self.conf('host'), protocol = True, ssl = self.conf('ssl')) parsed = urlparse(url) # rpc_url is only used on http/https scgi pass-through if parsed.scheme in ['http', 'https']: url += self.conf('rpc_url') if self.conf('username') and self.conf('password'): self.rt = RTorrent( url, self.conf('username'), self.conf('password') ) else: self.rt = RTorrent(url) self.error_msg = '' try: self.rt._verify_conn() except AssertionError as e: self.error_msg = e.message self.rt = None return self.rt
def addToLibrary(self, group = {}): if self.isDisabled(): return log.info('Sending notification to Plex') hosts = [cleanHost(x.strip() + ':32400') for x in self.conf('host').split(",")] for host in hosts: source_type = ['movie'] base_url = '%slibrary/sections' % host refresh_url = '%s/%%s/refresh' % base_url try: sections_xml = self.urlopen(base_url) xml_sections = minidom.parseString(sections_xml) sections = xml_sections.getElementsByTagName('Directory') for s in sections: if s.getAttribute('type') in source_type: url = refresh_url % s.getAttribute('key') x = self.urlopen(url) except: log.error('Plex library update failed for %s: %s', (host, traceback.format_exc())) return False return True
def connect(self, reconnect=False): """ Connect to the delugeRPC, re-use connection when already available :param reconnect: force reconnect :return: DelugeRPC instance """ # Load host from config and split out port. host = cleanHost(self.conf('host'), protocol=False).split(':') # Force host assignment if len(host) == 1: host.append(80) if not isInt(host[1]): log.error( 'Config properties are not filled in correctly, port is missing.' ) return False if not self.drpc or reconnect: self.drpc = DelugeRPC(host[0], port=host[1], username=self.conf('username'), password=self.conf('password')) return self.drpc
def download(self, data={}, movie={}, filedata=None): log.info('Sending "%s" to SABnzbd.', data.get('name')) params = { 'apikey': self.conf('api_key'), 'cat': self.conf('category'), 'mode': 'addurl', 'nzbname': self.createNzbName(data, movie), } if filedata: if len(filedata) < 50: log.error('No proper nzb available: %s', (filedata)) 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) try: if params.get('mode') is 'addfile': sab = self.urlopen( url, timeout=60, params={'nzbfile': (ss(nzb_filename), filedata)}, multipart=True, show_error=False, headers={'User-Agent': Env.getIdentifier()}) else: sab = self.urlopen(url, timeout=60, show_error=False, headers={'User-Agent': Env.getIdentifier()}) except URLError: log.error('Failed sending release, probably wrong HOST: %s', traceback.format_exc(0)) return False except: log.error( 'Failed sending release, use API key, NOT the NZB key: %s', traceback.format_exc(0)) return False result = sab.strip() if not result: log.error('SABnzbd didn\'t return anything.') return False log.debug('Result text from SAB: %s', result[:40]) if result[:2] == 'ok': log.info('NZB sent to SAB successfully.') return True else: log.error(result[:40]) return False
def download(self, data = None, media = None, filedata = None): if not media: media = {} if not data: data = {} response = False log.error('Sending "%s" (%s) to Synology.', (data['name'], data['protocol'])) # Load host from config and split out port. host = cleanHost(self.conf('host'), protocol = False).split(':') if not isInt(host[1]): log.error('Config properties are not filled in correctly, port is missing.') return False try: # Send request to Synology srpc = SynologyRPC(host[0], host[1], self.conf('username'), self.conf('password')) if data['protocol'] == 'torrent_magnet': log.info('Adding torrent URL %s', data['url']) response = srpc.create_task(url = data['url']) elif data['protocol'] in ['nzb', 'torrent']: log.info('Adding %s' % data['protocol']) if not filedata: log.error('No %s data found', data['protocol']) else: filename = data['name'] + '.' + data['protocol'] response = srpc.create_task(filename = filename, filedata = filedata) except: log.error('Exception while adding torrent: %s', traceback.format_exc()) finally: return self.downloadReturnId('') if response else False
def getDomain(self, url=''): if not self.domain: for proxy in self.proxy_list: prop_name = 'tpb_proxy.%s' % proxy last_check = float(Env.prop(prop_name, default=0)) if last_check > time.time() - 1209600: continue data = '' try: data = self.urlopen(proxy, timeout=3) except: log.debug('Failed tpb proxy %s', proxy) if 'title="Pirate Search"' in data: log.debug('Using proxy: %s', proxy) self.domain = proxy break Env.prop(prop_name, time.time()) if not self.domain: log.error( 'No TPB proxies left, please add one in settings, or let us know which one to add on the forum.' ) return None return cleanHost(self.domain).rstrip('/') + url
def test(self, **kwargs): host = self.conf('host') apikey = self.conf('apikey') message = self.test_message host = cleanHost(host) url = '%semby/Notifications/Admin' % (host) values = { 'Name': 'CouchPotato', 'Description': message, 'ImageUrl': 'https://raw.githubusercontent.com/RuudBurger/CouchPotatoServer/master/couchpotato/static/images/notify.couch.small.png' } data = json.dumps(values) try: req = urllib2.Request(url, data) req.add_header('X-MediaBrowser-Token', apikey) req.add_header('Content-Type', 'application/json') response = urllib2.urlopen(req) result = response.read() response.close() return {'success': True} except (urllib2.URLError, IOError), e: return False
def connect(self, reconnect=False): # Already connected? if not reconnect and self.rt is not None: return self.rt url = cleanHost(self.conf('host'), protocol=True, ssl=self.conf('ssl')) # Automatically add '+https' to 'httprpc' protocol if SSL is enabled if self.conf('ssl') and url.startswith('httprpc://'): url = url.replace('httprpc://', 'httprpc+https://') parsed = urlparse(url) # rpc_url is only used on http/https scgi pass-through if parsed.scheme in ['http', 'https']: url += self.conf('rpc_url') # Construct client self.rt = RTorrent(url, self.getAuth(), verify_ssl=self.getVerifySsl()) self.error_msg = '' try: self.rt.connection.verify() except AssertionError as e: self.error_msg = e.message self.rt = None return self.rt
def connect(self): # Load host from config and split out port. host = cleanHost(self.conf('host'), protocol = False).split(':') if not isInt(host[1]): log.error('Config properties are not filled in correctly, port is missing.') return False # This is where v4 and v5 begin to differ if(self.conf('version') == 'v4'): if not self.conf('api_key'): log.error('Config properties are not filled in correctly, API key is missing.') return False url = 'http://' + str(host[0]) + ':' + str(host[1]) + '/jsonrpc' client = JsonRpcClient(url, 'Token ' + self.conf('api_key')) self.hadouken_api = HadoukenAPIv4(client) return True else: auth_type = self.conf('auth_type') header = None if auth_type == 'api_key': header = 'Token ' + self.conf('api_key') elif auth_type == 'user_pass': header = 'Basic ' + b64encode(self.conf('auth_user') + ':' + self.conf('auth_pass')) url = 'http://' + str(host[0]) + ':' + str(host[1]) + '/api' client = JsonRpcClient(url, header) self.hadouken_api = HadoukenAPIv5(client) return True return False
def createBaseUrl(self): host = Env.setting("host") if host == "0.0.0.0" or host == "": host = "localhost" port = Env.setting("port") return "%s:%d%s" % (cleanHost(host).rstrip("/"), int(port), Env.get("web_base"))
def download(self, data=None, media=None, filedata=None): if not media: media = {} if not data: data = {} response = False log.error('Sending "%s" (%s) to Synology.', (data["name"], data["protocol"])) # Load host from config and split out port. host = cleanHost(self.conf("host"), protocol=False).split(":") if not isInt(host[1]): log.error("Config properties are not filled in correctly, port is missing.") return False try: # Send request to Synology srpc = SynologyRPC(host[0], host[1], self.conf("username"), self.conf("password")) if data["protocol"] == "torrent_magnet": log.info("Adding torrent URL %s", data["url"]) response = srpc.create_task(url=data["url"]) elif data["protocol"] in ["nzb", "torrent"]: log.info("Adding %s" % data["protocol"]) if not filedata: log.error("No %s data found", data["protocol"]) else: filename = data["name"] + "." + data["protocol"] response = srpc.create_task(filename=filename, filedata=filedata) except: log.error("Exception while adding torrent: %s", traceback.format_exc()) finally: return self.downloadReturnId("") if response else False
def createBaseUrl(self): host = Env.setting('host') if host == '0.0.0.0' or host == '': host = 'localhost' port = Env.setting('port') return '%s:%d%s' % (cleanHost(host).rstrip('/'), int(port), Env.get('web_base'))
def call(self, call, parameters = {}, repeat = False, auth = True, *args, **kwargs): # Login first if not self.session_id and auth: self.login() # Always add session id to request if self.session_id: parameters['sessionid'] = self.session_id params = tryUrlencode(parameters) url = cleanHost(self.conf('host')) + 'api/' + call url_opener = urllib2.build_opener(HTTPSHandler()) try: data = self.urlopen('%s?%s' % (url, params), opener = url_opener, *args, **kwargs) if data: return json.loads(data) except URLError, e: if hasattr(e, 'code') and e.code == 403: # Try login and do again if not repeat: self.login() return self.call(call, parameters = parameters, repeat = True, *args, **kwargs) log.error('Failed to parsing %s: %s', (self.getName(), traceback.format_exc()))
def getAuthorizationUrl(self): referer = getParam('host') callback_url = cleanHost(referer) + '%snotify.%s.credentials/' % (url_for('api.index').lstrip('/'), self.getName().lower()) oauth_consumer = oauth2.Consumer(self.consumer_key, self.consumer_secret) oauth_client = oauth2.Client(oauth_consumer) resp, content = oauth_client.request(self.urls['request'], 'POST', body = tryUrlencode({'oauth_callback': callback_url})) if resp['status'] != '200': log.error('Invalid response from Twitter requesting temp token: %s' % resp['status']) return jsonified({ 'success': False, }) else: self.request_token = dict(parse_qsl(content)) auth_url = self.urls['authorize'] + ("?oauth_token=%s" % self.request_token['oauth_token']) log.info('Redirecting to "%s"' % auth_url) return jsonified({ 'success': True, 'url': auth_url, })
def getAuthorizationUrl(self, host=None, **kwargs): callback_url = cleanHost(host) + '%snotify.%s.credentials/' % ( Env.get('api_base').lstrip('/'), self.getName().lower()) oauth_consumer = oauth2.Consumer(self.consumer_key, self.consumer_secret) oauth_client = oauth2.Client(oauth_consumer) resp, content = oauth_client.request( self.urls['request'], 'POST', body=tryUrlencode({'oauth_callback': callback_url})) if resp['status'] != '200': log.error( 'Invalid response from Twitter requesting temp token: %s', resp['status']) return { 'success': False, } else: self.request_token = dict(parse_qsl(content)) auth_url = self.urls['authorize'] + ( "?oauth_token=%s" % self.request_token['oauth_token']) log.info('Redirecting to "%s"', auth_url) return { 'success': True, 'url': auth_url, }
def call(self, call, parameters = None, repeat = False, auth = True, *args, **kwargs): # Login first if not parameters: parameters = {} if not self.session_id and auth: self.login() # Always add session id to request if self.session_id: parameters['sessionid'] = self.session_id params = tryUrlencode(parameters) url = cleanHost(self.conf('host'), ssl = self.conf('ssl')) + 'api/' + call try: data = self.urlopen('%s?%s' % (url, params), *args, **kwargs) if data: return json.loads(data) except URLError, e: if hasattr(e, 'code') and e.code == 403: # Try login and do again if not repeat: self.login() return self.call(call, parameters = parameters, repeat = True, **kwargs) log.error('Failed to parsing %s: %s', (self.getName(), traceback.format_exc()))
def getAllDownloadStatus(self, ids): log.debug('Checking NZBGet download status.') url = cleanHost(host=self.conf('host'), ssl=self.conf('ssl'), username=self.conf('username'), password=self.conf('password')) + self.rpc rpc = xmlrpclib.ServerProxy(url) try: if rpc.writelog('INFO', 'CouchPotato connected to check status'): log.debug('Successfully connected to NZBGet') else: log.info( 'Successfully connected to NZBGet, but unable to send a message' ) except socket.error: log.error( 'NZBGet is not responding. Please ensure that NZBGet is running and host setting is correct.' ) return [] except xmlrpclib.ProtocolError, e: if e.errcode == 401: log.error('Password is incorrect.') else: log.error('Protocol Error: %s', e) return []
def test(self, **kwargs): host = self.conf("host") apikey = self.conf("apikey") message = self.test_message host = cleanHost(host) url = "%semby/Notifications/Admin" % (host) values = { "Name": "CouchPotato", "Description": message, "ImageUrl": "https://raw.githubusercontent.com/CouchPotato/CouchPotatoServer/master/couchpotato/static/images/notify.couch.small.png", } data = json.dumps(values) try: req = urllib2.Request(url, data) req.add_header("X-MediaBrowser-Token", apikey) req.add_header("Content-Type", "application/json") response = urllib2.urlopen(req) result = response.read() response.close() return {"success": True} except (urllib2.URLError, IOError), e: return False
def download(self, data = None, media = None, filedata = None): if not media: media = {} if not data: data = {} if not filedata: log.error('Unable to get NZB file: %s', traceback.format_exc()) return False log.info('Sending "%s" to NZBGet.', data.get('name')) nzb_name = ss('%s.nzb' % self.createNzbName(data, media)) url = cleanHost(host = self.conf('host'), ssl = self.conf('ssl'), username = self.conf('username'), password = self.conf('password')) + self.rpc rpc = xmlrpclib.ServerProxy(url) try: if rpc.writelog('INFO', 'CouchPotato connected to drop off %s.' % nzb_name): log.debug('Successfully connected to NZBGet') else: log.info('Successfully connected to NZBGet, but unable to send a message') except socket.error: log.error('NZBGet is not responding. Please ensure that NZBGet is running and host setting is correct.') return False except xmlrpclib.ProtocolError, e: if e.errcode == 401: log.error('Password is incorrect.') else: log.error('Protocol Error: %s', e) return False
def createBaseUrl(self): host = Env.setting('host') if host == '0.0.0.0' or host == '': host = 'localhost' port = Env.setting('port') return '%s:%d%s' % (cleanHost(host).rstrip('/'), int(port), '/' + Env.setting('url_base').lstrip('/') if Env.setting('url_base') else '')
def getDomain(self, url = ''): if not self.domain: for proxy in self.proxy_list: prop_name = 'tpb_proxy.%s' % proxy last_check = float(Env.prop(prop_name, default = 0)) if last_check > time.time() - 1209600: continue data = '' try: data = self.urlopen(proxy, timeout = 3, show_error = False) except: log.debug('Failed tpb proxy %s', proxy) if 'title="Pirate Search"' in data: log.debug('Using proxy: %s', proxy) self.domain = proxy break Env.prop(prop_name, time.time()) if not self.domain: log.error('No TPB proxies left, please add one in settings, or let us know which one to add on the forum.') return None return cleanHost(self.domain).rstrip('/') + url
def call(self, request_params, use_json=True, **kwargs): url = cleanHost(self.conf('host'), ssl=self.conf('ssl')) + 'api?' + tryUrlencode( mergeDicts(request_params, { 'apikey': self.conf('api_key'), 'output': 'json' })) data = self.urlopen(url, timeout=60, show_error=False, verify_ssl=False, headers={'User-Agent': Env.getIdentifier()}, **kwargs) if use_json: d = json.loads(data) if d.get('error'): log.error('Error getting data from SABNZBd: %s', d.get('error')) return {} return d.get(request_params['mode']) or d else: return data
def addToLibrary(self, group={}): if self.isDisabled(): return log.info('Sending notification to Plex') hosts = [ cleanHost(x.strip() + ':32400') for x in self.conf('host').split(",") ] for host in hosts: source_type = ['movie'] base_url = '%slibrary/sections' % host refresh_url = '%s/%%s/refresh' % base_url try: sections_xml = self.urlopen(base_url) xml_sections = minidom.parseString(sections_xml) sections = xml_sections.getElementsByTagName('Directory') for s in sections: if s.getAttribute('type') in source_type: url = refresh_url % s.getAttribute('key') x = self.urlopen(url) except: log.error( 'Plex library update failed for %s, Media Server not running: %s', (host, traceback.format_exc(1))) return False return True
def createBaseUrl(self): host = Env.setting('host') if host == '0.0.0.0': host = 'localhost' port = Env.setting('port') return '%s:%d%s' % (cleanHost(host).rstrip('/'), int(port), '/' + Env.setting('url_base').lstrip('/') if Env.setting('url_base') else '')
def call(self, call, parameters = None, is_repeat = False, auth = True, *args, **kwargs): # Login first if not parameters: parameters = {} if not self.session_id and auth: self.login() # Always add session id to request if self.session_id: parameters['sessionid'] = self.session_id params = tryUrlencode(parameters) url = cleanHost(self.conf('host')) + 'api/' + call try: data = self.getJsonData('%s%s' % (url, '?' + params if params else ''), *args, cache_timeout = 0, show_error = False, **kwargs) if data: return data except HTTPError as e: sc = e.response.status_code if sc == 403: # Try login and do again if not is_repeat: self.login() return self.call(call, parameters = parameters, is_repeat = True, **kwargs) log.error('Failed to parsing %s: %s', (self.getName(), traceback.format_exc())) except: log.error('Failed to parsing %s: %s', (self.getName(), traceback.format_exc())) return {}
def connect(self, reconnect = False): # Already connected? if not reconnect and self.rt is not None: return self.rt url = cleanHost(self.conf('host'), protocol = True, ssl = self.conf('ssl')) # Automatically add '+https' to 'httprpc' protocol if SSL is enabled if self.conf('ssl') and url.startswith('httprpc://'): url = url.replace('httprpc://', 'httprpc+https://') parsed = urlparse(url) # rpc_url is only used on http/https scgi pass-through if parsed.scheme in ['http', 'https']: url += self.conf('rpc_url') self.rt = RTorrent( url, self.conf('username'), self.conf('password') ) self.error_msg = '' try: self.rt._verify_conn() except AssertionError as e: self.error_msg = e.message self.rt = None return self.rt
def connect(self, reconnect=False): # Already connected? if not reconnect and self.rt is not None: return self.rt url = cleanHost(self.conf('host'), protocol=True, ssl=self.conf('ssl')) parsed = urlparse(url) # rpc_url is only used on http/https scgi pass-through if parsed.scheme in ['http', 'https']: url += self.conf('rpc_url') if self.conf('username') and self.conf('password'): self.rt = RTorrent(url, self.conf('username'), self.conf('password')) else: self.rt = RTorrent(url) self.error_msg = '' try: self.rt._verify_conn() except AssertionError as e: self.error_msg = e.message self.rt = None return self.rt
def removeFailed(self, release_download): log.info('%s failed downloading, deleting...', release_download['name']) url = cleanHost(host=self.conf('host'), ssl=self.conf('ssl'), username=self.conf('username'), password=self.conf('password')) + self.rpc rpc = xmlrpclib.ServerProxy(url) try: if rpc.writelog('INFO', 'CouchPotato connected to delete some history'): log.debug('Successfully connected to NZBGet') else: log.info( 'Successfully connected to NZBGet, but unable to send a message' ) except socket.error: log.error( 'NZBGet is not responding. Please ensure that NZBGet is running and host setting is correct.' ) return False except xmlrpclib.ProtocolError, e: if e.errcode == 401: log.error('Password is incorrect.') else: log.error('Protocol Error: %s', e) return False
def createBaseUrl(self): host = Env.setting('host') if host == '0.0.0.0': host = 'localhost' port = Env.setting('port') return '%s:%d' % (cleanHost(host).rstrip('/'), int(port))
def download(self, data={}): if self.isDisabled() or not self.isCorrectType(data.get('type')): return log.info("Sending '%s' to SABnzbd." % data.get('name')) if self.conf('ppDir') and data.get('imdb_id'): try: pp_script_fn = self.buildPp(data.get('imdb_id')) except: log.info("Failed to create post-processing script.") pp_script_fn = False if not pp_script_fn: pp = False else: pp = True else: pp = False params = { 'apikey': self.conf('apikey'), 'cat': self.conf('category'), 'mode': 'addurl', 'name': data.get('url') } # sabNzbd complains about "invalid archive file" for newzbin urls # added using addurl, works fine with addid if data.get('addbyid'): params['mode'] = 'addid' if pp: params['script'] = pp_script_fn url = cleanHost(self.conf('host')) + "api?" + urlencode(params) log.info("URL: " + url) try: r = urllib2.urlopen(url, timeout=30) except: log.error("Unable to connect to SAB.") return False result = r.read().strip() if not result: log.error("SABnzbd didn't return anything.") return False log.debug("Result text from SAB: " + result) 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) return False
def download(self, data={}, movie={}): if self.isDisabled() or not self.isCorrectType(data.get('type')): return log.info("Sending '%s' to SABnzbd." % data.get('name')) if self.conf('ppDir') and data.get('imdb_id'): try: pp_script_fn = self.buildPp(data.get('imdb_id')) except: log.info("Failed to create post-processing script.") pp_script_fn = False if not pp_script_fn: pp = False else: pp = True else: pp = False params = { 'apikey': self.conf('api_key'), 'cat': self.conf('category'), 'mode': 'addfile', 'nzbname': '%s%s' % (data.get('name'), self.cpTag(movie)), } nzb_file = data.get('download')(url=data.get('url'), nzb_id=data.get('id')) if pp: params['script'] = pp_script_fn url = cleanHost(self.conf('host')) + "api?" + urlencode(params) try: data = self.urlopen( url, params={"nzbfile": (params['nzbname'] + ".nzb", nzb_file)}, multipart=True) except Exception: log.error("Unable to connect to SAB: %s" % traceback.format_exc()) return False result = data.strip() if not result: log.error("SABnzbd didn't return anything.") return False log.debug("Result text from SAB: " + result[:40]) 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]) return False
def download(self, data = {}): if self.isDisabled() or not self.isCorrectType(data.get('type')): return log.info("Sending '%s' to SABnzbd." % data.get('name')) if self.conf('ppDir') and data.get('imdb_id'): try: pp_script_fn = self.buildPp(data.get('imdb_id')) except: log.info("Failed to create post-processing script.") pp_script_fn = False if not pp_script_fn: pp = False else: pp = True else: pp = False params = { 'apikey': self.conf('apikey'), 'cat': self.conf('category'), 'mode': 'addurl', 'name': data.get('url') } # sabNzbd complains about "invalid archive file" for newzbin urls # added using addurl, works fine with addid if data.get('addbyid'): params['mode'] = 'addid' if pp: params['script'] = pp_script_fn url = cleanHost(self.conf('host')) + "api?" + urlencode(params) log.info("URL: " + url) try: r = urllib2.urlopen(url, timeout = 30) except: log.error("Unable to connect to SAB.") return False result = r.read().strip() if not result: log.error("SABnzbd didn't return anything.") return False log.debug("Result text from SAB: " + result) 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) return False
def getAuthorizationUrl(self, host=None, **kwargs): callback_url = cleanHost(host) + "%sautomation.trakt.credentials/" % (Env.get("api_base").lstrip("/")) log.debug("callback_url is %s", callback_url) target_url = self.urls["oauth"] + "?target=" + callback_url log.debug("target_url is %s", target_url) return {"success": True, "url": target_url}
def createItems(self, url, cache_key, host, single_cat = False, movie = None, quality = None, for_feed = False): results = [] data = self.getCache(cache_key, url, cache_timeout = 1800, headers = {'User-Agent': Env.getIdentifier()}) if data: try: try: data = XMLTree.fromstring(data) nzbs = self.getElements(data, 'channel/item') except Exception, e: log.debug('%s, %s' % (self.getName(), e)) return results results = [] for nzb in nzbs: date = '' size = 0 for item in nzb: if item.attrib.get('name') == 'size': size = item.attrib.get('value') elif item.attrib.get('name') == 'usenetdate': date = item.attrib.get('value') if date is '': log.debug('Date not parsed properly or not available for %s: %s' % (host['host'], self.getTextElement(nzb, "title"))) if size is 0: log.debug('Size not parsed properly or not available for %s: %s' % (host['host'], self.getTextElement(nzb, "title"))) id = self.getTextElement(nzb, "guid").split('/')[-1:].pop() new = { 'id': id, 'provider': self.getName(), 'type': 'nzb', 'name': self.getTextElement(nzb, "title"), 'age': self.calculateAge(int(time.mktime(parse(date).timetuple()))), 'size': int(size) / 1024 / 1024, 'url': (self.getUrl(host['host'], self.urls['download']) % id) + self.getApiExt(host), 'download': self.download, 'detail_url': '%sdetails/%s' % (cleanHost(host['host']), id), 'content': self.getTextElement(nzb, "description"), } if not for_feed: new['score'] = fireEvent('score.calculate', new, movie, single = True) is_correct_movie = fireEvent('searcher.correct_movie', nzb = new, movie = movie, quality = quality, imdb_results = True, single_category = single_cat, single = True) if is_correct_movie: results.append(new) self.found(new) else: results.append(new) return results except SyntaxError: log.error('Failed to parse XML response from Newznab: %s' % host) return results
def createItems(self, url, cache_key, host, movie = None, quality = None, for_feed = False): results = [] data = self.getCache(cache_key, url, cache_timeout = 1800, headers = {'User-Agent': Env.getIdentifier()}) if data: try: try: data = XMLTree.fromstring(data) nzbs = self.getElements(data, 'channel/item') except Exception, e: log.debug('%s, %s', (self.getName(), e)) return results results = [] for nzb in nzbs: date = '' size = 0 for item in nzb: if item.attrib.get('name') == 'size': size = item.attrib.get('value') elif item.attrib.get('name') == 'usenetdate': date = item.attrib.get('value') if date is '': log.debug('Date not parsed properly or not available for %s: %s', (host['host'], self.getTextElement(nzb, "title"))) if size is 0: log.debug('Size not parsed properly or not available for %s: %s', (host['host'], self.getTextElement(nzb, "title"))) id = self.getTextElement(nzb, "guid").split('/')[-1:].pop() new = { 'id': id, 'provider': self.getName(), 'provider_extra': host['host'], 'type': 'nzb', 'name': self.getTextElement(nzb, "title"), 'age': self.calculateAge(int(time.mktime(parse(date).timetuple()))), 'size': int(size) / 1024 / 1024, 'url': (self.getUrl(host['host'], self.urls['download']) % id) + self.getApiExt(host), 'download': self.download, 'detail_url': '%sdetails/%s' % (cleanHost(host['host']), id), 'content': self.getTextElement(nzb, "description"), } if not for_feed: is_correct_movie = fireEvent('searcher.correct_movie', nzb = new, movie = movie, quality = quality, imdb_results = True, single = True) if is_correct_movie: new['score'] = fireEvent('score.calculate', new, movie, single = True) results.append(new) self.found(new) else: results.append(new) return results except SyntaxError: log.error('Failed to parse XML response from Newznab: %s', host) return results
def test(self): host = cleanHost(self.conf("host"), protocol=False).split(":") try: srpc = SynologyRPC(host[0], host[1], self.conf("username"), self.conf("password")) test_result = srpc.test() except: return False return test_result
def connect(self): # Load host from config and split out port. host = cleanHost(self.conf('host')).rstrip('/').rsplit(':', 1) if not isInt(host[1]): log.error('Config properties are not filled in correctly, port is missing.') return False self.trpc = TransmissionRPC(host[0], port = host[1], rpc_url = self.conf('rpc_url').strip('/ '), username = self.conf('username'), password = self.conf('password')) return self.trpc
def test(self): host = cleanHost(self.conf('host'), protocol = False).split(':') try: srpc = SynologyRPC(host[0], host[1], self.conf('username'), self.conf('password')) test_result = srpc.test() except: return False return test_result
def createHost(self, host, port = None): h = cleanHost(host) p = urlparse(h) h = h.rstrip('/') if port and not p.port: h += ':%s' % port return h
def download(self, data = {}, movie = {}): if self.isDisabled() or not self.isCorrectType(data.get('type')): return log.info("Sending '%s' to SABnzbd." % data.get('name')) if self.conf('ppDir') and data.get('imdb_id'): try: pp_script_fn = self.buildPp(data.get('imdb_id')) except: log.info("Failed to create post-processing script.") pp_script_fn = False if not pp_script_fn: pp = False else: pp = True else: pp = False params = { 'apikey': self.conf('api_key'), 'cat': self.conf('category'), 'mode': 'addfile', 'nzbname': '%s%s' % (data.get('name'), self.cpTag(movie)), } nzb_file = data.get('download')(url = data.get('url'), nzb_id = data.get('id')) if pp: params['script'] = pp_script_fn url = cleanHost(self.conf('host')) + "api?" + urlencode(params) try: data = self.urlopen(url, params = {"nzbfile": (params['nzbname'] + ".nzb", nzb_file)}, multipart = True) except Exception: log.error("Unable to connect to SAB: %s" % traceback.format_exc()) return False result = data.strip() if not result: log.error("SABnzbd didn't return anything.") return False log.debug("Result text from SAB: " + result[:40]) 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]) return False
def download(self, data={}, movie={}, manual=False, filedata=None): if self.isDisabled(manual) or not self.isCorrectType(data.get('type')): return log.info('Sending "%s" to SABnzbd.', data.get('name')) params = { 'apikey': self.conf('api_key'), 'cat': self.conf('category'), 'mode': 'addurl', 'nzbname': self.createNzbName(data, movie), } 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) try: if params.get('mode') is 'addfile': sab = self.urlopen( url, timeout=60, params={"nzbfile": (nzb_filename, filedata)}, multipart=True, show_error=False) else: sab = self.urlopen(url, timeout=60, show_error=False) except: log.error('Failed sending release: %s', traceback.format_exc()) return False result = sab.strip() if not result: log.error("SABnzbd didn't return anything.") return False log.debug("Result text from SAB: " + result[:40]) 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]) return False
def connect(self): # Load host from config and split out port. host = cleanHost(self.conf('host'), protocol = False).split(':') if not isInt(host[1]): log.error('Config properties are not filled in correctly, port is missing.') return False self.utorrent_api = uTorrentAPI(host[0], port = host[1], username = self.conf('username'), password = self.conf('password')) return self.utorrent_api
def createHost(self, host, port=None, use_https=False): h = cleanHost(host, True, use_https) p = urlparse(h) h = h.rstrip('/') if port and not p.port: h += ':%s' % port return h
def createHost(self, host, port=None): h = cleanHost(host) p = urlparse(h) h = h.rstrip('/') if port and not p.port: h += ':%s' % port return h
def test(self): host = cleanHost(self.conf('host'), protocol=False).split(':') try: srpc = SynologyRPC(host[0], host[1], self.conf('username'), self.conf('password')) test_result = srpc.test() except: return False return test_result
def connect(self, reconnect = False): # Load host from config and split out port. host = cleanHost(self.conf('host'), protocol = False).split(':') if not isInt(host[1]): log.error('Config properties are not filled in correctly, port is missing.') return False if not self.drpc or reconnect: self.drpc = DelugeRPC(host[0], port = host[1], username = self.conf('username'), password = self.conf('password')) return self.drpc
def getAuthorizationUrl(self, host = None, **kwargs): callback_url = cleanHost(host) + '%sautomation.trakt.credentials/' % (Env.get('api_base').lstrip('/')) log.debug('callback_url is %s', callback_url) target_url = self.urls['oauth'] + "?target=" + callback_url log.debug('target_url is %s', target_url) return { 'success': True, 'url': target_url, }
def _searchOnHost(self, host, movie, quality, results): arguments = tryUrlencode({ 'imdbid': movie['library']['identifier'].replace('tt', ''), 'apikey': host['api_key'], 'extended': 1 }) url = '%s&%s' % (self.getUrl(host['host'], self.urls['search']), arguments) nzbs = self.getRSSData(url, cache_timeout=1800, headers={'User-Agent': Env.getIdentifier()}) for nzb in nzbs: date = None for item in nzb: if item.attrib.get('name') == 'usenetdate': date = item.attrib.get('value') break if not date: date = self.getTextElement(nzb, 'pubDate') nzb_id = self.getTextElement(nzb, 'guid').split('/')[-1:].pop() name = self.getTextElement(nzb, 'title') if not name: continue results.append({ 'id': nzb_id, 'provider_extra': host['host'], 'name': self.getTextElement(nzb, 'title'), 'age': self.calculateAge(int(time.mktime(parse(date).timetuple()))), 'size': int(self.getElement(nzb, 'enclosure').attrib['length']) / 1024 / 1024, 'url': (self.getUrl(host['host'], self.urls['download']) % nzb_id) + self.getApiExt(host), 'detail_url': '%sdetails/%s' % (cleanHost(host['host']), nzb_id), 'content': self.getTextElement(nzb, 'description'), })
def getAuthorizationUrl(self, host = None, **kwargs): callback_url = cleanHost(host) + '%sdownloader.putio.credentials/' % (Env.get('api_base').lstrip('/')) log.debug('callback_url is %s', callback_url) target_url = self.oauth_authenticate + "?target=" + callback_url log.debug('target_url is %s', target_url) return { 'success': True, 'url': target_url, }
def _searchOnHost(self, host, movie, quality, results): arguments = tryUrlencode({ 'imdbid': movie['library']['identifier'].replace('tt', ''), 'apikey': host['api_key'], 'extended': 1 }) url = '%s&%s' % (self.getUrl(host['host'], self.urls['search']), arguments) nzbs = self.getRSSData(url, cache_timeout = 1800, headers = {'User-Agent': Env.getIdentifier()}) for nzb in nzbs: date = None spotter = None for item in nzb: if date and spotter: break if item.attrib.get('name') == 'usenetdate': date = item.attrib.get('value') break # Get the name of the person who posts the spot if item.attrib.get('name') == 'poster': if "@spot.net" in item.attrib.get('value'): spotter = item.attrib.get('value').split("@")[0] continue if not date: date = self.getTextElement(nzb, 'pubDate') nzb_id = self.getTextElement(nzb, 'guid').split('/')[-1:].pop() name = self.getTextElement(nzb, 'title') if not name: continue name_extra = '' if spotter: name_extra = spotter results.append({ 'id': nzb_id, 'provider_extra': urlparse(host['host']).hostname or host['host'], 'name': name, 'name_extra': name_extra, 'age': self.calculateAge(int(time.mktime(parse(date).timetuple()))), 'size': int(self.getElement(nzb, 'enclosure').attrib['length']) / 1024 / 1024, 'url': (self.getUrl(host['host'], self.urls['download']) % tryUrlencode(nzb_id)) + self.getApiExt(host), 'detail_url': '%sdetails/%s' % (cleanHost(host['host']), tryUrlencode(nzb_id)), 'content': self.getTextElement(nzb, 'description'), 'score': host['extra_score'], })
def connect(self): if self.qb is not None: self.qb.logout() url = cleanHost(self.conf('host'), protocol = True, ssl = False) if self.conf('username') and self.conf('password'): self.qb = QBittorrentClient(url) self.qb.login(username=self.conf('username'), password=self.conf('password')) else: self.qb = QBittorrentClient(url) return self.qb._is_authenticated
def download(self, data=None, media=None, filedata=None): """ Send a torrent/nzb file to the downloader :param data: dict returned from provider Contains the release information :param media: media dict with information Used for creating the filename when possible :param filedata: downloaded torrent/nzb filedata The file gets downloaded in the searcher and send to this function This is done to have failed checking before using the downloader, so the downloader doesn't need to worry about that :return: boolean One faile returns false, but the downloaded should log his own errors """ if not media: media = {} if not data: data = {} response = False log.error('Sending "%s" (%s) to Synology.', (data['name'], data['protocol'])) # Load host from config and split out port. host = cleanHost(self.conf('host'), protocol=False).split(':') if not isInt(host[1]): log.error( 'Config properties are not filled in correctly, port is missing.' ) return False try: # Send request to Synology srpc = SynologyRPC(host[0], host[1], self.conf('username'), self.conf('password'), self.conf('destination')) if data['protocol'] == 'torrent_magnet': log.info('Adding torrent URL %s', data['url']) response = srpc.create_task(url=data['url']) elif data['protocol'] in ['nzb', 'torrent']: log.info('Adding %s' % data['protocol']) if not filedata: log.error('No %s data found', data['protocol']) else: filename = data['name'] + '.' + data['protocol'] response = srpc.create_task(filename=filename, filedata=filedata) except: log.error('Exception while adding torrent: %s', traceback.format_exc()) finally: return self.downloadReturnId('') if response else False