diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 1a163c752b..4930a47e32 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -33,4 +33,5 @@ * steffwiz * pulgalipe * BartKoppelmans + * phil9l * VictorChen diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 1c1fe04cf0..061f4c734e 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import os import datetime import json import logging @@ -28,8 +29,8 @@ def position(self): def __init__(self, config): self.config = config - self.pokemon_list = json.load(open('data/pokemon.json')) - self.item_list = json.load(open('data/items.json')) + self.pokemon_list = json.load(open(os.path.join('data', 'pokemon.json'))) + self.item_list = json.load(open(os.path.join('data', 'items.json'))) def start(self): self._setup_logging() @@ -80,21 +81,27 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None): response_gym_details = self.api.call() fort['gym_details'] = response_gym_details['responses']['GET_GYM_DETAILS'] - user_web_location = 'web/location-%s.json' % (self.config.username) - # should check if file exists first but os is not imported here + user_web_location = os.path.join('web', 'location-%s.json' % (self.config.username)) # alt is unused atm but makes using *location easier - with open(user_web_location,'w') as outfile: - json.dump( - {'lat': lat, - 'lng': lng, - 'alt': alt, - 'cells': cells - }, outfile) - - user_data_lastlocation = 'data/last-location-%s.json' % (self.config.username) - with open(user_data_lastlocation, 'w') as outfile: - outfile.truncate() - json.dump({'lat': lat, 'lng': lng}, outfile) + try: + with open(user_web_location,'w') as outfile: + json.dump( + {'lat': lat, + 'lng': lng, + 'alt': alt, + 'cells': cells + }, outfile) + except IOError as e: + logger.log('[x] Error while opening location file: %s' % e, 'red') + + user_data_lastlocation = os.path.join('data', 'last-location-%s.json' % (self.config.username)) + try: + with open(user_data_lastlocation, 'w') as outfile: + outfile.truncate() + json.dump({'lat': lat, 'lng': lng}, outfile) + except IOError as e: + logger.log('[x] Error while opening location file: %s' % e, 'red') + def find_close_cells(self, lat, lng): cellid = get_cellid(lat, lng)