Advertisement
Guest User

adding configparser

a guest
Jul 26th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. import json
  2. import logging
  3. import os
  4. import sys
  5. import time
  6. import ConfigParser
  7.  
  8. from datetime import datetime
  9. from geopy.distance import vincenty
  10. from pgoapi import PGoApi
  11.  
  12. from pokedata import json_deserializer, json_serializer
  13. from pokesearch import Pokesearch
  14. from pokeslack import Pokeslack
  15. from pokeutil import get_pos_by_name
  16.  
  17. logger = logging.getLogger(__name__)
  18.  
  19. if __name__ == '__main__':
  20.  
  21.     logging.basicConfig(stream=sys.stdout, level=logging.INFO)
  22.     logging.getLogger("requests").setLevel(logging.WARNING)
  23.     logging.getLogger("pgoapi.pgoapi").setLevel(logging.WARNING)
  24.     logging.getLogger("pgoapi.rpc_api").setLevel(logging.WARNING)
  25.  
  26.     # used for local testing without starting up heroku
  27.   #  env = {}
  28.   #  if os.path.exists('.env'):
  29.   #      with open('.env', 'r') as fp:
  30.   #          for line in fp:
  31.   #              parts = line.split('=')
  32.   #              env[parts[0].strip()] = parts[1].strip()
  33.  
  34.   # auth_service = str(os.environ.get('AUTH_SERVICE', env.get('AUTH_SERVICE')))
  35.   # username = str(os.environ.get('USERNAME', env.get('USERNAME')))
  36.   # password = str(os.environ.get('PASSWORD', env.get('PASSWORD')))
  37.   # location_name = str(os.environ.get('LOCATION_NAME', env.get('LOCATION_NAME')))
  38.   # rarity_limit = int(os.environ.get('RARITY_LIMIT', env.get('RARITY_LIMIT')))
  39.   # slack_webhook_url = str(os.environ.get('SLACK_WEBHOOK_URL', env.get('SLACK_WEBHOOK_URL')))
  40.  
  41.     configParser = ConfigParser.RawConfigParser()
  42.     configFilePath = r'ps.config'
  43.     configParser.read(configFilePath)
  44.  
  45.     auth_service = configParser.get('ps-config','AUTH_SERVICE')
  46.     username = configParser.get('ps-config','USERNAME')
  47.     password = configParser.get('ps-config','PASSWORD')
  48.     location_name = configParser.get('ps-config','LOCATION_NAME')
  49.     rarity_limit = configParser.get('ps-config','RARITY_LIMIT')
  50.     slack_webhook_url = configParser.get('ps-config','SLACK_WEBHOOK_URL')
  51.  
  52.     # const vars
  53.     step_size = 0.0025
  54.     step_limit = 3
  55.  
  56.     # debug vars, used to test slack integration w/o waiting
  57.     use_cache = False
  58.     cached_filename = 'cached_pokedata.json'
  59.     search_timeout = 30
  60.  
  61.     position, address = get_pos_by_name(location_name)
  62.     logger.info('location_name: %s', address)
  63.  
  64.     api = PGoApi()
  65.     pokesearch = Pokesearch(api, auth_service, username, password, position)
  66.     pokeslack = Pokeslack(rarity_limit, slack_webhook_url)
  67.  
  68.     if not use_cache or not os.path.exists(cached_filename):
  69.         logger.info('searching starting at latlng: (%s, %s)', position[0], position[1])
  70.         pokesearch.login()
  71.         while True:
  72.             pokemons = []
  73.             for pokemon in pokesearch.search(position[0], position[1], step_limit, step_size):
  74.                 pokemon_position = (pokemon['latitude'], pokemon['longitude'], 0)
  75.                 distance = vincenty(position, pokemon_position).miles
  76.                 expires_in = pokemon['disappear_time'] - datetime.utcnow()
  77.                 logger.info("adding pokemon: %s - %s, rarity: %s, expires in: %s, distance: %s miles", pokemon['pokemon_id'], pokemon['name'], pokemon['rarity'], expires_in, distance)
  78.                 pokeslack.try_send_pokemon(pokemon, position, distance, debug=False)
  79.                 pokemons.append(pokemon)
  80.             with open(cached_filename, 'w') as fp:
  81.                 json.dump(pokemons, fp, default=json_serializer, indent=4)
  82.             logging.info('done searching, waiting %s seconds...', search_timeout)
  83.             time.sleep(search_timeout)
  84.     else:
  85.         with open(cached_filename, 'r') as fp:
  86.             pokemons = json.load(fp, object_hook=json_deserializer)
  87.             for pokemon in pokemons:
  88.                 pokemon_position = (pokemon['latitude'], pokemon['longitude'], 0)
  89.                 distance = vincenty(position, pokemon_position).miles
  90.                 pokeslack.try_send_pokemon(pokemon, position, distance, debug=True)
  91.         logger.info('loaded cached pokemon data for %s pokemon', len(pokemons))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement