From 8470e038340197d203b239f83ea5127b874f8a9e Mon Sep 17 00:00:00 2001 From: phil9l Date: Tue, 26 Jul 2016 22:39:40 +0500 Subject: [PATCH 1/4] Handling file opening errors and and updated pathes --- pokemongo_bot/__init__.py | 40 +++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 117987a987..c6d71157eb 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 @@ -23,8 +24,8 @@ class PokemonGoBot(object): 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() @@ -75,21 +76,28 @@ 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 creating 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 creating location file: %s' % e, 'red') + def find_close_cells(self, lat, lng): cellid = get_cellid(lat, lng) From c58149950f31f530cc45331668c6c8979ae56686 Mon Sep 17 00:00:00 2001 From: phil9l Date: Tue, 26 Jul 2016 22:42:01 +0500 Subject: [PATCH 2/4] Removed extra blank line --- pokemongo_bot/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index c6d71157eb..9699ce78f4 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -89,7 +89,6 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None): except IOError as e: logger.log('[x] Error while creating 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: From 7f3c1cb883443087b05b72c9331bf611c90abe26 Mon Sep 17 00:00:00 2001 From: phil9l Date: Tue, 26 Jul 2016 22:43:47 +0500 Subject: [PATCH 3/4] Fixed error text --- pokemongo_bot/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 9699ce78f4..4a48b05d1e 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -87,7 +87,7 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None): 'cells': cells }, outfile) except IOError as e: - logger.log('[x] Error while creating location file: %s' % e, 'red') + 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: @@ -95,7 +95,7 @@ def update_web_location(self, cells=[], lat=None, lng=None, alt=None): outfile.truncate() json.dump({'lat': lat, 'lng': lng}, outfile) except IOError as e: - logger.log('[x] Error while creating location file: %s' % e, 'red') + logger.log('[x] Error while opening location file: %s' % e, 'red') def find_close_cells(self, lat, lng): From c1516b6da3f20e1ca835d863644d4189ce41555e Mon Sep 17 00:00:00 2001 From: phil9l Date: Tue, 26 Jul 2016 23:47:28 +0500 Subject: [PATCH 4/4] Adding nickname to contributors file --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 7931bab321..418dfd4d7f 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -33,3 +33,4 @@ * steffwiz * pulgalipe * BartKoppelmans + * phil9l