Beispiel #1
0
    def __unlock_wechat(self,
                        url,
                        resp,
                        session,
                        unlock_callback=None,
                        identify_image_callback=None):
        if unlock_callback is None:
            unlock_callback = unlock_weixin_callback_example

        r_captcha = session.get(
            'https://mp.weixin.qq.com/mp/verifycode?cert={}'.format(
                time.time() * 1000))
        if not r_captcha.ok:
            raise WechatSogouRequestsException(
                'WechatSogouAPI unlock_history get img', resp)

        r_unlock = unlock_callback(url, session, resp, r_captcha.content,
                                   identify_image_callback)

        if r_unlock['ret'] != 0:
            raise WechatSogouVcodeOcrException(
                '[WechatSogouAPI identify image] code: {ret}, msg: {errmsg}, cookie_count: {cookie_count}'
                .format(ret=r_unlock.get('ret'),
                        errmsg=r_unlock.get('errmsg'),
                        cookie_count=r_unlock.get('cookie_count')))
Beispiel #2
0
    def __get_by_unlock(self,
                        url,
                        referer=None,
                        is_need_unlock=None,
                        unlock_platform=None,
                        unlock_callback=None,
                        identify_image_callback=None):
        assert is_need_unlock is None or callable(is_need_unlock)
        assert unlock_platform is None or callable(unlock_platform)

        if identify_image_callback is None:
            identify_image_callback = identify_image_callback_by_hand
        assert unlock_callback is None or callable(unlock_callback)
        assert callable(identify_image_callback)

        session = requests.session()
        resp = self.__get(url,
                          session,
                          headers=self.__set_cookie(referer=referer))

        if is_need_unlock(resp):
            for i in range(self.captcha_break_times):
                try:
                    unlock_platform(url, resp, session, unlock_callback,
                                    identify_image_callback)
                    break
                except WechatSogouVcodeOcrException as e:
                    if i == self.captcha_break_times - 1:
                        raise WechatSogouVcodeOcrException(e)

            resp = self.__get(url,
                              session,
                              headers=self.__set_cookie(referer=referer))

        return resp
Beispiel #3
0
    def __unlock_sogou(self,
                       url,
                       resp,
                       session,
                       unlock_callback=None,
                       identify_image_callback=None):
        if unlock_callback is None:
            unlock_callback = unlock_sogou_callback_example

        millis = int(round(time.time() * 1000))
        r_captcha = session.get(
            'http://weixin.sogou.com/antispider/util/seccode.php?tc={}'.format(
                millis))
        if not r_captcha.ok:
            raise WechatSogouRequestsException('WechatSogouAPI get img', resp)

        r_unlock = unlock_callback(url, session, resp, r_captcha.content,
                                   identify_image_callback)

        if r_unlock['code'] != 0:
            raise WechatSogouVcodeOcrException(
                '[WechatSogouAPI identify image] code: {code}, msg: {msg}'.
                format(code=r_unlock.get('code'), msg=r_unlock.get('msg')))
        else:
            self.__set_cache(session.cookies.get('SUID'), r_unlock['id'])
Beispiel #4
0
    def __get_by_unlock(self,
                        url,
                        referer=None,
                        unlock_platform=None,
                        unlock_callback=None,
                        identify_image_callback=None):
        assert unlock_platform is None or callable(unlock_platform)

        if identify_image_callback is None:
            identify_image_callback = identify_image_callback_by_hand
        assert unlock_callback is None or callable(unlock_callback)
        assert callable(identify_image_callback)

        session = requests.session()
        resp = self.__get(url,
                          session,
                          headers=self.__set_cookie(referer=referer))
        if 'antispider' in resp.url or '请输入验证码' in resp.text:
            for i in range(self.captcha_break_times):
                try:
                    unlock_platform(url, resp, session, unlock_callback,
                                    identify_image_callback)
                    break
                except WechatSogouVcodeOcrException as e:
                    if i == self.captcha_break_times - 1:
                        raise WechatSogouVcodeOcrException(e)

            if '请输入验证码' in resp.text:
                resp = session.get(url)
            else:
                headers = self.__set_cookie(referer=referer)
                headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64)'
                resp = self.__get(url, session, headers)

        return resp
