Exemplo n.º 1
0
    def initUserConfig(self):

        sql = ''' SELECT
                      config
                  FROM
                      `configs`
                  WHERE
                      userId = {} '''.format(self.userId)

        result = self.db.query(sql)

        config = None

        for row in result:
            config = row['config']

        if config is None:
            raise Exception('Config is invalid for user {}'.format(
                self.userId))

        try:
            configObj = json.loads(config.decode('utf-8', 'ignore'))
        except ValueError as e:
            raise Exception('Config is invalid for user {}'.format(
                self.userId))

        ## Login
        obj = configObj.pop('login')

        self.username = obj.pop('username')
        password = obj.pop('password')
        self.password = base64.b64decode(password)

        self.image = OutputPath.getAuthPath('user_{}'.format(self.userId))
        self.originalImage = OutputPath.getAuthPath('user_{}_original'.format(
            self.userId))

        self.source = OutputPath.getAuthPath('user_{}'.format(self.userId),
                                             'html')
Exemplo n.º 2
0
    def plogin(self):
        def isValidAuthCode(code):

            if code is None or len(code) != 4:
                return False

            for c in code:

                if c.isdigit():
                    continue

                if c.isalpha():
                    continue

                # Wrong word
                return False

            return True

        if self.pCookies is not None:
            return True

        # https://github.com/mozilla/geckodriver/releases
        # browser = webdriver.Firefox()

        # https://chromedriver.storage.googleapis.com/index.html
        browser = webdriver.Chrome()

        try:
            # Plogin
            browser.get(self.ploginUrl)

            # Login by username and password

            # Username and password
            randomSleep(1, 2)
            inputElement(browser.find_element_by_id('username'), self.pin)

            randomSleep(1, 2)
            inputElement(browser.find_element_by_id('password'), self.password)

            # Submit
            buttonElement = browser.find_element_by_id('loginBtn')

            # Code
            codeElement = browser.find_element_by_id('code')
            imageElement = browser.find_element_by_id('imgCode')

            times = 0

            if codeElement.is_displayed():

                while codeElement.is_displayed() and times < 50:

                    times += 1

                    # Image to text
                    path = OutputPath.getAuthPath(self.pin)

                    ImageKit.saveCapture(browser, imageElement, path)

                    code = ImageKit.getText(path)

                    codeElement.send_keys(code)

                    if not isValidAuthCode(code):

                        # Refresh auth code
                        randomSleep(0.5, 1)
                        imageElement.click()

                        # Wait for updating auth code
                        randomSleep(1, 2)
                        codeElement.clear()

                        continue

                    # Submit
                    randomSleep(1, 2)
                    buttonElement.click()

                    error = self.getBrowserError(browser)

                    if error is None:
                        print 'Succeed after', times, 'tries.'
                        break

                    if u'验证码' not in error:
                        raise Exception('Unable to login for "{}": {}'.format(
                            self.pin, error))

                    randomSleep(1, 2)
                    codeElement.clear()
                    randomSleep(1, 2)

                else:
                    raise Exception('Unable to login for "{}"'.format(
                        self.pin))

            else:
                # Submit
                randomSleep(1, 2)
                buttonElement.click()

                wait = WebDriverWait(browser, 3)

                error = self.getBrowserError(browser)

                if error is not None:
                    raise Exception('Unable to login for "{}": {}'.format(
                        self.pin, error))

            print 'Loginned for', self.pin

            # Redirect to wqs
            qwsUrl = getProperty(self.configFile, 'cps-qwd-wqs-url')

            browser.get(qwsUrl)
            time.sleep(10)

            # Save as type of cookie for requests
            self.pCookies = dict()
            for cookie in browser.get_cookies():

                k = cookie['name']
                v = cookie['value']

                self.pCookies[k] = v

        except Exception as e:
            print e
        finally:
            browser.quit()
