def submit(self, captcha, captchaType="file", match=None): req = get_request() #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) try: res = self.load(self.SUBMIT_URL, post={ 'vendor_key': self.PYLOAD_KEY, 'key': self.config.get('passkey'), 'gen_task_id': "1", 'file': (pycurl.FORM_FILE, captcha) }, req=req) finally: req.close() data = dict(x.split(' ', 1) for x in res.splitlines()) if not data or "Value" not in data: raise BypassCaptchaException(res) result = data['Value'] ticket = data['TaskId'] self.log_debug("Result %s : %s" % (ticket, result)) return ticket, result
def send_to_transmission(self, url): transmission_rpc_url = self.config.get('rpc_url') client_request_id = self.classname + "".join(random.choice('0123456789ABCDEF') for _i in xrange(4)) req = get_request() try: response = self.load(transmission_rpc_url, post=json.dumps({'arguments': {'filename': url}, 'method' : 'torrent-add', 'tag' : client_request_id}), req=req) except Exception, e: if isinstance(e, BadHeader) and e.code == 409: headers = dict(re.findall(r'(?P<name>.+?): (?P<value>.+?)\r?\n', req.header)) session_id = headers['X-Transmission-Session-Id'] req.c.setopt(pycurl.HTTPHEADER, ["X-Transmission-Session-Id: %s" % session_id]) try: response = self.load(transmission_rpc_url, post=json.dumps({'arguments': {'filename': url}, 'method' : 'torrent-add', 'tag' : client_request_id}), req=req) res = json.loads(response) if "result" in res: self.log_debug("Result: %s" % res['result']) except Exception, e: self.log_error(e)
def submit(self, captcha, captchaType="file", match=None): req = get_request() #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) try: #@NOTE: Workaround multipart-post bug in HTTPRequest.py if re.match("^\w*$", self.get_config('password')): multipart = True data = (pycurl.FORM_FILE, captcha) else: multipart = False with open(captcha, 'rb') as f: data = f.read() data = b64encode(data) res = self.load(self.SUBMIT_URL, post={'action': "UPLOADCAPTCHA", 'username': self.get_config('username'), 'password': self.get_config('password'), 'file': data}, multipart=multipart, req=req) finally: req.close() if res.startswith("ERROR"): raise ImageTyperzException(res) else: data = res.split('|') if len(data) == 2: ticket, result = data else: raise ImageTyperzException("Unknown response: %s" % res) return ticket, result
def _process_captcha(self, task): task.data['ticket'] = ticket = uuid.uuid4() result = None with open(task.captchaFile, 'rb') as f: data = f.read() req = get_request() #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) try: result = self.load(self.API_URL, post={ 'action': "upload", 'key': self.config.get('passkey'), 'file': base64.b64encode(data), 'gen_task_id': ticket }, req=req) finally: req.close() self.log_debug("Result %s : %s" % (ticket, result)) task.setResult(result)
def submit(self, captcha, captchaType="file", match=None): req = get_request() #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) try: res = self.load(self.SUBMIT_URL, post={'vendor_key': self.PYLOAD_KEY, 'key': self.get_config('passkey'), 'gen_task_id': "1", 'file': (pycurl.FORM_FILE, captcha)}, req=req) finally: req.close() data = dict(x.split(' ', 1) for x in res.splitlines()) if not data or "Value" not in data: raise BypassCaptchaException(res) result = data['Value'] ticket = data['TaskId'] self.log_debug("Result %s : %s" % (ticket, result)) return ticket, result
def get_info(urls): h = get_request() h.c.setopt(pycurl.HTTPHEADER, ["Accept: text/html", "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"]) for url in urls: html = h.load(url) yield parse_fileInfo(MegaRapidCz, url, html)
def send(self, event, msg, key): req = get_request() req.c.setopt(pycurl.HTTPHEADER, ["Access-Token: %s" % str(key)]) self.load("https://api.pushbullet.com/v2/pushes", post={'type' : 'note', 'title' : event, 'message': msg}, req=req)
def send(self, event, msg, key): req = get_request() req.c.setopt(pycurl.HTTPHEADER, ["Access-Token: %s" % str(key)]) self.load("https://api.pushbullet.com/v2/pushes", post={ 'type': 'note', 'title': event, 'message': msg }, req=req)
def send(self, event, msg, key): self.log_info("Sending message to Telegram") req = get_request() url = 'https://api.telegram.org/bot{0}/sendMessage'.format(str(key)) self.load(url, post={ 'chat_id': self.get_chatid(), 'text': '[{0}] {1}'.format(event, msg) }, req=req)
def api_response(self, api="captcha", post=False, multipart=False): req = get_request() req.c.setopt(pycurl.HTTPHEADER, [ "Accept: application/json", "User-Agent: pyLoad %s" % self.pyload.version ]) if post: if not isinstance(post, dict): post = {} post.update({ 'username': self.config.get('username'), 'password': self.config.get('password') }) res = None try: html = self.load("%s%s" % (self.API_URL, api), post=post, multipart=multipart, req=req) self.log_debug(html) res = json.loads(html) if "error" in res: raise DeathByCaptchaException(res['error']) elif "status" not in res: raise DeathByCaptchaException(str(res)) except BadHeader as e: if e.code == 403: raise DeathByCaptchaException('not-logged-in') elif e.code == 413: raise DeathByCaptchaException('invalid-captcha') elif e.code == 503: raise DeathByCaptchaException('service-overload') elif e.code in (400, 405): raise DeathByCaptchaException('invalid-request') else: raise finally: req.close() return res
def send_to_transmission(self, url): transmission_rpc_url = self.config.get('rpc_url') client_request_id = self.classname + \ "".join(random.choice('0123456789ABCDEF') for _i in range(4)) req = get_request() try: response = self.load(transmission_rpc_url, post=json.dumps({ 'arguments': { 'filename': url }, 'method': 'torrent-add', 'tag': client_request_id }), req=req) except Exception as e: if isinstance(e, BadHeader) and e.code == 409: headers = dict( re.findall(r'(?P<name>.+?): (?P<value>.+?)\r?\n', req.header)) session_id = headers['X-Transmission-Session-Id'] req.c.setopt(pycurl.HTTPHEADER, ["X-Transmission-Session-Id: %s" % session_id]) try: response = self.load(transmission_rpc_url, post=json.dumps({ 'arguments': { 'filename': url }, 'method': 'torrent-add', 'tag': client_request_id }), req=req) res = json.loads(response) if "result" in res: self.log_debug("Result: %s" % res['result']) except Exception as e: self.log_error(e) else: self.log_error(e)
def api_response(self, api="captcha", post=False, multipart=False): req = get_request() req.c.setopt(pycurl.HTTPHEADER, ["Accept: application/json", "User-Agent: pyLoad %s" % self.pyload.version]) if post: if not isinstance(post, dict): post = {} post.update({'username': self.config.get('username'), 'password': self.config.get('password')}) res = None try: html = self.load("%s%s" % (self.API_URL, api), post=post, multipart=multipart, req=req) self.log_debug(html) res = json.loads(html) if "error" in res: raise DeathByCaptchaException(res['error']) elif "status" not in res: raise DeathByCaptchaException(str(res)) except BadHeader, e: if e.code == 403: raise DeathByCaptchaException('not-logged-in') elif e.code == 413: raise DeathByCaptchaException('invalid-captcha') elif e.code == 503: raise DeathByCaptchaException('service-overload') elif e.code in (400, 405): raise DeathByCaptchaException('invalid-request') else: raise
def submit(self, captcha, captchaType="file", match=None): req = get_request() #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) try: #@NOTE: Workaround multipart-post bug in HTTPRequest.py if re.match("^\w*$", self.config.get('password')): multipart = True data = (pycurl.FORM_FILE, captcha) else: multipart = False with open(captcha, 'rb') as f: data = f.read() data = base64.b64encode(data) res = self.load(self.SUBMIT_URL, post={ 'action': "UPLOADCAPTCHA", 'username': self.config.get('username'), 'password': self.config.get('password'), 'file': data }, multipart=multipart, req=req) finally: req.close() if res.startswith("ERROR"): raise ImageTyperzException(res) else: data = res.split('|') if len(data) == 2: ticket, result = data else: raise ImageTyperzException("Unknown response: %s" % res) return ticket, result
def _process_captcha(self, task): task.data['ticket'] = ticket = uuid.uuid4() result = None with open(task.captchaFile, 'rb') as f: data = f.read() req = get_request() #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) try: result = self.load(self.API_URL, post={'action' : "upload", 'key' : self.get_config('passkey'), 'file' : b64encode(data), 'gen_task_id': ticket}, req=req) finally: req.close() self.log_debug("Result %s : %s" % (ticket, result)) task.setResult(result)
def load(self, url, get={}, post={}, ref=True, cookies=True, just_header=False, decode=True, multipart=False, redirect=True, req=None): """ Load content at url and returns it :param url: :param get: :param post: :param ref: :param cookies: :param just_header: If True only the header will be retrieved and returned as dict :param decode: Wether to decode the output according to http header, should be True in most cases :return: Loaded content """ if self.pyload.debug: self.log_debug( "LOAD URL " + url, *[ "%s=%s" % (key, value) for key, value in locals().items() if key not in ("self", "url", "_[1]") ]) url = fixurl(url, unquote=True) #: Recheck in 0.4.10 if req is False: req = get_request() req.setOption("timeout", 60) # @TODO: Remove in 0.4.10 elif not req: req = self.req #@TODO: Move to network in 0.4.10 if isinstance(cookies, list): set_cookies(req.cj, cookies) #@TODO: Move to network in 0.4.10 if not redirect: # @NOTE: req can be a HTTPRequest or a Browser object (req.http if hasattr(req, "http") else req).c.setopt( pycurl.FOLLOWLOCATION, 0) elif type(redirect) is int: # @NOTE: req can be a HTTPRequest or a Browser object (req.http if hasattr(req, "http") else req).c.setopt( pycurl.MAXREDIRS, redirect) #@TODO: Move to network in 0.4.10 if isinstance(ref, six.string_types): req.lastURL = ref html = req.load( url, get, post, bool(ref), bool(cookies), just_header, multipart, decode is True) # @TODO: Fix network multipart in 0.4.10 #@TODO: Move to network in 0.4.10 if not redirect: # @NOTE: req can be a HTTPRequest or a Browser object (req.http if hasattr(req, "http") else req).c.setopt( pycurl.FOLLOWLOCATION, 1) elif type(redirect) is int: maxredirs = int( self.pyload.api.getConfigValue( "UserAgentSwitcher", "maxredirs", "plugin")) or 5 # @TODO: Remove `int` in 0.4.10 # @NOTE: req can be a HTTPRequest or a Browser object (req.http if hasattr(req, "http") else req).c.setopt( pycurl.MAXREDIRS, maxredirs) #@TODO: Move to network in 0.4.10 if decode: html = html_unescape(html) #@TODO: Move to network in 0.4.10 if isinstance(decode, six.string_types): html = _decode(html, decode) self.last_html = html if self.pyload.debug: self.dump_html() #@TODO: Move to network in 0.4.10 header = {'code': req.code, 'url': req.lastEffectiveURL} # @NOTE: req can be a HTTPRequest or a Browser object header.update( parse_html_header( req.http.header if hasattr(req, "http") else req.header)) self.last_header = header if just_header: return header else: return html
def upload(self, path, url, get={}, ref=True, cookies=True, just_header=False, decode=True, redirect=True, req=None): # @TODO: This should really go to HTTPRequest.py """ Uploads a file at url and returns response content :param url: :param get: :param ref: :param cookies: :param just_header: If True only the header will be retrieved and returned as dict :param decode: Wether to decode the output according to http header, should be True in most cases :return: Response content """ if self.pyload.debug: self.log_debug( "UPLOAD URL " + url, *[ "%s=%s" % (key, value) for key, value in locals().items() if key not in ("self", "url", "_[1]") ]) with open(path, 'rb') as f: url = fixurl(url, unquote=True) #: Recheck in 0.4.10 if req is False: req = get_request() req.setOption("timeout", 60) # @TODO: Remove in 0.4.10 elif not req: req = self.req if isinstance(cookies, list): set_cookies(req.cj, cookies) http_req = self.req.http if hasattr(self.req, "http") else self.req if not redirect: # @NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.FOLLOWLOCATION, 0) elif type(redirect) is int: # @NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.MAXREDIRS, redirect) if isinstance(ref, basestring): http_req.lastURL = ref http_req.setRequestContext(url, get, {}, bool(ref), bool(cookies), False) http_req.header = "" http_req.c.setopt(pycurl.HTTPHEADER, http_req.headers) http_req.c.setopt(pycurl.UPLOAD, 1) http_req.c.setopt(pycurl.READFUNCTION, f.read) http_req.c.setopt(pycurl.INFILESIZE, os.path.getsize(path)) if just_header: http_req.c.setopt(pycurl.FOLLOWLOCATION, 0) http_req.c.setopt(pycurl.NOBODY, 1) http_req.c.perform() html = http_req.header http_req.c.setopt(pycurl.FOLLOWLOCATION, 1) http_req.c.setopt(pycurl.NOBODY, 0) else: http_req.c.perform() html = http_req.getResponse() http_req.c.setopt(pycurl.UPLOAD, 0) http_req.c.setopt(pycurl.INFILESIZE, 0) http_req.c.setopt(pycurl.POSTFIELDS, "") http_req.lastEffectiveURL = http_req.c.getinfo( pycurl.EFFECTIVE_URL) http_req.addCookies() try: http_req.code = http_req.verifyHeader() finally: http_req.rep.close() http_req.rep = None if decode is True: html = http_req.decodeResponse(html) if not redirect: http_req.c.setopt(pycurl.FOLLOWLOCATION, 1) elif type(redirect) is int: maxredirs = int( self.pyload.api.getConfigValue( "UserAgentSwitcher", "maxredirs", "plugin")) or 5 # @TODO: Remove `int` in 0.4.10 # @NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.MAXREDIRS, maxredirs) if decode: html = html_unescape(html) #@TODO: Move to network in 0.4.10 if isinstance(decode, basestring): html = _decode(html, decode) self.last_html = html if self.pyload.debug: self.dump_html() #@TODO: Move to network in 0.4.10 header = {'code': req.code, 'url': req.lastEffectiveURL} # @NOTE: req can be a HTTPRequest or a Browser object header.update(parse_html_header(http_req.header)) self.last_header = header if just_header: return header else: return html
def send(self, event, msg, key): req = get_request() self.log_info("Sending message to discord") self.load(self.get_key(), post={'content': event + '\n' + msg}, req=req)
class CaptchaBrotherhood(Addon): __name__ = "CaptchaBrotherhood" __type__ = "hook" __version__ = "0.15" __status__ = "testing" __config__ = [("activated", "bool", "Activated", False), ("username", "str", "Username", ""), ("password", "password", "Password", ""), ("check_client", "bool", "Don't use if client is connected", True)] __description__ = """Send captchas to CaptchaBrotherhood.com""" __license__ = "GPLv3" __authors__ = [("RaNaN", "*****@*****.**"), ("zoidberg", "*****@*****.**")] API_URL = "http://www.captchabrotherhood.com/" def get_credits(self): res = self.load(self.API_URL + "askCredits.aspx", get={ 'username': self.config.get('username'), 'password': self.config.get('password') }) if not res.startswith("OK"): raise CaptchaBrotherhoodException(res) else: credits = int(res[3:]) self.log_info(_("%d credits left") % credits) self.info['credits'] = credits return credits def submit(self, captcha, captchaType="file", match=None): try: img = Image.open(captcha) output = StringIO.StringIO() self.log_debug("CAPTCHA IMAGE", img, img.format, img.mode) if img.format in ("GIF", "JPEG"): img.save(output, img.format) else: if img.mode != "RGB": img = img.convert("RGB") img.save(output, "JPEG") data = output.getvalue() output.close() except Exception, e: raise CaptchaBrotherhoodException( "Reading or converting captcha image failed: %s" % e) req = get_request() url = "%ssendNewCaptcha.aspx?%s" % ( self.API_URL, urllib.urlencode({ 'username': self.config.get('username'), 'password': self.config.get('password'), 'captchaSource': "pyLoad", 'timeout': "80" })) req.c.setopt(pycurl.URL, url) req.c.setopt(pycurl.POST, 1) req.c.setopt(pycurl.POSTFIELDS, data) req.c.setopt(pycurl.HTTPHEADER, ["Content-Type: text/html"]) try: req.c.perform() res = req.getResponse() except Exception, e: raise CaptchaBrotherhoodException("Submit captcha image failed")
def load(self, url, get={}, post={}, ref=True, cookies=True, just_header=False, decode=True, multipart=False, redirect=True, req=None): """ Load content at url and returns it :param url: :param get: :param post: :param ref: :param cookies: :param just_header: If True only the header will be retrieved and returned as dict :param decode: Wether to decode the output according to http header, should be True in most cases :return: Loaded content """ if self.pyload.debug: self.log_debug("LOAD URL " + url, *["%s=%s" % (key, value) for key, value in locals().items() if key not in ("self", "url", "_[1]")]) url = fixurl(url, unquote=True) #: Recheck in 0.4.10 if req is False: req = get_request() req.setOption("timeout", 60) #@TODO: Remove in 0.4.10 elif not req: req = self.req #@TODO: Move to network in 0.4.10 if isinstance(cookies, list): set_cookies(req.cj, cookies) #@TODO: Move to network in 0.4.10 if not redirect: req.http.c.setopt(pycurl.FOLLOWLOCATION, 0) elif type(redirect) is int: req.http.c.setopt(pycurl.MAXREDIRS, redirect) #@TODO: Move to network in 0.4.10 if isinstance(ref, basestring): req.lastURL = ref html = req.load(url, get, post, bool(ref), bool(cookies), just_header, multipart, decode is True) #@TODO: Fix network multipart in 0.4.10 #@TODO: Move to network in 0.4.10 if not redirect: req.http.c.setopt(pycurl.FOLLOWLOCATION, 1) elif type(redirect) is int: maxredirs = int(self.pyload.api.getConfigValue("UserAgentSwitcher", "maxredirs", "plugin")) or 5 #@TODO: Remove `int` in 0.4.10 req.http.c.setopt(pycurl.MAXREDIRS, maxredirs) #@TODO: Move to network in 0.4.10 if decode: html = html_unescape(html) #@TODO: Move to network in 0.4.10 if isinstance(decode, basestring): html = _decode(html, decode) self.last_html = html if self.pyload.debug: self.dump_html() #@TODO: Move to network in 0.4.10 header = {'code': req.code, 'url': req.lastEffectiveURL} header.update(parse_html_header(req.http.header if hasattr(req, "http") else req.header)) #@NOTE: req can be a HTTPRequest or a Browser object self.last_header = header if just_header: return header else: return html