Skip to content

Commit

Permalink
Cleanup/remove try catch (#96)
Browse files Browse the repository at this point in the history
* Specified Exception better

* Fixed faulty logic in _remove_ignored_pokemon

* Cleaned up __init__ to not use try/catch statements.

Fixes #70

* Updated pylintrc

* Fixed some bugs

Revert changing requirements

* Fixed exceptions and pylint

Fixed bugs
  • Loading branch information
meyer9 authored and Max Leiter committed Jul 28, 2016
1 parent df6ed61 commit d130493
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 81 deletions.
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
disable=line-too-long,
missing-docstring,
relative-import,
broad-except,
too-many-instance-attributes,
too-many-arguments,
too-many-locals,
too-many-branches,
too-many-statements,
too-few-public-methods
too-few-public-methods,
too-many-return-statements
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ sudo: false
language: python
cache: pip
python:
- '2.7'
- '3.5'
- '2.7'
addons:
apt:
packages:
Expand Down
14 changes: 7 additions & 7 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ def init_config():
loaded_config = {}
with open(config.json) as data:
loaded_config.update(json.load(data))
for key in loaded_config:
if config.__dict__.get(key) is None:
config.__dict__[key] = loaded_config.get(key)
for key in config.__dict__:
if config.__dict__.get(key) is None and loaded_config.get(key) is not None:
config.__dict__[key] = loaded_config.get(key)
except Exception:
except ValueError:
logging.error("Error loading %s", config.json)
return None
for key in loaded_config:
if config.__dict__.get(key) is None:
config.__dict__[key] = loaded_config.get(key)
for key in config.__dict__:
if config.__dict__.get(key) is None and loaded_config.get(key) is not None:
config.__dict__[key] = loaded_config.get(key)

for key in config.__dict__:
if config.__dict__.get(key) is None and default_config.get(key) is not None:
Expand Down
130 changes: 59 additions & 71 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,24 @@ def _work_on_forts(self, position, map_cells):

def _remove_ignored_pokemon(self, map_cells):
if self.process_ignored_pokemon:
try:
for cell in map_cells:
for pokemon in cell['wild_pokemons'][:]:
for cell in map_cells:
wild_pokemons = cell.get('wild_pokemons')
catchable_pokemons = cell.get('catchable_pokemons')
if wild_pokemons is not None:
for pokemon in wild_pokemons[:]:
pokemon_id = pokemon['pokemon_data']['pokemon_id']
pokemon_name = [x for x in self.pokemon_list if int(x.get('Number')) == pokemon_id][0]['Name']

if pokemon_name in self.ignores:
cell['wild_pokemons'].remove(pokemon)
except KeyError:
pass

try:
for cell in map_cells:
for pokemon in cell['catchable_pokemons'][:]:
wild_pokemons.remove(pokemon)
if catchable_pokemons is not None:
for pokemon in catchable_pokemons[:]:
pokemon_id = pokemon['pokemon_id']
pokemon_name = [x for x in self.pokemon_list if int(x.get('Number')) == pokemon_id][0]['Name']

if pokemon_name in self.ignores:
cell['catchable_pokemons'].remove(pokemon)
except KeyError:
pass
catchable_pokemons.remove(pokemon)


def _work_on_catchable_pokemon(self, map_cells):
for cell in map_cells:
Expand Down Expand Up @@ -277,18 +274,14 @@ def update_inventory(self):
self.api.get_inventory()
response = self.api.call()
self.inventory = list()
try:
inventory_items = response['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']
for item in inventory_items:
try:
item_data = item['inventory_item_data']['item']
if 'item_id' not in item_data or 'count' not in item_data:
continue
self.inventory.append(item_data)
except KeyError:
pass
except KeyError:
pass
inventory_items = response.get('responses', {}).get('GET_INVENTORY', {}).get('inventory_delta', {}).get('inventory_items')
if inventory_items is None:
return
for item in inventory_items:
item_data = item.get('inventory_item_data', {}).get('item')
if item_data is None or 'item_id' not in item_data or 'count' not in item_data:
continue
self.inventory.append(item_data)

def pokeball_inventory(self):
self.api.get_player().get_inventory()
Expand All @@ -306,13 +299,13 @@ def pokeball_inventory(self):
Item.ITEM_MASTER_BALL.value: 0}

