Skip to content

Commit

Permalink
Merge pull request #5624 from Gobberwart/gobb_snipetimeout
Browse files Browse the repository at this point in the history
Add timeouts to sniper source config
  • Loading branch information
Gobberwart authored Sep 23, 2016
2 parents dfbfa87 + b31f8af commit 9c05c96
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
{
"enabled": false,
"url": "http://localhost:5000/raw_data",
"timeout": 3,
"key": "pokemons",
"mappings": {
"id": { "param": "pokemon_id" },
Expand All @@ -236,6 +237,7 @@
{
"enabled": false,
"url": "https://pokewatchers.com/grab/",
"timeout": 10,
"mappings": {
"iv": { "param": "iv" },
"id": { "param": "pid" },
Expand All @@ -248,6 +250,7 @@
{
"enabled": false,
"url": "http://pokesnipers.com/api/v1/pokemon.json",
"timeout": 10,
"key": "results",
"mappings": {
"iv": { "param": "iv" },
Expand Down
3 changes: 3 additions & 0 deletions configs/config.json.map.example
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@
"enabled": false,
"url": "http://localhost:5000/raw_data",
"key": "pokemons",
"timeout": 3,
"mappings": {
"id": { "param": "pokemon_id" },
"name": { "param": "pokemon_name" },
Expand All @@ -490,6 +491,7 @@
{
"enabled": false,
"url": "https://pokewatchers.com/grab/",
"timeout": 10,
"mappings": {
"iv": { "param": "iv" },
"id": { "param": "pid" },
Expand All @@ -502,6 +504,7 @@
{
"enabled": false,
"url": "http://pokesnipers.com/api/v1/pokemon.json",
"timeout": 10,
"key": "results",
"mappings": {
"iv": { "param": "iv" },
Expand Down
12 changes: 10 additions & 2 deletions docs/configuration_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
- [Description](#description)
- [Options](#options)
- [Example](#example)
- [Sniping _(Sniper)_](#sniping-_-sniper-_)
- [Sniping _(Sniper)_](#sniper)
- [Description](#description)
- [Options](#options)
- [Example](#example)
Expand Down Expand Up @@ -725,7 +725,7 @@ This task will fetch current pokemon spawns from /raw_data of an PokemonGo-Map i
}
```

## Sniping _(Sniper)_
## Sniper
[[back to top](#table-of-contents)]

### Description
Expand Down Expand Up @@ -754,6 +754,8 @@ This task is an upgrade version of the MoveToMapPokemon task. It will fetch poke
* `sources.key` - The JSON key that contains the results, eg.: For a JSON response such as `{ "SomeWeirdoName": [{"id": 123, ...}, {"id": 143, ...}]}`, `SomeWeirdoName` would be the key name.
* `sources.url` - The URL that will provide the JSON.
* `sources.enabled` - Defines whether this source is enabled or not. This has nothing to do with the task's `enabled`.
* `sources.timeout` - How long to wait for this source to respond before giving up (default 5 seconds)
* `mappings`- Map JSON parameters to required values.
- `iv` - The JSON param that corresponds to the pokemon IV. Only certain sources provide this info. **NOTE:** `social` mode does not provide this info!
- `id` - The JSON param that corresponds to the pokemon ID. (required)
- `name` - The JSON param that corresponds to the pokemon name. (required)
Expand Down Expand Up @@ -781,6 +783,8 @@ This task is an upgrade version of the MoveToMapPokemon task. It will fetch poke
"sources": [
{
"url": "http://pokesnipers.com/api/v1/pokemon.json",
"enabled": true,
"timeout": 15,
"key": "results",
"mappings": {
"iv": { "param": "iv" },
Expand All @@ -793,6 +797,8 @@ This task is an upgrade version of the MoveToMapPokemon task. It will fetch poke
{
"url": "http://localhost:5000/raw_data",
"key": "pokemons",
"enabled": true,
"timeout": 5,
"mappings": {
"id": { "param": "pokemon_id" },
"name": { "param": "pokemon_name" },
Expand All @@ -803,6 +809,8 @@ This task is an upgrade version of the MoveToMapPokemon task. It will fetch poke
},
{
"url": "https://pokewatchers.com/grab/",
"enabled": true,
"timeout": 15,
"mappings": {
"iv": { "param": "iv" },
"id": { "param": "pid" },
Expand Down
15 changes: 8 additions & 7 deletions pokemongo_bot/cell_workers/sniper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@ def __init__(self, data):
self.enabled = data.get('enabled', False)
self.time_mask = data.get('time_mask', '%Y-%m-%d %H:%M:%S')
self.mappings = SniperSourceMapping(data.get('mappings', {}))
self.timeout = data.get('timeout', 5)

def __str__(self):
return self.url

def fetch_raw(self, timeoutz):
def fetch_raw(self):
some_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/52.0.2743.116 Safari/537.36'
response = requests.get(self.url, headers={'User-Agent': some_agent}, timeout=timeoutz)
response = requests.get(self.url, headers={'User-Agent': some_agent}, timeout=self.timeout)
results = response.json()

# If the results is a dict, retrieve the list from it by the given key. This will return a list afterall.
return results.get(self.key, []) if isinstance(results, dict) else results

def fetch(self, timeout):
def fetch(self):
pokemons = []

try:
results = self.fetch_raw(timeout)
results = self.fetch_raw()

# Parse results
for result in results:
Expand Down Expand Up @@ -105,8 +106,8 @@ def validate(self):
try:
if self.enabled:
errors = []
data = self.fetch_raw(10)

data = self.fetch_raw()
# Check whether the params really exist if they have been specified like so
if data:
if self.mappings.iv.exists and self.mappings.iv.param not in data[0]:
Expand Down Expand Up @@ -424,7 +425,7 @@ def _get_pokemons_from_url(self):
for source in self.sources:
try:
if source.enabled:
source_pokemons = source.fetch(3)
source_pokemons = source.fetch()
self._trace("Source '{}' returned {} results".format(source.url, len(source_pokemons)))

# Merge lists, making sure to exclude repeated data. Use location as the hash key
Expand Down

0 comments on commit 9c05c96

Please sign in to comment.