예제 #1
0
def run(configFile, userId, name, shareFile, index, savefile, logFile):

    OutputPath.init(configFile)

    thread = ThreadWritableObject(configFile, name, logFile)
    thread.start()

    sys.stdout = thread
    sys.errout = thread # XXX: Actually, it does NOT work

    try:
        db = None
        qwd = None

        db = Database(configFile, 'register')
        db.initialize()

        qwd = QWD(db, userId)

        viewer = Viewer(configFile, qwd)
        data = viewer.get(shareFile, index)

        if savefile is not None:
            with open(savefile, 'w') as fp:
                fp.write(reprDict(data))
        else:
            print reprDict(data)

    except KeyboardInterrupt:
        pass
    except Exception, e:
        print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        traceback.print_exc(file=sys.stdout)
예제 #2
0
def run(configfile, name, content, savefile):

    OutputPath.init(configFile)

    try:

        db = Database(configFile, 'specials')
        db.initialize()

        evaluation = Evaluation(configFile, db)

        data = evaluation.search(content)

        if savefile is not None:
            with open(savefile, 'w') as fp:
                fp.write(reprDict(data))
        else:
            print reprDict(data)

        return 0

    except KeyboardInterrupt:
        pass
    except Exception, e:
        print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        traceback.print_exc(file=sys.stdout)
예제 #3
0
파일: outputter.py 프로젝트: wean/coupon
def run(configfile, name):

    OutputPath.init(configFile)

    thread = ThreadWritableObject(configFile, name)
    thread.start()

    sys.stdout = thread
    sys.errout = thread # XXX: Actually, it does NOT work

    try:

        db = Database(configFile, 'specials')
        db.initialize()

        evaluation = Evaluation(configFile, db)

        evaluation.updateOverdue()

        path = OutputPath.getSharePath()
        sharePath = getProperty(configFile, 'output-share-file')

        cmd = '/bin/rm -f {1} && /bin/ln -s {0} {1}'.format(path, sharePath)
        runCommand(cmd)

        data = evaluation.output()

        with open(path, 'w') as fp:
            fp.write(reprDict(data))

    except KeyboardInterrupt:
        pass
    except Exception, e:
        print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        traceback.print_exc(file=sys.stdout)
예제 #4
0
def run(configFile, userId, name, content, savefile, logFile):

    OutputPath.init(configFile)

    thread = ThreadWritableObject(configFile, name, logFile)
    thread.start()

    sys.stdout = thread
    sys.errout = thread  # XXX: Actually, it does NOT work

    try:
        key = SearchingKeyRegex.parse(content)

        if key is None:
            key = content

        print 'Searching "', key, '" from user', userId

        db = None
        qwd = None

        db = Database(configFile, 'register')
        db.initialize()

        qwd = QWD(db, userId)

        searcher = Searcher(configFile, qwd)
        data = searcher.explore(key)

        if savefile is not None:
            with open(savefile, 'w') as fp:
                fp.write(reprDict(data))
        else:
            print reprDict(data)

    except KeyboardInterrupt:
        pass
    except Exception, e:
        print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        traceback.print_exc(file=sys.stdout)
예제 #5
0
    def getInputFromFile(self, notice, msg, image, prompt, length):

        with open(self.outputPath, 'w') as fp:

            content = dict()

            content['id'] = self.aId

            content['notice'] = notice
            content['message'] = msg

            content['image'] = None

            if image is not None:
                with open(image) as imageFp:
                    content['image'] = base64.b64encode(imageFp.read())

            content['prompt'] = prompt
            content['length'] = length

            content = JsonResult.create(content, self.code, prompt)

            fp.write(reprDict(content))

        for i in range(self.retries):

            time.sleep(1)

            try:
                with open(self.inputPath) as fp:

                    content = fp.read()
                    content = content.strip()

                    if ((length > 0 and len(content) is length) or (length < 0 and len(content) > 0)) \
                        and self.content != content:

                        self.content = content
                        self.code += 1  # Increase code

                        break

            except IOError as e:
                pass
        else:
            return None

        return self.content
예제 #6
0
def run(configFile, userId, name, screenshotPath, inputPath, outputPath,
        loginConfigFile, logFile):

    OutputPath.init(configFile)

    thread = ThreadWritableObject(configFile, name, logFile)
    thread.start()

    sys.stdout = thread
    sys.errout = thread  # XXX: Actually, it does NOT work

    result = JsonResult.error()

    try:
        db = Database(configFile, 'register')
        db.initialize()

        account = Account(db, userId)
        result = account.login(screenshotPath, inputPath, outputPath,
                               loginConfigFile)

    except KeyboardInterrupt:
        pass
    except Exception as e:
        print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        traceback.print_exc(file=sys.stdout)
    finally:
        try:
            if db is not None:
                db.quit()
        except:
            pass

    try:
        if outputPath is not None:
            with open(outputPath, 'w') as fp:
                fp.write(reprDict(result))
    except KeyboardInterrupt:
        pass
    except Exception as e:
        print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        traceback.print_exc(file=sys.stdout)

    thread.quit()
    thread.join()
예제 #7
0
    def updateDb(self, driver):

        # Redirect to wqs
        time.sleep(1)

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

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

            cookies[k] = v

        entryCookies = reprDict(cookies)

        sql = ''' UPDATE
                      `configs`
                  SET
                      `entryCookies`= '{}'
                  WHERE
                      `userId` = {} '''.format(entryCookies, self.userId)

        self.db.query(sql)
예제 #8
0
 def __repr__(self):
     return reprDict(self.times)
예제 #9
0
파일: qwd.py 프로젝트: 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