From d7b4f99739ee316712f278b1a48c50a1d30557a5 Mon Sep 17 00:00:00 2001 From: Douglas Camata Date: Thu, 28 Jul 2016 16:40:40 +0200 Subject: [PATCH 1/8] makes spin forts and catch pokemon config in json not being overwritten by default cli args value if user dont provide cli args --- pokecli.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pokecli.py b/pokecli.py index c912eecb2a..0043d5f457 100755 --- a/pokecli.py +++ b/pokecli.py @@ -201,6 +201,8 @@ def init_config(): if key in config and value: setattr(config, key, value) + config.spin_forts = load.get('spin_forts', config.spin_forts) + config.catch_pokemon = load.get('catch_pokemon', config.catch_pokemon) config.catch = load.get('catch', {}) config.release = load.get('release', {}) config.item_filter = load.get('item_filter', {}) From b44572a03aa0577f12a6e852cd8f0a3dfc31f56e Mon Sep 17 00:00:00 2001 From: Douglas Camata Date: Thu, 28 Jul 2016 17:04:19 +0200 Subject: [PATCH 2/8] huge fix to how cli and json parameters are loaded The CLI parameter parser now uses JSON-loaded parameters as first fallback to missing parameters. The second fallback to missing parameters are the default values previously used. This is the perfect handling for making CLI args override JSON configuration only for provided args. Non-provided args that are not found in are set to the default value we think most users are going to like. --- pokecli.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/pokecli.py b/pokecli.py index 0043d5f457..496833331d 100755 --- a/pokecli.py +++ b/pokecli.py @@ -87,13 +87,13 @@ def init_config(): "--catch_pokemon", help="Enable catching pokemon", type=bool, - default=True + default=load.get('catch_pokemon', True) ) parser.add_argument( "--spin_forts", help="Enable Spinning Pokestops", type=bool, - default=True + default=load.get('spin_forts', True) ) parser.add_argument( "-w", @@ -101,14 +101,14 @@ def init_config(): help= "Walk instead of teleport with given speed (meters per second, e.g. 2.5)", type=float, - default=2.5 + default=load.get('walk', 2.5) ) parser.add_argument( "-k", "--gmapkey", help="Set Google Maps API KEY", type=str, - default=None + default=load.get('gmapkey', None) ) parser.add_argument( "-ms", @@ -116,21 +116,21 @@ def init_config(): help= "Set the steps around your initial location(DEFAULT 5 mean 25 cells around your location)", type=int, - default=50 + default=load.get('max_steps', 50) ) parser.add_argument( "-rp", "--release_pokemon", help="Allow transfer pokemon to professor based on release configuration. Default is false", type=bool, - default=False + default=load.get('release_pokemon', False) ) parser.add_argument( "-d", "--debug", help="Debug Mode", type=bool, - default=False + default=load.get('debug', False) ) parser.add_argument( "-t", @@ -144,49 +144,49 @@ def init_config(): "--distance_unit", help="Set the unit to display distance in (e.g, km for kilometers, mi for miles, ft for feet)", type=str, - default="km" + default=load.get('distance_unit', 'km') ) parser.add_argument( "-ev", "--evolve_all", help="(Batch mode) Pass \"all\" or a list of pokemons to evolve (e.g., \"Pidgey,Weedle,Caterpie\"). Bot will start by attempting to evolve all pokemons. Great after popping a lucky egg!", type=str, - default=[] + default=load.get('evolve_all', []) ) parser.add_argument( "-cm", "--cp_min", help="Minimum CP for evolve all. Bot will attempt to first evolve highest IV pokemons with CP larger than this.", type=int, - default=300 + default=load.get('cp_min', 300) ) parser.add_argument( "-ec", "--evolve_captured", help="(Ad-hoc mode) Bot will attempt to evolve all the pokemons captured!", type=bool, - default=False + default=load.get('evolve_captured', False) ) parser.add_argument( "-le", "--use_lucky_egg", help="Uses lucky egg when using evolve_all", type=bool, - default=False + default=load.get('use_lucky_egg', False) ) parser.add_argument( "-rt", "--reconnecting_timeout", help="Timeout between reconnecting if error occured (in minutes, e.g. 15)", type=float, - default=15.0 + default=load.get('reconnecting_timeout', 15.0) ) parser.add_argument( "-hr", "--health_record", help="Send anonymous bot event to GA for bot health record. Set \"health_record\":false if you need disable it.", type=bool, - default=True + default=load.get('health_record', True) ) # Start to parse other attrs @@ -201,8 +201,6 @@ def init_config(): if key in config and value: setattr(config, key, value) - config.spin_forts = load.get('spin_forts', config.spin_forts) - config.catch_pokemon = load.get('catch_pokemon', config.catch_pokemon) config.catch = load.get('catch', {}) config.release = load.get('release', {}) config.item_filter = load.get('item_filter', {}) @@ -229,7 +227,7 @@ def init_config(): if not os.path.isdir(web_dir): raise - if config.evolve_all: + if config.evolve_all and isinstance(config.evolve_all, str): config.evolve_all = [str(pokemon_name) for pokemon_name in config.evolve_all.split(',')] return config From 6dfb640e4a30788111790527fa4c67091c8a5b5f Mon Sep 17 00:00:00 2001 From: Douglas Camata Date: Thu, 28 Jul 2016 17:19:57 +0200 Subject: [PATCH 3/8] overriding config from loaded JSON is not necessary here anymore --- pokecli.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pokecli.py b/pokecli.py index 496833331d..2f2d9018d2 100755 --- a/pokecli.py +++ b/pokecli.py @@ -196,11 +196,6 @@ def init_config(): if not config.password and 'password' not in load: config.password = getpass("Password: ") - # Passed in arguments should trump - for key, value in load.iteritems(): - if key in config and value: - setattr(config, key, value) - config.catch = load.get('catch', {}) config.release = load.get('release', {}) config.item_filter = load.get('item_filter', {}) From ef90572f26c74a3729456749fc4d71f068309185 Mon Sep 17 00:00:00 2001 From: Douglas Camata Date: Thu, 28 Jul 2016 17:34:57 +0200 Subject: [PATCH 4/8] trying to fix the auth_service parameters --- pokecli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pokecli.py b/pokecli.py index 2f2d9018d2..7b77b2bb60 100755 --- a/pokecli.py +++ b/pokecli.py @@ -71,7 +71,8 @@ def init_config(): "-a", "--auth_service", help="Auth Service ('ptc' or 'google')", - required=required("auth_service") + required=required("auth_service"), + default=load.get('auth_service', None) ) parser.add_argument("-u", "--username", help="Username") parser.add_argument("-p", "--password", help="Password") From 5f4c818c51eb330919d17f9abd995e93bb705585 Mon Sep 17 00:00:00 2001 From: Douglas Camata Date: Thu, 28 Jul 2016 17:43:53 +0200 Subject: [PATCH 5/8] add mixing cli args fixes that were forgotten --- pokecli.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pokecli.py b/pokecli.py index 7b77b2bb60..162fc41027 100755 --- a/pokecli.py +++ b/pokecli.py @@ -74,15 +74,31 @@ def init_config(): required=required("auth_service"), default=load.get('auth_service', None) ) - parser.add_argument("-u", "--username", help="Username") - parser.add_argument("-p", "--password", help="Password") - parser.add_argument("-l", "--location", help="Location", type=lambda s: unicode(s, 'utf8')) + parser.add_argument( + "-u", + "--username", + help="Username", + default=load.get('username', None) + ) + parser.add_argument( + "-p", + "--password", + help="Password", + default=load.get('password', None) + ) + parser.add_argument( + "-l", + "--location", + help="Location", + type=lambda s: unicode(s, 'utf8'), + default=load.get('location', None) + ) parser.add_argument( "-lc", "--location_cache", help="Bot will start at last known location", type=bool, - default=False + default=load.get('location_cache', False) ) parser.add_argument( "--catch_pokemon", From 8a0f42062ed84949a46f55a87a2e67698c238f16 Mon Sep 17 00:00:00 2001 From: Douglas Camata Date: Thu, 28 Jul 2016 17:57:08 +0200 Subject: [PATCH 6/8] fixing unicode load in location --- pokecli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pokecli.py b/pokecli.py index 162fc41027..8a7e7bb5ec 100755 --- a/pokecli.py +++ b/pokecli.py @@ -90,8 +90,8 @@ def init_config(): "-l", "--location", help="Location", - type=lambda s: unicode(s, 'utf8'), - default=load.get('location', None) + type=lambda s: not isinstance(s, unicode) and unicode(s, 'utf8') or str(s), + default=load.get('location', '') ) parser.add_argument( "-lc", From 7215598074db2b6e26810da516186a21456204ce Mon Sep 17 00:00:00 2001 From: Douglas Camata Date: Thu, 28 Jul 2016 18:37:11 +0200 Subject: [PATCH 7/8] refactoring parameter configuration to avoid mistakes --- pokecli.py | 207 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 130 insertions(+), 77 deletions(-) diff --git a/pokecli.py b/pokecli.py index 8a7e7bb5ec..82c649a097 100755 --- a/pokecli.py +++ b/pokecli.py @@ -67,143 +67,183 @@ def init_config(): # Read passed in Arguments required = lambda x: not x in load - parser.add_argument( - "-a", - "--auth_service", + add_config( + parser, + load, + short_flag="-a", + long_flag="--auth_service", help="Auth Service ('ptc' or 'google')", required=required("auth_service"), - default=load.get('auth_service', None) + default=None ) - parser.add_argument( - "-u", - "--username", + add_config( + parser, + load, + short_flag="-u", + long_flag="--username", help="Username", - default=load.get('username', None) + default=None ) - parser.add_argument( - "-p", - "--password", + add_config( + parser, + load, + short_flag="-p", + long_flag="--password", help="Password", - default=load.get('password', None) + default=None ) - parser.add_argument( - "-l", - "--location", + add_config( + parser, + load, + short_flag="-l", + long_flag="--location", help="Location", type=lambda s: not isinstance(s, unicode) and unicode(s, 'utf8') or str(s), - default=load.get('location', '') + default='' ) - parser.add_argument( - "-lc", - "--location_cache", + add_config( + parser, + load, + short_flag="-lc", + long_flag="--location_cache", help="Bot will start at last known location", type=bool, - default=load.get('location_cache', False) + default=False ) - parser.add_argument( - "--catch_pokemon", + add_config( + parser, + load, + long_flag="--catch_pokemon", help="Enable catching pokemon", type=bool, - default=load.get('catch_pokemon', True) + default=True ) - parser.add_argument( - "--spin_forts", + add_config( + parser, + load, + long_flag="--spin_forts", help="Enable Spinning Pokestops", type=bool, - default=load.get('spin_forts', True) + default=True ) - parser.add_argument( - "-w", - "--walk", + add_config( + parser, + load, + short_flag="-w", + long_flag="--walk", help= "Walk instead of teleport with given speed (meters per second, e.g. 2.5)", type=float, - default=load.get('walk', 2.5) + default=2.5 ) - parser.add_argument( - "-k", - "--gmapkey", + add_config( + parser, + load, + short_flag="-k", + long_flag="--gmapkey", help="Set Google Maps API KEY", type=str, - default=load.get('gmapkey', None) + default=None ) - parser.add_argument( - "-ms", - "--max_steps", + add_config( + parser, + load, + short_flag="-ms", + long_flag="--max_steps", help= "Set the steps around your initial location(DEFAULT 5 mean 25 cells around your location)", type=int, - default=load.get('max_steps', 50) + default=50 ) - parser.add_argument( - "-rp", - "--release_pokemon", + add_config( + parser, + load, + short_flag="-rp", + long_flag="--release_pokemon", help="Allow transfer pokemon to professor based on release configuration. Default is false", type=bool, - default=load.get('release_pokemon', False) + default=False ) - parser.add_argument( - "-d", - "--debug", + add_config( + parser, + load, + short_flag="-d", + long_flag="--debug", help="Debug Mode", type=bool, - default=load.get('debug', False) + default=False ) - parser.add_argument( - "-t", - "--test", + add_config( + parser, + load, + short_flag="-t", + long_flag="--test", help="Only parse the specified location", type=bool, default=False ) - parser.add_argument( - "-du", - "--distance_unit", + add_config( + parser, + load, + short_flag="-du", + long_flag="--distance_unit", help="Set the unit to display distance in (e.g, km for kilometers, mi for miles, ft for feet)", type=str, - default=load.get('distance_unit', 'km') + default='km' ) - parser.add_argument( - "-ev", - "--evolve_all", + add_config( + parser, + load, + short_flag="-ev", + long_flag="--evolve_all", help="(Batch mode) Pass \"all\" or a list of pokemons to evolve (e.g., \"Pidgey,Weedle,Caterpie\"). Bot will start by attempting to evolve all pokemons. Great after popping a lucky egg!", type=str, - default=load.get('evolve_all', []) + default=[] ) - parser.add_argument( - "-cm", - "--cp_min", + add_config( + parser, + load, + short_flag="-ecm", + long_flag="--evolve_cp_min", help="Minimum CP for evolve all. Bot will attempt to first evolve highest IV pokemons with CP larger than this.", type=int, - default=load.get('cp_min', 300) + default=300 ) - parser.add_argument( - "-ec", - "--evolve_captured", + add_config( + parser, + load, + short_flag="-ec", + long_flag="--evolve_captured", help="(Ad-hoc mode) Bot will attempt to evolve all the pokemons captured!", type=bool, - default=load.get('evolve_captured', False) + default=False ) - parser.add_argument( - "-le", - "--use_lucky_egg", + add_config( + parser, + load, + short_flag="-le", + long_flag="--use_lucky_egg", help="Uses lucky egg when using evolve_all", type=bool, - default=load.get('use_lucky_egg', False) + default=False ) - parser.add_argument( - "-rt", - "--reconnecting_timeout", + add_config( + parser, + load, + short_flag="-rt", + long_flag="--reconnecting_timeout", help="Timeout between reconnecting if error occured (in minutes, e.g. 15)", type=float, - default=load.get('reconnecting_timeout', 15.0) + default=15.0 ) - parser.add_argument( - "-hr", - "--health_record", + add_config( + parser, + load, + short_flag="-hr", + long_flag="--health_record", help="Send anonymous bot event to GA for bot health record. Set \"health_record\":false if you need disable it.", type=bool, - default=load.get('health_record', True) + default=True ) # Start to parse other attrs @@ -245,6 +285,19 @@ def init_config(): return config +def add_config(parser, json_config, short_flag=None, long_flag=None, **kwargs): + if not long_flag: + raise Exception('add_config calls requires long_flag parameter!') + if 'default' in kwargs: + attribute_name = long_flag.split('--')[1] + kwargs['default'] = json_config.get(attribute_name, kwargs['default']) + if short_flag: + args = (short_flag, long_flag) + else: + args = (long_flag,) + parser.add_argument(*args, **kwargs) + + def main(): logger.log('PokemonGO Bot v1.0', 'green') sys.stdout = codecs.getwriter('utf8')(sys.stdout) From 9076926392f6d059624fd6af77084da703c95bee Mon Sep 17 00:00:00 2001 From: Douglas Camata Date: Thu, 28 Jul 2016 18:38:24 +0200 Subject: [PATCH 8/8] changed the order of functions in pokecli.py to follow some guidelines main function comes first all all its children below --- pokecli.py | 110 ++++++++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 56 deletions(-) diff --git a/pokecli.py b/pokecli.py index 82c649a097..66e1f80af0 100755 --- a/pokecli.py +++ b/pokecli.py @@ -44,6 +44,59 @@ ssl._create_default_https_context = ssl._create_unverified_context +def main(): + logger.log('PokemonGO Bot v1.0', 'green') + sys.stdout = codecs.getwriter('utf8')(sys.stdout) + sys.stderr = codecs.getwriter('utf8')(sys.stderr) + + config = init_config() + if not config: + return + logger.log('Configuration initialized', 'yellow') + + finished = False + + while not finished: + try: + bot = PokemonGoBot(config) + bot.start() + bot.metrics.capture_stats() + + logger.log('Starting PokemonGo Bot....', 'green') + + while True: + bot.tick() + + except KeyboardInterrupt: + logger.log('Exiting PokemonGo Bot', 'red') + finished = True + if bot.metrics.start_time is None: + return # Bot didn't actually start, no metrics to show. + + metrics = bot.metrics + metrics.capture_stats() + logger.log('') + logger.log('Ran for {}'.format(metrics.runtime()), 'cyan') + logger.log('Total XP Earned: {} Average: {:.2f}/h'.format(metrics.xp_earned(), metrics.xp_per_hour()), 'cyan') + logger.log('Travelled {:.2f}km'.format(metrics.distance_travelled()), 'cyan') + logger.log('Visited {} stops'.format(metrics.visits['latest'] - metrics.visits['start']), 'cyan') + logger.log('Encountered {} pokemon, {} caught, {} released, {} evolved, {} never seen before' + .format(metrics.num_encounters(), metrics.num_captures(), metrics.releases, + metrics.num_evolutions(), metrics.num_new_mons()), 'cyan') + logger.log('Threw {} pokeball{}'.format(metrics.num_throws(), '' if metrics.num_throws() == 1 else 's'), + 'cyan') + logger.log('Earned {} Stardust'.format(metrics.earned_dust()), 'cyan') + logger.log('') + if metrics.highest_cp is not None: + logger.log('Highest CP Pokemon: {}'.format(metrics.highest_cp['desc']), 'cyan') + if metrics.most_perfect is not None: + logger.log('Most Perfect Pokemon: {}'.format(metrics.most_perfect['desc']), 'cyan') + + + except NotLoggedInException: + logger.log('[x] Error while connecting to the server, please wait %s minutes' % config.reconnecting_timeout, 'red') + time.sleep(config.reconnecting_timeout * 60) + def init_config(): parser = argparse.ArgumentParser() config_file = "configs/config.json" @@ -284,7 +337,6 @@ def init_config(): return config - def add_config(parser, json_config, short_flag=None, long_flag=None, **kwargs): if not long_flag: raise Exception('add_config calls requires long_flag parameter!') @@ -297,60 +349,6 @@ def add_config(parser, json_config, short_flag=None, long_flag=None, **kwargs): args = (long_flag,) parser.add_argument(*args, **kwargs) - -def main(): - logger.log('PokemonGO Bot v1.0', 'green') - sys.stdout = codecs.getwriter('utf8')(sys.stdout) - sys.stderr = codecs.getwriter('utf8')(sys.stderr) - - config = init_config() - if not config: - return - logger.log('Configuration initialized', 'yellow') - - finished = False - - while not finished: - try: - bot = PokemonGoBot(config) - bot.start() - bot.metrics.capture_stats() - - logger.log('Starting PokemonGo Bot....', 'green') - - while True: - bot.tick() - - except KeyboardInterrupt: - logger.log('Exiting PokemonGo Bot', 'red') - finished = True - if bot.metrics.start_time is None: - return # Bot didn't actually start, no metrics to show. - - metrics = bot.metrics - metrics.capture_stats() - logger.log('') - logger.log('Ran for {}'.format(metrics.runtime()), 'cyan') - logger.log('Total XP Earned: {} Average: {:.2f}/h'.format(metrics.xp_earned(), metrics.xp_per_hour()), 'cyan') - logger.log('Travelled {:.2f}km'.format(metrics.distance_travelled()), 'cyan') - logger.log('Visited {} stops'.format(metrics.visits['latest'] - metrics.visits['start']), 'cyan') - logger.log('Encountered {} pokemon, {} caught, {} released, {} evolved, {} never seen before' - .format(metrics.num_encounters(), metrics.num_captures(), metrics.releases, - metrics.num_evolutions(), metrics.num_new_mons()), 'cyan') - logger.log('Threw {} pokeball{}'.format(metrics.num_throws(), '' if metrics.num_throws() == 1 else 's'), - 'cyan') - logger.log('Earned {} Stardust'.format(metrics.earned_dust()), 'cyan') - logger.log('') - if metrics.highest_cp is not None: - logger.log('Highest CP Pokemon: {}'.format(metrics.highest_cp['desc']), 'cyan') - if metrics.most_perfect is not None: - logger.log('Most Perfect Pokemon: {}'.format(metrics.most_perfect['desc']), 'cyan') - - - except NotLoggedInException: - logger.log('[x] Error while connecting to the server, please wait %s minutes' % config.reconnecting_timeout, 'red') - time.sleep(config.reconnecting_timeout * 60) - - + if __name__ == '__main__': main()