def get_iv(): pokemon_id = request.args["pokemon_id"] pokemon_name = get_pokemon_name(pokemon_id) lat = request.args["latitude"] lng = request.args["longitude"] encounter_id = request.args.get("encounter_id") spawn_point_id = request.args.get("spawn_point_id") # Check cache cache_key = encounter_id if encounter_id else "{}-{}-{}".format( pokemon_id, lat, lng) result = get_cached_encounter(cache_key) if result: log.info( u"Returning cached result: {:.1f}% level {} {} with {} CP".format( result['iv_percent'], result['pokemon_level'], pokemon_name, result['cp'])) return jsonify(result) # Create a ScoutJob job = ScoutJob(pokemon_id, encounter_id, spawn_point_id, lat, lng) # Enqueue and wait for job to be processed jobs.put(job) while not job.processed: time.sleep(1) # Cache successful jobs and return result if job.result['success']: cache_encounter(cache_key, job.result) return jsonify(job.result)
def get_iv(): if not app_state.accept_new_requests: return reject('Not accepting new requests.') if not have_active_scouts(): return reject('No active scout available. All banned?') pokemon_id = request.args["pokemon_id"] pokemon_name = get_pokemon_name(pokemon_id) forced = request.args.get('forced') prio = PRIO_HIGH if forced is not None else get_pokemon_prio(pokemon_id) cache_enable = cfg_get('cache_timer') > 0 max_queued_jobs = cfg_get('max_queued_jobs') num_jobs = jobs.qsize() if max_queued_jobs and num_jobs >= max_queued_jobs and prio == PRIO_LOW: return reject( "Job queue full ({} items). Rejecting encounter with priority '{}'." .format(num_jobs, PRIO_NAMES[prio])) lat = request.args["latitude"] lng = request.args["longitude"] weather = request.args.get("weather", "unknown") encounter_id = normalize_encounter_id(request.args.get("encounter_id")) # Spawn point ID is assumed to be a hex string spawn_point_id = request.args.get("spawn_point_id") despawn_time = request.args.get("despawn_time") if cache_enable: # Check cache cache_key = "{}-{}".format( encounter_id, weather) if encounter_id else "{}-{}-{}".format( pokemon_id, lat, lng) result = get_cached_encounter(cache_key) if result: log.info( u"Returning cached result: {:.1f}% level {} {} with {} CP". format(result['iv_percent'], result['level'], pokemon_name, result['cp'])) return jsonify(result) # Create a ScoutJob job = ScoutJob(pokemon_id, encounter_id, spawn_point_id, lat, lng, despawn_time=despawn_time) # Enqueue and wait for job to be processed jobs.put((prio, time.time(), job)) while not job.processed: time.sleep(1) # Cache successful jobs and return result if cache_enable and job.result['success']: cache_encounter(cache_key, job.result) return jsonify(job.result)
def get_iv(): error = None if not app_state.accept_new_requests: error = 'Not accepting new requests.' if not have_active_scouts(): error = 'No active scout available. All banned?' max_queued_jobs = cfg_get('max_queued_jobs') num_jobs = jobs.qsize() if max_queued_jobs and num_jobs >= max_queued_jobs: error = "Job queue full ({} items). Perform less encounters or add more scouts.".format( num_jobs) if error: log.warning(error) return jsonify({'success': False, 'error': error}) pokemon_id = request.args["pokemon_id"] pokemon_name = get_pokemon_name(pokemon_id) lat = request.args["latitude"] lng = request.args["longitude"] weather = request.args.get("weather", "unknown") encounter_id = normalize_encounter_id(request.args.get("encounter_id")) # Spawn point ID is assumed to be a hex string spawn_point_id = request.args.get("spawn_point_id") despawn_time = request.args.get("despawn_time") # Check cache cache_key = "{}-{}".format(encounter_id, weather) if encounter_id else "{}-{}-{}".format( pokemon_id, lat, lng) result = get_cached_encounter(cache_key) if result: log.info( u"Returning cached result: {:.1f}% level {} {} with {} CP".format( result['iv_percent'], result['level'], pokemon_name, result['cp'])) return jsonify(result) # Create a ScoutJob job = ScoutJob(pokemon_id, encounter_id, spawn_point_id, lat, lng, despawn_time=despawn_time) # Enqueue and wait for job to be processed jobs.put(job) while not job.processed: time.sleep(1) # Cache successful jobs and return result if job.result['success']: cache_encounter(cache_key, job.result) return jsonify(job.result)
def get_iv(): if not app_state.accept_new_requests: return jsonify({ 'success': False, 'error': 'Not accepting new requests.' }) pokemon_id = request.args["pokemon_id"] pokemon_name = get_pokemon_name(pokemon_id) lat = request.args["latitude"] lng = request.args["longitude"] encounter_id = normalize_encounter_id(request.args.get("encounter_id")) # Spawn point ID is assumed to be a hex string spawn_point_id = request.args.get("spawn_point_id") # Check cache cache_key = encounter_id if encounter_id else "{}-{}-{}".format( pokemon_id, lat, lng) result = get_cached_encounter(cache_key) if result: log.info( u"Returning cached result: {:.1f}% level {} {} with {} CP".format( result['iv_percent'], result['level'], pokemon_name, result['cp'])) return jsonify(result) # Create a ScoutJob job = ScoutJob(pokemon_id, encounter_id, spawn_point_id, lat, lng) # Enqueue and wait for job to be processed jobs.put(job) while not job.processed: time.sleep(1) # Cache successful jobs and return result if job.result['success']: cache_encounter(cache_key, job.result) return jsonify(job.result)
def run(self): self.log_info("Waiting for job...") while True: job = self.job_queue.get() try: self.log_info(u"Scouting a {} at {}, {}".format( job.pokemon_name, job.lat, job.lng)) # Initialize API (lat, lng) = jitter_location(job.lat, job.lng) self.set_position(lat, lng, job.altitude) if not self.check_login(): job.result = self.scout_error(self.last_msg) continue # Check if banned. if self.is_banned(): job.result = self.scout_error("Account banned") break if job.encounter_id and job.spawn_point_id: job.result = self.scout_by_encounter_id(job) else: if self.find_pokemon(job): time.sleep(2) job.result = self.scout_by_encounter_id(job) else: job.result = self.scout_error( "Could not determine encounter_id for {} at {}, {}" .format(job.pokemon_name, job.lat, job.lng)) # Mark shadowbanned if too many errors sb_threshold = cfg_get('shadowban_threshold') if sb_threshold and self.errors >= sb_threshold: self.shadowbanned = True if self.shadowbanned: self.log_warning( "Account probably shadowbanned. Stopping.") break except: job.result = self.scout_error(repr(sys.exc_info())) finally: job.processed = True self.update_history() if job.result['message']['success']: payload = {} payload['pokehunt_id'] = job.result['message'][ 'pokehunt_id'] #response = requests.post('http://192.168.1.101:1418/test4', json=payload) cache_key = job.encounter_id if job.encounter_id else "{}-{}-{}".format( pokemon_id, lat, lng) cache_encounter(cache_key, job.result) #return jsonify(job.result) response = requests.post(cfg_get('customwebhook'), json=job.result) if (response.status_code != 200): log.error("Error sending webhook: {}".format( response.raise_for_status())) payload = {} payload['pokehunt_id'] = job.result['message'][ 'pokehunt_id']