def on_task_output(self, task, config): """Mark all accepted episodes as acquired on MyEpisodes""" if not task.accepted: # Nothing accepted, don't do anything return username = config['username'] password = config['password'] cookiejar = http.cookiejar.CookieJar() opener = request.build_opener(request.HTTPCookieProcessor(cookiejar)) baseurl = request.Request('http://www.myepisodes.com/login.php?') loginparams = parse.urlencode({'username': username, 'password': password, 'action': 'Login'}) try: logincon = opener.open(baseurl, loginparams) loginsrc = logincon.read() except URLError as e: log.error('Error logging in to myepisodes: %s' % e) return if str(username) not in loginsrc: raise plugin.PluginWarning(('Login to myepisodes.com failed, please check ' 'your account data or see if the site is down.'), log) for entry in task.accepted: try: self.mark_episode(task, entry, opener) except plugin.PluginWarning as w: log.warning(str(w))
def __init__(self, timeout=10, addHeaders=True): socket.setdefaulttimeout(timeout) # 设置超时时间 self.__perLen = 0 self.__opener = request.build_opener() request.install_opener(self.__opener) if addHeaders: self.__addHeaders()
def __init__(self, url, username, password): super(BitcoindConnection, self).__init__() self.id = 'bitcoin_sneak' self.url = url self.username = username self.password = password pm = request.HTTPPasswordMgrWithDefaultRealm() pm.add_password(None, url, username, password) request.install_opener( request.build_opener(request.HTTPHandler(), request.HTTPBasicAuthHandler(pm)))
def load_defaults(service): try: default_url = urlparse.urlparse(service.DEFAULTS_URL) if default_url.scheme not in ['http', 'https', 'file']: raise Exception("Invalid scheme: {}".format(default_url.scheme)) proxy_handler = urllib.ProxyHandler({}) if service.proxy_uri: proxy_handler = urllib.ProxyHandler({ 'http': service.proxy_uri, 'https': service.proxy_uri }) opener = urllib.build_opener(proxy_handler) req = urllib.Request(service.DEFAULTS_URL) with opener.open(req, timeout=CONNECTION_TIMEOUT) as f: data = f.read() service.defaults = json.loads(data.decode()) except Exception as e: logging.getLogger(service.__module__).error( "Failed to load up to date defaults, using offline copy: {}". format(e)) service.defaults = service.OFFLINE_DEFAULTS
def _put(self, url, param_dict=None, securityHandler=None, additional_headers=None, proxy_url=None, proxy_port=80, compress=True): """ Performs a put operation on a URL. Inputs: param_dict - key/preParams pair of values that will be turned into a json string ex: {"foo": "bar"} securityHandler - object that handles the token or other site security. It must inherit from the base security class. ex: arcrest.AGOLSecurityHandler("SomeUsername", "SOMEPASSWORD") additional_headers - are additional key/preParams headers that a user wants to pass during the operation. ex: {"accept-encoding": "gzip"} proxy_url - url of the proxy proxy_port - default 80, port number of the proxy compress - default true, determines if gzip should be used of not for the web operation. Output: returns dictionary or string depending on web operation. """ # ensure that no spaces are in the url url = url.replace(" ", "%20") if param_dict is None: param_dict = {} if additional_headers is None: additional_headers = {} if self._verify == False and \ sys.version_info[0:3] >= (2, 7, 9) and \ hasattr(ssl, 'create_default_context'): ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE headers = { "User-Agent": self.useragent, 'Accept': '*/*', 'Content-type': 'application/json' } if securityHandler and securityHandler.referer_url: headers['referer'] = securityHandler.referer_url # opener = None # return_value = None handlers = [RedirectHandler()] param_dict, handler, cj = self._processHandler(securityHandler, param_dict) if handler is not None: handlers.append(handler) if cj is not None: handlers.append(request.HTTPCookieProcessor(cj)) if compress: headers['Accept-Encoding'] = 'gzip' else: headers['Accept-Encoding'] = '' for k, v in additional_headers.items(): headers[k] = v del k, v hasContext = 'context' in self._has_context(request.urlopen) if self._verify == False and \ sys.version_info[0:3] >= (2, 7, 9) and \ hasattr(ssl, 'create_default_context'): ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE opener = request.build_opener(*handlers) opener.addheaders = [(k, v) for k, v in headers.items()] request.install_opener(opener) data = urlencode(param_dict) if self.PY3: data = data.encode('ascii') data = json.dumps(param_dict) opener.data = data # request.get_method = lambda: 'PUT' req = request.Request("{}?token={}".format( self._asString(url), self._securityHandler.token), data=data, headers=headers) req.get_method = lambda: 'PUT' for k, v in headers.items(): req.add_header(k, v) if hasContext and self._verify == False: resp = request.urlopen(req, context=ctx) else: resp = request.urlopen(req) self._last_code = resp.getcode() self._last_url = resp.geturl() return_value = self._process_response(resp=resp) if isinstance(return_value, dict): if "error" in return_value and \ 'message' in return_value['error']: if return_value['error']['message'].lower( ) == 'request not made over ssl': if url.startswith('http://'): url = url.replace('http://', 'https://') return self._put(url, param_dict, securityHandler, additional_headers, proxy_url, proxy_port, compress) return return_value else: return return_value return return_value
def debug(): """ Activate debugging on urllib2 """ handler = HTTPSHandler(debuglevel=1) opener = build_opener(handler) install_opener(opener)