def unlock_weixin_callback_example(url, req, resp, img, identify_image_callback):
    """手动打码解锁

    Parameters
    ----------
    url : str or unicode
        验证码页面 之前的 url
    req : requests.sessions.Session
        requests.Session() 供调用解锁
    resp : requests.models.Response
        requests 访问页面返回的,已经跳转了
    img : bytes
        验证码图片二进制数据
    identify_image_callback : callable
        处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example

    Returns
    -------
    dict
        {
            'ret': '',
            'errmsg': '',
            'cookie_count': '',
        }
    """
    # no use resp

    unlock_url = 'https://mp.weixin.qq.com/mp/verifycode'
    data = {
        'cert': time.time() * 1000,
        'input': identify_image_callback(img)
    }
    headers = {
        'Host': 'mp.weixin.qq.com',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Referer': url
    }
    r_unlock = req.post(unlock_url, data, headers=headers)
    if not r_unlock.ok:
        raise WechatSogouVcodeOcrException(
            'unlock[{}] failed: {}[{}]'.format(unlock_url, r_unlock.text, r_unlock.status_code))
    
    return r_unlock.json()
def unlock_sogou_callback_example(url, req, resp, img, identify_image_callback):
    """手动打码解锁

    Parameters
    ----------
    url : str or unicode
        验证码页面 之前的 url
    req : requests.sessions.Session
        requests.Session() 供调用解锁
    resp : requests.models.Response
        requests 访问页面返回的,已经跳转了
    img : bytes
        验证码图片二进制数据
    identify_image_callback : callable
        处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example

    Returns
    -------
    dict
        {
            'code': '',
            'msg': '',
        }
    """
    # no use resp
    url_quote = url.split('weixin.sogou.com/')[-1]
    unlock_url = 'http://weixin.sogou.com/antispider/thank.php'
    data = {
        'c': identify_image_callback(img),
        'r': '%2F' + url_quote,
        'v': 5
    }
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Referer': 'http://weixin.sogou.com/antispider/?from=%2f' + url_quote
    }
    r_unlock = req.post(unlock_url, data, headers=headers)
    if not r_unlock.ok:
        raise WechatSogouVcodeOcrException(
            'unlock[{}] failed: {}'.format(unlock_url, r_unlock.text, r_unlock.status_code))

    return r_unlock.json()
Beispiel #7
0
    def __deblocking_history(self, url, resp, req, deblocking_callback,
                             identify_image_callback):
        r_img = req.get(
            'https://mp.weixin.qq.com/mp/verifycode?cert={}'.format(
                time.time() * 1000))
        if not r_img.ok:
            raise WechatSogouRequestsException(
                'WechatSogouAPI deblocking_history get img', resp)

        if callable(deblocking_callback):
            r_deblocking = deblocking_callback(req, resp, r_img.content)
        else:
            identify_image_callback = identify_image_callback if callable(
                identify_image_callback) else identify_image_callback_example
            r_deblocking = deblocking_callback_history_example(
                url, req, resp, r_img.content, identify_image_callback)

        if r_deblocking['ret'] != 0:
            raise WechatSogouVcodeOcrException(
                '[WechatSogouAPI identify image] code: {ret}, msg: {errmsg}, cookie_count: {cookie_count}'
                .format(**r_deblocking))
Beispiel #8
0
    def __deblocking_search(self, url, resp, req, deblocking_callback,
                            identify_image_callback):
        millis = int(round(time.time() * 1000))
        r_img = req.get(
            'http://weixin.sogou.com/antispider/util/seccode.php?tc={}'.format(
                millis))
        if not r_img.ok:
            raise WechatSogouRequestsException('WechatSogouAPI get img', resp)

        if callable(deblocking_callback):
            r_deblocking = deblocking_callback(req, resp, r_img.content)
        else:
            identify_image_callback = identify_image_callback if callable(
                identify_image_callback) else identify_image_callback_example
            r_deblocking = deblocking_callback_search_example(
                url, req, resp, r_img.content, identify_image_callback)

        if r_deblocking['code'] != 0:
            raise WechatSogouVcodeOcrException(
                '[WechatSogouAPI identify image] code: {code}, msg: {msg}'.
                format(**r_deblocking))
        else:
            self.__set_cache(req.cookies.get('SUID'), r_deblocking['id'])