Ejemplo n.º 1
0
    def login(self, username, password):
        self._login_auth = ZhihuOAuth()
        data = LOGIN_DATA.copy()
        data['username'] = username
        data['password'] = password
        gen_signature(data)

        if self.need_captcha():
            captcha_image = self.get_captcha()
            with open(CAPTCHA_FILE, 'wb') as f:
                f.write(captcha_image)
            print('Please open {0} for captcha'.format(
                os.path.abspath(CAPTCHA_FILE)))

            captcha = input('captcha: ')
            os.remove(os.path.abspath(CAPTCHA_FILE))
            res = self._session.post(
                CAPTCHA_URL,
                auth=self._login_auth,
                data={'input_text': captcha}
            )
            try:
                json_dict = res.json()
                if 'error' in json_dict:
                    raise LoginException(json_dict['error']['message'])
            except (ValueError, KeyError) as e:
                raise LoginException('Maybe input wrong captcha value')

        self.save_token(self._login_auth, data)
Ejemplo n.º 2
0
 def save_token(self, auth, data):
     res = self._session.post(LOGIN_URL, auth=auth, data=data)
     try:
         json_dict = res.json()
         if 'error' in json_dict:
             raise LoginException(json_dict['error']['message'])
         self._token = ZhihuToken.from_dict(json_dict)
     except (ValueError, KeyError) as e:
         raise LoginException(str(e))
     else:
         ZhihuToken.save_file(self.token_file, json_dict)
Ejemplo n.º 3
0
 def need_captcha(self):
     res = self._session.get(CAPTCHA_URL, auth=self._login_auth)
     try:
         j = res.json()
         return j['show_captcha']
     except KeyError:
         raise LoginException('Show captcha fail!')
Ejemplo n.º 4
0
    def login(self):
        # re-use browser if one exists
        if not self.logged_in:
            # submit login form
            self.b = mechanize.Browser(factory=mechanize.RobustFactory())
            self.b.set_debug_http(self.debug)
            self.b.set_debug_redirects(self.debug)
            self.b.set_debug_responses(self.debug)
            self.b.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(),
                                      max_time=2)

            self.cj = cookielib.LWPCookieJar(self.config['ba']['cookiejar'])
            try:
                self.cj.load()
            except Exception as e:
                pass
            self.b.set_cookiejar(self.cj)

            self.b.open(self.config['ba']['base'])
            self.b.select_form(name="toploginform")
            self.b['membershipNumber'] = self.config['ba']['username']
            self.b['password'] = self.config['ba']['password']
            response = self.b.submit()
            html = response.get_data()

            if "We are not able to recognise the membership number or PIN/password that you have supplied" in html or "You have made too many invalid login attempts" in html:
                raise LoginException()

            # it worked
            self.logged_in = True

        # always re-get this page, after a successful login we are sent to the exec club homepage
        response = self.b.open(self.config['ba']['base'])
        return response
Ejemplo n.º 5
0
 def save_token(self, auth, data):
     res = self._session.post(LOGIN_URL, auth=ZhugeOAuth(), data=data)
     try:
         json_dict = res.json()
         if 'error' in json_dict:
             raise LoginException(json_dict['error']['message'])
         self._token = ZhugeToken.from_dict(json_dict)
     except (ValueError, KeyError) as e:
         # login again
         time.sleep(5)
         self.save_token(auth, data)
Ejemplo n.º 6
0
 async def login_user(self):
     assert self.state==User.States.CLEAR
     logger.debug('User State is: {}'.format(self.state))
     try:        
         response = await self.session.post(self.login_url, data={'username':self.username,'password':'******'}, allow_redirects=False)
     except Exception:
         logger.error('Error occured trying to log in - {}'.format(self.username)) 
         raise LoginException(self.username)
     logger.debug('Login Response is {}'.format(response.text))
     if response.status!=302:
         raise LoginRedirectError(self.username)
     else:
         self.state = User.States.LOGGED_IN
     logger.info('Loggin Sucessful for: {} '.format(self.username))