Exemplo n.º 3
0
Arquivo: qwd.py Projeto: wean/coupon
    def plogin(self, retries=0, force=False):

        def isValidAuthCode(code):

            if code is None or len(code) != 4:
                return False

            for c in code:

                if c.isdigit():
                    continue

                if c.isalpha():
                    continue

                # Wrong word
                return False

            return True

        if self.pCookies is not None:
            return True

        if force:
            self.entryCookies = None
            self.keyCookies = None

        if retries is 0:
            pass
        elif retries is 1:
            self.keyCookies = None
        elif retries is 2:
            self.entryCookies = None
        else:
            return False

        cookies = None

        if self.entryCookies is not None:

            try:
                cookies = json.loads(self.entryCookies.decode('utf-8', 'ignore'))
            except ValueError as e:
                pass

        if cookies is None:

            self.dbUpdated = True

            display = Display(visible=0, size=(800, 600))
            display.start()

            if 'firefox' == self.ploginType:

                # https://github.com/mozilla/geckodriver/releases
                browser = webdriver.Firefox()

            else: # Chrome

                # https://chromedriver.storage.googleapis.com/index.html
                browser = webdriver.Chrome()

            try:
                # Plogin
                browser.get(self.ploginUrl)

                # Login by username and password

                # Username and password
                randomSleep(1, 2)
                inputElement(browser.find_element_by_id('username'), self.username)

                randomSleep(1, 2)
                inputElement(browser.find_element_by_id('password'), self.password)

                # Submit
                buttonElement = browser.find_element_by_id('loginBtn')

                # Code
                codeElement = browser.find_element_by_id('code')
                imageElement = browser.find_element_by_id('imgCode')

                times = 0

                if codeElement.is_displayed():

                    print 'Auth code is needed ...'

                    while codeElement.is_displayed() and times < 50:

                        times += 1

                        # Image to text
                        path = OutputPath.getAuthPath(self.username)

                        ImageKit.saveCapture(browser, imageElement, path)

                        code = ImageKit.getText(path)

                        codeElement.send_keys(code)

                        if not isValidAuthCode(code):

                            # Refresh auth code
                            randomSleep(0.5, 1)
                            imageElement.click()

                            # Wait for updating auth code 
                            randomSleep(1, 2)
                            codeElement.clear()

                            continue

                        # Submit
                        randomSleep(1, 2)
                        buttonElement.click()

                        error = self.getBrowserError(browser)

                        if error is None:
                            print 'Succeed after', times, 'tries.'
                            break

                        if u'验证码' not in error:
                            raise Exception('Unable to login for "{}": {}'.format(self.userId, error))

                        randomSleep(1, 2)
                        codeElement.clear()
                        randomSleep(1, 2)

                    else:
                        raise Exception('Unable to login for "{}"'.format(self.userId))

                else:
                    # Submit
                    randomSleep(1, 2)
                    buttonElement.click()

                    wait = WebDriverWait(browser, 3)
                  
                    error = self.getBrowserError(browser)

                    if error is not None:
                        raise Exception('Unable to login for "{}": {}'.format(self.userId, error))

                print 'Loginned for user', self.userId, 'with type', self.loginType

                # Redirect to wqs
                time.sleep(1)

                # Save as type of cookie for requests
                cookies = dict()
                for cookie in browser.get_cookies():

                    k = cookie['name']
                    v = cookie['value']

                    cookies[k] = v

                self.entryCookies = reprDict(cookies)

            except Exception as e:
                print 'Unable to get entry cookie with an error:\n', e
                return False
            finally:
                browser.quit()

                if display is not None:
                    display.stop()

        # Update pCookies
        self.pCookies = cookies

        cookies = None

        if self.keyCookies is not None:

            try:
                cookies = json.loads(self.keyCookies.decode('utf-8', 'ignore'))
            except ValueError as e:
                pass

        if cookies is None:

            self.dbUpdated = True

            try:
                # Headers
                headers = {'User-Agent': self.userAgent}

                params = {'rurl': self.wqsUrl}

                r = requests.get(self.wqUrl, params=params, cookies=self.pCookies, headers=headers, allow_redirects=False)
            except Exception as e: 
                print 'Unable to get key cookie with an error:\n', e
                return False

            cookies = dict()
            for cookie in r.cookies:
                cookies[cookie.name] = cookie.value

            self.keyCookies = reprDict(cookies)

        # Update pCookies
        self.pCookies.update(cookies)

        return True