Skip to content

Commit

Permalink
Fix for clash between CLI and JSON args (#1420)
Browse files Browse the repository at this point in the history
* makes spin forts and catch pokemon config in json not being overwritten by default cli args value if user dont provide cli args

* 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.

* overriding config from loaded JSON is not necessary here anymore

* trying to fix the auth_service parameters

* add mixing cli args fixes that were forgotten

* fixing unicode load in location

* refactoring parameter configuration to avoid mistakes

* changed the order of functions in pokecli.py to follow some guidelines

main function comes first all all its children below
  • Loading branch information
douglascamata authored Jul 28, 2016
1 parent 522e35f commit 8852f62
Showing 1 changed file with 177 additions and 114 deletions.
291 changes: 177 additions & 114 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -67,123 +120,180 @@ 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")
required=required("auth_service"),
default=None
)
add_config(
parser,
load,
short_flag="-u",
long_flag="--username",
help="Username",
default=None
)
add_config(
parser,
load,
short_flag="-p",
long_flag="--password",
help="Password",
default=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(
"-lc",
"--location_cache",
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=''
)
add_config(
parser,
load,
short_flag="-lc",
long_flag="--location_cache",
help="Bot will start at last known location",
type=bool,
default=False
)
parser.add_argument(
"--catch_pokemon",
add_config(
parser,
load,
long_flag="--catch_pokemon",
help="Enable catching pokemon",
type=bool,
default=True
)
parser.add_argument(
"--spin_forts",
add_config(
parser,
load,
long_flag="--spin_forts",
help="Enable Spinning Pokestops",
type=bool,
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=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=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=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=False
)
parser.add_argument(
"-d",
"--debug",
add_config(
parser,
load,
short_flag="-d",
long_flag="--debug",
help="Debug Mode",
type=bool,
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="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=[]
)
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=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=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=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=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=True
Expand All @@ -196,11 +306,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', {})
Expand Down Expand Up @@ -230,65 +335,23 @@ 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

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)
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()

0 comments on commit 8852f62

Please sign in to comment.