Exemplo n.º 1
0
def level_up_account(args, location, accounts, errors):
    while True:
        try:
            # Loop the queue.
            try:
                (account, error_count) = accounts.get(False)
            except Empty:
                break

            log.info('Starting account %s', account['username'])
            status = {
                'type': 'Worker',
                'message': 'Creating thread...',
                'success': 0,
                'fail': 0,
                'noitems': 0,
                'skip': 0,
                'captcha': 0,
                'username': '',
                'proxy_display': None,
                'proxy_url': None,
            }
            api = setup_api(args, status, account)
            api.set_position(*location)
            key = key_scheduler.next()
            api.activate_hash_server(key)
            check_login(args, account, api, status['proxy_url'])
            log.info('Account %s, level %d.', account['username'],
                     account['level'])
            (error, forts) = get_location_forts(api, account, location)
            if error:
                errors[error].append(account)
                accounts.task_done()
                continue
            log.info('%d stops in range', len(forts))
            for fort in forts:
                if pokestop_spinnable(fort, location):
                    spin_pokestop(api, account, args, fort, location)
            log.info('Ended with account %s.', account['username'])
            log.info('Account %s, level %d.', account['username'],
                     account['level'])
        except TooManyLoginAttempts:
            errors[ErrorType.login_error].append(account)
        except AccountBannedException:
            errors[ErrorType.banned].append(account)
        except Exception as e:
            if error_count < 2:
                log.exception('Exception in worker: %s. retrying.', e)
                accounts.put((account, error_count + 1))
            else:
                errors[ErrorType.generic].append(account)

        accounts.task_done()
Exemplo n.º 2
0
def perform_scout(p):
    global api, last_scout_timestamp, encounter_cache

    if not args.scout_account_username:
        return {"msg": "No scout account configured."}

    pokemon_name = get_pokemon_name(p.pokemon_id)

    # Check cache once in a non-blocking way
    if p.encounter_id in encounter_cache:
        result = encounter_cache[p.encounter_id]
        log.info(u"Cached scout-result: level {} {} with CP {}.".format(
            result["level"], pokemon_name, result["cp"]))
        return result

    scoutLock.acquire()

    # Check cache again after mutually exclusive access
    if p.encounter_id in encounter_cache:
        result = encounter_cache[p.encounter_id]
        log.info(u"Cached scout-result: level {} {} with CP {}.".format(
            result["level"], pokemon_name, result["cp"]))
        return result

    # Delay scouting
    now = time.time()
    if last_scout_timestamp is not None and now < last_scout_timestamp + scout_delay_seconds:
        wait_secs = last_scout_timestamp + scout_delay_seconds - now
        log.info(
            "Waiting {} more seconds before next scout use.".format(wait_secs))
        time.sleep(wait_secs)

    log.info(u"Scouting a {} at {}, {}".format(pokemon_name, p.latitude,
                                               p.longitude))
    step_location = jitter_location([p.latitude, p.longitude, 42])

    if api is None:
        # instantiate pgoapi
        api = PGoApi()

    api.set_position(*step_location)
    account = {
        "auth_service": args.scout_account_auth,
        "username": args.scout_account_username,
        "password": args.scout_account_password
    }
    check_login(args, account, api, None, False)

    if args.hash_key:
        key = key_scheduler.next()
        log.debug('Using key {} for this scout use.'.format(key))
        api.activate_hash_server(key)

    request_result = encounter_request(long(b64decode(p.encounter_id)),
                                       p.spawnpoint_id, p.latitude,
                                       p.longitude)

    # Update last timestamp
    last_scout_timestamp = time.time()
    scoutLock.release()

    return parse_scout_result(request_result, p.encounter_id, pokemon_name)
