def submit(self, captcha, captcha_type="file", match=None): with get_request() as req: #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) 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, ) 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(f"Result {ticket} : {result}") return ticket, result
def send(self, event, msg, key): req = get_request() req.c.setopt(pycurl.HTTPHEADER, ["Access-Token: {}".format(str(key))]) self.load( "https://api.pushbullet.com/v2/pushes", post={"type": "note", "title": event, "message": msg}, req=req, )
def api_response(self, api="captcha", post=False, multipart=False): with get_request() as req: req.c.setopt( pycurl.HTTPHEADER, [ "Accept: application/json", f"User-Agent: pyLoad {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( "{}{}".format(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 exc: if exc.code == 403: raise DeathByCaptchaException("not-logged-in") elif exc.code == 413: raise DeathByCaptchaException("invalid-captcha") elif exc.code == 503: raise DeathByCaptchaException("service-overload") elif exc.code in (400, 405): raise DeathByCaptchaException("invalid-request") else: raise 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 _ 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 exc: if isinstance(exc, BadHeader) and exc.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, [f"X-Transmission-Session-Id: {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(f"Result: {res['result']}") except Exception as exc: self.log_error(exc) else: self.log_error(exc)
def send_to_transmission(self, url): transmission_rpc_url = self.config.get("rpc_url") client_request_id = self.classname + "".join( random.choice("0123456789ABCDEF") for _ 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 exc: if isinstance(exc, BadHeader) and exc.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, [f"X-Transmission-Session-Id: {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(f"Result: {res['result']}") except Exception as exc: self.log_error(exc) else: self.log_error(exc)
def submit(self, captcha, captcha_type="file", match=None): with get_request() as req: #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) # NOTE: Workaround multipart-post bug in HTTPRequest.py if re.match(r"^\w*$", self.config.get("password")): multipart = True data = (pycurl.FORM_FILE, captcha) else: multipart = False with open(captcha, mode="rb") as fp: data = fp.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, ) if res.startswith("ERROR"): raise ImageTyperzException(res) else: data = res.split("|") if len(data) == 2: ticket, result = data else: raise ImageTyperzException("Unknown response: {}".format(res)) return ticket, result
def submit(self, captcha, captcha_type="file", match=None): with get_request() as req: #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) # NOTE: Workaround multipart-post bug in HTTPRequest.py if re.match(r"^\w*$", self.config.get("password")): multipart = True data = (pycurl.FORM_FILE, captcha) else: multipart = False with open(captcha, mode="rb") as file: data = file.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, ) if res.startswith("ERROR"): raise ImageTyperzException(res) else: data = res.split("|") if len(data) == 2: ticket, result = data else: raise ImageTyperzException("Unknown response: {}".format(res)) return ticket, result
def _process_captcha(self, task): task.data["ticket"] = ticket = uuid.uuid4() result = None with open(task.captcha_params["file"], mode="rb") as fp: data = fp.read() with get_request() as req: #: Raise timeout threshold req.c.setopt(pycurl.LOW_SPEED_TIME, 80) result = self.load( self.API_URL, post={ "action": "upload", "key": self.config.get("passkey"), "file": base64.b64encode(data), "gen_task_id": ticket, }, req=req, ) self.log_debug(f"Result {ticket}: {result}") task.set_result(result)
def api_info(cls, url): info = {} file_id = re.match(cls.__pattern__, url).group("ID") req = get_request() req.c.setopt(pycurl.HTTPHEADER, ["Accept: application/json, text/plain, */*"]) file_info = json.loads( req.load("https://www.fshare.vn/api/v3/files/folder", get={"linkcode": file_id})) req.close() if file_info.get("status") == 404: info["status"] = 1 else: info.update({ "name": file_info["current"]["name"], "size": file_info["current"]["size"], "status": 2, }) return info
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, *[ "{}={}".format(key, value) for key, value in locals().items() if key not in ("self", "url", "_[1]") ], ) with open(path, mode="rb") as fp: url = fixurl(url, unquote=True) #: Recheck in 0.6.x if req is False: req = get_request() elif not req: req = self.req if isinstance(cookies, list): set_cookies(req.cj, cookies) # NOTE: req can be a HTTPRequest or a Browser object http_req = self.req.http if hasattr(self.req, "http") else self.req if not redirect: http_req.c.setopt(pycurl.FOLLOWLOCATION, 0) elif isinstance(redirect, int): http_req.c.setopt(pycurl.MAXREDIRS, redirect) if isinstance(ref, str): http_req.last_url = ref http_req.set_request_context(url, get, {}, bool(ref), bool(cookies), False) http_req.c.setopt(pycurl.HTTPHEADER, http_req.request_headers) http_req.response_header = b"" http_req.c.setopt(pycurl.UPLOAD, 1) http_req.c.setopt(pycurl.READFUNCTION, fp.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() http_req.c.setopt(pycurl.FOLLOWLOCATION, 1) http_req.c.setopt(pycurl.NOBODY, 0) else: http_req.c.perform() http_req.c.setopt(pycurl.UPLOAD, 0) http_req.c.setopt(pycurl.INFILESIZE, 0) http_req.c.setopt(pycurl.POSTFIELDS, "") http_req.last_effective_url = http_req.c.getinfo(pycurl.EFFECTIVE_URL) http_req.add_cookies() http_req.code = http_req.verify_header() html = http_req.response_header if just_header else http_req.get_response() http_req.rep.close() http_req.rep = None if decode is True: html = http_req.decode_response(html) if not redirect: http_req.c.setopt(pycurl.FOLLOWLOCATION, 1) elif isinstance(redirect, int): maxredirs = ( self.pyload.api.get_config_value( "UserAgentSwitcher", "maxredirs", "plugin" ) or 5 ) # NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.MAXREDIRS, maxredirs) if decode: html = purge.unescape(html) self.last_html = html if self.pyload.debug: self.dump_html() # TODO: Move to network in 0.6.x header = {"code": req.code, "url": req.last_effective_url} # NOTE: req can be a HTTPRequest or a Browser object header.update(parse_html_header(http_req.response_header)) self.last_header = header if just_header: return header else: return html
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, *[ "{}={}".format(key, value) for key, value in locals().items() if key not in ("self", "url", "_[1]") ], ) url = fixurl(url, unquote=True) #: Recheck in 0.6.x if req is False: req = get_request() elif not req: req = self.req # TODO: Move to network in 0.6.x if isinstance(cookies, list): set_cookies(req.cj, cookies) http_req = self.req.http if hasattr(self.req, "http") else self.req # TODO: Move to network in 0.6.x 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) # TODO: Move to network in 0.6.x if isinstance(ref, str): req.last_url = ref html = req.load( url, get, post, bool(ref), bool(cookies), just_header, multipart, decode is True, ) # TODO: Fix network multipart in 0.6.x # TODO: Move to network in 0.6.x if not redirect: # NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.FOLLOWLOCATION, 1) elif type(redirect) is int: maxredirs = ( self.pyload.api.get_config_value( "UserAgentSwitcher", "maxredirs", "plugin" ) or 5 ) # NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.MAXREDIRS, maxredirs) # TODO: Move to network in 0.6.x if decode: html = purge.unescape(html) self.last_html = html if self.pyload.debug: self.dump_html() # TODO: Move to network in 0.6.x header = {"code": req.code, "url": req.last_effective_url} header.update(parse_html_header(http_req.response_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)
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, *[ "{}={}".format(key, value) for key, value in locals().items() if key not in ("self", "url", "_[1]") ], ) with open(path, mode="rb") as file: url = fixurl(url, unquote=True) #: Recheck in 0.6.x if req is False: req = get_request() req.set_option("timeout", 60) # TODO: Remove in 0.6.x 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 isinstance(redirect, int): # NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.MAXREDIRS, redirect) if isinstance(ref, str): http_req.last_url = ref http_req.set_request_context(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, file.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.get_response() http_req.c.setopt(pycurl.UPLOAD, 0) http_req.c.setopt(pycurl.INFILESIZE, 0) http_req.c.setopt(pycurl.POSTFIELDS, "") http_req.last_effective_url = http_req.c.getinfo(pycurl.EFFECTIVE_URL) http_req.add_cookies() try: http_req.code = http_req.verify_header() finally: http_req.rep.close() http_req.rep = None if decode is True: html = http_req.decode_response(html) if not redirect: http_req.c.setopt(pycurl.FOLLOWLOCATION, 1) elif isinstance(redirect, int): maxredirs = ( int( self.pyload.api.get_config_value( "UserAgentSwitcher", "maxredirs", "plugin" ) ) or 5 ) # TODO: Remove `int` in 0.6.x # 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.6.x html = _decode(html, decode) self.last_html = html if self.pyload.debug: self.dump_html() # TODO: Move to network in 0.6.x header = {"code": req.code, "url": req.last_effective_url} # 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 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, *[ "{}={}".format(key, value) for key, value in locals().items() if key not in ("self", "url", "_[1]") ], ) url = fixurl(url, unquote=True) #: Recheck in 0.6.x if req is False: req = get_request() req.set_option("timeout", 60) # TODO: Remove in 0.6.x elif not req: req = self.req # TODO: Move to network in 0.6.x if isinstance(cookies, list): set_cookies(req.cj, cookies) http_req = self.req.http if hasattr(self.req, "http") else self.req # TODO: Move to network in 0.6.x if not redirect: # NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.FOLLOWLOCATION, 0) elif isinstance(redirect, int): # NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.MAXREDIRS, redirect) # TODO: Move to network in 0.6.x if isinstance(ref, str): req.last_url = ref html = req.load( url, get, post, bool(ref), bool(cookies), just_header, multipart, decode is True, ) # TODO: Fix network multipart in 0.6.x # TODO: Move to network in 0.6.x if not redirect: # NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.FOLLOWLOCATION, 1) elif isinstance(redirect, int): maxredirs = ( int( self.pyload.api.get_config_value( "UserAgentSwitcher", "maxredirs", "plugin" ) ) or 5 ) # TODO: Remove `int` in 0.6.x # NOTE: req can be a HTTPRequest or a Browser object http_req.c.setopt(pycurl.MAXREDIRS, maxredirs) # TODO: Move to network in 0.6.x if decode: html = html_unescape(html) # TODO: Move to network in 0.6.x html = _decode(html, decode) self.last_html = html if self.pyload.debug: self.dump_html() # TODO: Move to network in 0.6.x header = {"code": req.code, "url": req.last_effective_url} # 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)