async def main():
    try:
        class AccountManager(BaseManager): pass
        AccountManager.register('captcha_queue')
        AccountManager.register('extra_queue')
        AccountManager.register('lv30_captcha_queue')
        AccountManager.register('lv30_account_queue')
        manager = AccountManager(address=get_address(), authkey=conf.AUTHKEY)
        manager.connect()
        captcha_queue = manager.captcha_queue()
        extra_queue = manager.extra_queue()
        lv30_captcha_queue = manager.lv30_captcha_queue()
        lv30_account_queue = manager.lv30_account_queue()

        def put_account_queue(account):
            if account.get('level', 0) < 30:
                extra_queue.put(account)
            else:
                lv30_account_queue.put(account)

        def put_captcha_queue(account):
            if account.get('leve', 0) < 30:
                captcha_queue.put(account)
            else:
                lv30_captcha_queue.put(account)

        if conf.GO_HASH:
            hashkey = conf.GO_HASH_KEY
        else:
            hashkey = conf.HASH_KEY
        activate_hash_server(hashkey,
            go_hash=conf.GO_HASH,
            hash_endpoint=conf.HASH_ENDPOINT,
            gohash_endpoint=conf.GOHASH_ENDPOINT)

        driver = webdriver.Chrome()
        driver.set_window_size(803, 807)

        while not captcha_queue.empty() or not lv30_captcha_queue.empty():
            try:
                account = captcha_queue.get()
            except Empty:
                try:
                    account = lv30_captcha_queue.get()
                except Empty:
                    break
            username = account.get('username')
            location = account.get('location')
            if location and location != (0,0,0):
                lat = location[0]
                lon = location[1]
            else:
                lat, lon = randomize_point(center, 0.0001)

            try:
                alt = altitudes.get((lat, lon))
            except KeyError:
                alt = await altitudes.fetch((lat, lon))

            try:
                device_info = get_device_info(account)
                api = PGoApi(device_info=device_info)
                api.set_position(lat, lon, alt)

                authenticated = False
                try:
                    if account['provider'] == 'ptc':
                        api.auth_provider = AuthPtc()
                        api.auth_provider._access_token = account['auth']
                        api.auth_provider._access_token_expiry = account['expiry']
                        if api.auth_provider.check_access_token():
                            api.auth_provider.authenticated = True
                            authenticated = True
                except KeyError:
                    pass

                if not authenticated:
                    await api.set_authentication(username=username,
                                                 password=account['password'],
                                                 provider=account.get('provider', 'ptc'))

                request = api.create_request()
                await request.call()

                await sleep(.6)

                request.download_remote_config_version(platform=1, app_version=9100)
                request.check_challenge()
                request.get_hatched_eggs()
                request.get_inventory(last_timestamp_ms=account.get('inventory_timestamp', 0))
                request.check_awarded_badges()
                request.download_settings()
                responses = await request.call()
                account['time'] = time()

                challenge_url = responses['CHECK_CHALLENGE'].challenge_url
                timestamp = responses['GET_INVENTORY'].inventory_delta.new_timestamp_ms
                account['location'] = lat, lon
                account['inventory_timestamp'] = timestamp
                if challenge_url == ' ':
                    account['captcha'] = False
                    print('No CAPTCHA was pending on {}.'.format(username))
                    put_account_queue(account)
                else:
                    print('Trying to solve {}.'.format(username))
                    if await solve_captcha(challenge_url, api, driver, timestamp):
                        account['time'] = time()
                        account['captcha'] = False
                        print('Solved CAPTCHA for {}, putting back in rotation.'.format(username))
                        put_account_queue(account)
                    else:
                        account['time'] = time()
                        print('Failed to solve for {}'.format(username))
                        put_captcha_queue(account)
            except KeyboardInterrupt:
                put_captcha_queue(account)
                break
            except KeyError:
                print('Unexpected or empty response for {}, putting back on queue.'.format(username))
                put_captcha_queue(account)
                try:
                    print(response)
                except Exception:
                    pass
                await sleep(3)
            except (ex.AuthException, ex.AuthTokenExpiredException) as e:
                print('Authentication error on {}: {}'.format(username, e))
                put_captcha_queue(account)
                await sleep(3)
            except ex.AiopogoError as e:
                print('aiopogo error on {}: {}'.format(username, e))
                put_captcha_queue(account)
                await sleep(3)
            except Exception:
                put_captcha_queue(account)
                raise
    finally:
        try:
            driver.close()
            close_sessions()
        except Exception:
            pass