Exemplo n.º 3
0
def perform_scout(p):
    global api, last_scout_timestamp, encounter_cache

    if not args.scout:
        return {"msg": "Scouting disabled"}

    if len(accounts) == 0:
        return {"msg": "No scout account configured."}

    pokemon_name = get_pokemon_name(p.pokemon_id)

    # Check cache once in a non-blocking way
    if p.encounter_id in encounter_cache:
        result = encounter_cache[p.encounter_id]
        log.info(u"Cached scout-result: level {} {} with CP {}.".format(
            result["level"], pokemon_name, result["cp"]))
        return result

    step_location = []
    scoutLock.acquire()
    try:
        now = time.time()
        account = None
        wait_secs = args.scout_cooldown_delay
        for acc in accounts:
            if account is None:
                last_scout_timestamp = acc["last_used"]
                if acc['in_use']:
                    continue
                elif last_scout_timestamp is not None \
                    and now < last_scout_timestamp + args.scout_cooldown_delay:
                    wait_secs = min(
                        last_scout_timestamp + args.scout_cooldown_delay - now,
                        wait_secs)
                else:
                    account = acc
                    account["last_used"] = now
                    acc['in_use'] = True
    finally:
        scoutLock.release()

    if account is None:
        log.info(
            "Waiting {} more seconds before next scout use.".format(wait_secs))
        # time.sleep(wait_secs)
        request_result = {}
        request_result["wait"] = wait_secs
    else:
        # Check cache again after mutually exclusive access
        if p.encounter_id in encounter_cache:
            result = encounter_cache[p.encounter_id]
            log.info(u"Cached scout-result: level {} {} with CP {}.".format(
                result["level"], pokemon_name, result["cp"]))
            return result

        # Delay scouting

        if last_scout_timestamp is not None and now < last_scout_timestamp + args.scout_cooldown_delay:
            wait_secs = last_scout_timestamp + args.scout_cooldown_delay - now
            log.info("Waiting {} more seconds before next scout use.".format(
                wait_secs))
            # time.sleep(wait_secs)
            request_result = {}
            request_result["wait"] = wait_secs
        else:
            log.info(u"Scouting a {} at {}, {}".format(pokemon_name,
                                                       p.latitude,
                                                       p.longitude))
            step_location = jitter_location([p.latitude, p.longitude, 42])

            if api is None:
                # instantiate pgoapi
                api = PGoApi()

            api.set_position(*step_location)

            proxy_url = False
            if args.proxy is not None and len(args.proxy) > 0:
                proxy_num, proxy_url = get_new_proxy(args)
                if proxy_url:
                    log.debug('Using proxy %s', proxy_url)
                    api.set_proxy({'http': proxy_url, 'https': proxy_url})

            try:
                check_login(args, account, api, None, proxy_url)
                if args.hash_key:
                    key = key_scheduler.next()
                    log.debug('Using key {} for this scout use.'.format(key))
                    api.activate_hash_server(key)

                request_result = encounter_request(
                    long(b64decode(p.encounter_id)), p.spawnpoint_id,
                    p.latitude, p.longitude)

                # Update last timestamp
                account['last_used'] = time.time()
            except TooManyLoginAttempts:
                log.error("{} failed to login, going to sleep for 600 seconds".
                          format(account['username']))
                account['last_used'] = time.mktime(
                    (datetime.datetime.now() +
                     datetime.timedelta(seconds=600)).timetuple())
                account['in_use'] = False
                return {"msg": "Scout can't login"}
            finally:
                account['in_use'] = False

    return parse_scout_result(request_result, p.encounter_id, pokemon_name,
                              step_location, account)
Exemplo n.º 4
0
def level_up_account(args, location, accounts, errors):
    while True:
        try:
            # Loop the queue.
            try:
                (account, error_count) = accounts.get(False)
            except Empty:
                break

            log.info('Starting account %s', account['username'])
            status = {
                'type': 'Worker',
                'message': 'Creating thread...',
                'success': 0,
                'fail': 0,
                'noitems': 0,
                'skip': 0,
                'captcha': 0,
                'username': '',
                'proxy_display': None,
                'proxy_url': None,
            }

            api = setup_api(args, status, account)
            key = key_scheduler.next()

            api.set_position(*location)
            api.activate_hash_server(key)
            check_login(args, account, api, status['proxy_url'])

            log.info('Logged in account %s, level %d.', account['username'],
                     account['level'])

            (error, forts) = get_location_forts(api, account, location)

            if error:
                errors[error].append(account)
                accounts.task_done()
                continue

            spinnable_pokestops = filter(
                lambda fort: pokestop_spinnable(fort, location), forts)

            # No stops in spin range.
            first_fort = forts[0]
            if not spinnable_pokestops:
                log.critical(
                    'No Pokestops in spinnable range. Please move'
                    " the location. There's a Pokestop at"
                    ' %s, %s.', first_fort.latitude, first_fort.longitude)
                os._exit(1)

            # Past this point, guaranteed to have a spinnable stop.
            fort = spinnable_pokestops[0]
            spin_pokestop(api, account, args, fort, location)

            log.info('Spun Pokestop with account %s, level %d.',
                     account['username'], account['level'])
        except TooManyLoginAttempts:
            errors[ErrorType.login_error].append(account)
        except AccountBannedException:
            errors[ErrorType.banned].append(account)
        except Exception as e:
            if error_count < 2:
                log.exception('Exception in worker: %s. Retrying.', e)
                accounts.put((account, error_count + 1))
            else:
                errors[ErrorType.generic].append(account)

        accounts.task_done()