예제 #1
0
    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))
예제 #2
0
    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
예제 #3
0
 def addCookiejar(self):
     '''''为 self.__opener 添加 cookiejar handler。'''
     cj = http.cookiejar.CookieJar()
     self.__opener.add_handler(request.HTTPCookieProcessor(cj))