Example #2
0
async def main():
    try:
        class AccountManager(BaseManager): pass
        AccountManager.register('captcha_queue')
        AccountManager.register('extra_queue')
        manager = AccountManager(address=get_address(), authkey=conf.AUTHKEY)
        manager.connect()
        captcha_queue = manager.captcha_queue()
        extra_queue = manager.extra_queue()

        activate_hash_server(conf.HASH_KEY)

        driver = webdriver.Chrome()
        driver.set_window_size(803, 807)

        while not captcha_queue.empty():
            account = captcha_queue.get()
            username = account.get('username')
            location = account.get('location')
            if location and location != (0,0,0):
                lat = location[0]
                lon = location[1]
            else:
                lat, lon = randomize_point(center, 0.0001)

            try:
                alt = altitudes.get(point)
            except KeyError:
                alt = await altitudes.fetch(point)

            try:
                device_info = get_device_info(account)
                api = PGoApi(device_info=device_info)
                api.set_position(lat, lon, alt)

                authenticated = False
                try:
                    if account['provider'] == 'ptc':
                        api.auth_provider = AuthPtc()
                        api.auth_provider._access_token = account['auth']
                        api.auth_provider._access_token_expiry = account['expiry']
                        if api.auth_provider.check_access_token():
                            api.auth_provider.authenticated = True
                            authenticated = True
                except KeyError:
                    pass

                if not authenticated:
                    await api.set_authentication(username=username,
                                                 password=account['password'],
                                                 provider=account.get('provider', 'ptc'))

                request = api.create_request()
                await request.call()

                await sleep(.6)

                request.download_remote_config_version(platform=1, app_version=6100)
                request.check_challenge()
                request.get_hatched_eggs()
                request.get_inventory()
                request.check_awarded_badges()
                request.download_settings()
                response = await request.call()
                account['time'] = time()

                responses = response['responses']
                challenge_url = responses['CHECK_CHALLENGE']['challenge_url']
                timestamp = responses.get('GET_INVENTORY', {}).get('inventory_delta', {}).get('new_timestamp_ms')
                account['location'] = lat, lon, alt
                account['inventory_timestamp'] = timestamp
                if challenge_url == ' ':
                    account['captcha'] = False
                    print('No CAPTCHA was pending on {}.'.format(username))
                    extra_queue.put(account)
                else:
                    if await solve_captcha(challenge_url, api, driver, timestamp):
                        account['time'] = time()
                        account['captcha'] = False
                        print('Solved CAPTCHA for {}, putting back in rotation.'.format(username))
                        extra_queue.put(account)
                    else:
                        account['time'] = time()
                        print('Failed to solve for {}'.format(username))
                        captcha_queue.put(account)
            except KeyboardInterrupt:
                captcha_queue.put(account)
                break
            except KeyError:
                print('Unexpected or empty response for {}, putting back on queue.'.format(username))
                captcha_queue.put(account)
                try:
                    print(response)
                except Exception:
                    pass
                await sleep(3)
            except (ex.AuthException, ex.AuthTokenExpiredException) as e:
                print('Authentication error on {}: {}'.format(username, e))
                captcha_queue.put(account)
                await sleep(3)
            except ex.AiopogoError as e:
                print('aiopogo error on {}: {}'.format(username, e))
                captcha_queue.put(account)
                await sleep(3)
            except Exception:
                captcha_queue.put(account)
                raise
    finally:
        try:
            driver.close()
            close_sessions()
        except Exception:
            pass