for item in inventory_list:
try:
item_id = int(item['inventory_item_data']['item']['item_id'])
item_count = int(item['inventory_item_data']['item']['count'])
if item_id in balls_stock:
balls_stock[item_id] = item_count
except KeyError:
item_data = item.get('inventory_item_data', {}).get('item')
if item_data is None:
continue
item_id = int(item_data['item_id'])
item_count = int(item_data['count'])
if item_id in balls_stock:
balls_stock[item_id] = item_count
return balls_stock

def _set_starting_position(self):
Expand All @@ -337,7 +330,7 @@ def _set_starting_position(self):
logger.log('')

return
except Exception:
except IOError:
if not self.config.location:
sys.exit("No cached Location. Please specify initial location.")
else:
Expand Down Expand Up @@ -378,18 +371,16 @@ def get_inventory_count(self, what):
return 0
pokecount = 0
itemcount = 1
try:
inventory_items = response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']
for item in inventory_items:
# print('item {}'.format(item))
if 'inventory_item_data' in item:
item_data = item['inventory_item_data']
if 'pokemon_data' in item_data:
pokecount += 1
if 'item' in item_data and 'count' in item_data['item']:
itemcount += item_data['item']['count']
except KeyError:
pass
inventory_items = response_dict.get('responses', {}).get('GET_INVENTORY', {}).get('inventory_delta', {}).get('inventory_items')
if inventory_items is None:
return 0
for item in inventory_items:
if 'inventory_item_data' in item:
item_data = item['inventory_item_data']
if 'pokemon_data' in item_data:
pokecount += 1
if 'item' in item_data and 'count' in item_data['item']:
itemcount += item_data['item']['count']

if 'pokemon' in what:
return pokecount
Expand All @@ -403,30 +394,27 @@ def get_player_info(self):
if response_dict is None:
logger.log("Couldn't get player info!", "red")
return
try:
inventory_items = response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']
for item in inventory_items:
# print('item {}'.format(item))
try:
player_stats = item['inventory_item_data']['player_stats']

if 'experience' not in player_stats:
player_stats['experience'] = 0

if 'level' in player_stats:
logger.log('[#] -- Level: {level}'.format(**player_stats))

if 'next_level_xp' in player_stats:
nextlvlxp = int(player_stats['next_level_xp']) - int(player_stats['experience'])
logger.log('[#] -- Experience: {experience}'.format(**player_stats))
logger.log('[#] -- Experience until next level: {}'.format(nextlvlxp))

if 'pokemons_captured' in player_stats:
logger.log('[#] -- Pokemon Captured: {pokemons_captured}'.format(**player_stats))

if 'poke_stop_visits' in player_stats:
logger.log('[#] -- Pokestops Visited: {poke_stop_visits}'.format(**player_stats))
except KeyError:
pass
except KeyError:
pass
inventory_items = inventory_items = response_dict.get('responses', {}).get('GET_INVENTORY', {}).get('inventory_delta', {}).get('inventory_items')
if inventory_items is None:
return
for item in inventory_items:
if item.get("inventory_item_data", {}).get("player_stats") is None:
continue
player_stats = item['inventory_item_data']['player_stats']

if 'experience' not in player_stats:
player_stats['experience'] = 0

if 'level' in player_stats:
logger.log('[#] -- Level: {level}'.format(**player_stats))

if 'next_level_xp' in player_stats:
nextlvlxp = int(player_stats['next_level_xp']) - int(player_stats['experience'])
logger.log('[#] -- Experience: {experience}'.format(**player_stats))
logger.log('[#] -- Experience until next level: {}'.format(nextlvlxp))

if 'pokemons_captured' in player_stats:
logger.log('[#] -- Pokemon Captured: {pokemons_captured}'.format(**player_stats))

if 'poke_stop_visits' in player_stats:
logger.log('[#] -- Pokestops Visited: {poke_stop_visits}'.format(**player_stats))

0 comments on commit d130493

Please sign in to comment.