コード例 #1
0
ファイル: pokecli.py プロジェクト: tetsunosuke/pgoapi
def my_main():
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Position could not be found by name')
        return
        
    if config.test:
        return

    # instantiate pgoapi
    api = pgoapi.PGoApi()
    api.set_position(*position)
    if not api.login(config.auth_service, config.username, config.password):
        return

    response_dict = api.get_inventory()

    # execute the RPC call
    with open("inventory.json", "w") as f:
        json.dump(response_dict,f, indent=4, sort_keys=True)
    inventory_items =  response_dict["responses"]["GET_INVENTORY"]["inventory_delta"]["inventory_items"]
    my_pokemons = {}
    for item in inventory_items:
        try:
            if item["inventory_item_data"].has_key("pokemon_data"):
                pokemon_data = item["inventory_item_data"]["pokemon_data"]
                if pokemon_data.has_key("pokemon_id"):
                    #log.debug("id=%s, name=%s, cp=%s", pokemon_data["pokemon_id"], poke_id2name(pokemon_data["pokemon_id"]), pokemon_data["cp"])
                    if my_pokemons.has_key(pokemon_data["pokemon_id"]):
                        my_pokemons[pokemon_data["pokemon_id"]].append(pokemon_data)
                    else:
                        my_pokemons[pokemon_data["pokemon_id"]] = [pokemon_data]
                      
        except:
            traceback.print_exc()
    #log.info(my_pokemons)
    with open("pokemons.json", "w") as f:
        json.dump(my_pokemons,f, indent=4, sort_keys=True)

    # 保持しているデータを処理
    for id in my_pokemons:
        owns = my_pokemons[id]
        for pokemon in owns:
            if pokemon.has_key("favorite"):
                log.debug(poke_id2name(id))
                continue
            if weaker(pokemon) or nomore(pokemon):
                time.sleep(3)
                dict = api.release_pokemon(pokemon_id = pokemon["id"])
コード例 #2
0
def main():

	#All LEDs should be disabled initially 
    enable_led(led1, False)
    enable_led(led2, False)
    enable_led(led3, False)
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    #load the config 
    config = init_config()
    #load the pgoapi
    api = pgoapi.PGoapi()
    #get position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Could not find the position')
        return
    elif config.test:
        return

    api.set_position(*position) 
コード例 #3
0
ファイル: pokecli.py プロジェクト: rnadna1/fsd
def main():
    # log settings
    # log format
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service,
                     config.username,
                     config.password,
                     app_simulation=True):
        return

    # get player profile call (single command example)
    # ----------------------
    response_dict = api.get_player()
    print('Response dictionary (get_player): \n\r{}'.format(
        pprint.PrettyPrinter(indent=4).pformat(response_dict)))

    # sleep due to server-side throttling
    time.sleep(0.2)

    # get player profile + inventory call (thread-safe/chaining example)
    # ----------------------
    req = api.create_request()
    req.get_player()
    req.get_inventory()
    response_dict = req.call()
    print('Response dictionary (get_player + get_inventory): \n\r{}'.format(
        pprint.PrettyPrinter(indent=4).pformat(response_dict)))
コード例 #4
0
ファイル: search.py プロジェクト: Exosum/pogom
def set_location(location, radius):
    position = get_pos_by_name(location)
    log.info('Parsed location is: {:.4f}/{:.4f}/{:.4f} (lat/lng/alt)'.
             format(*position))

    SearchConfig.ORIGINAL_LATITUDE = position[0]
    SearchConfig.ORIGINAL_LONGITUDE = position[1]
    SearchConfig.RADIUS = radius
コード例 #5
0
def main():
    # log settings
    # log format
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # Load pokemon
    pokemon_list = []
    pokemon_dict = json.load(open('examples/pogo-optimizer/data/pokemon.json'))

    # set player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service,
                     config.username,
                     config.password,
                     app_simulation=True):
        return

    lat, lng, x = api.get_position()
    origin = LatLng.from_degrees(lat, lng)
    parent = CellId.from_lat_lng(LatLng.from_degrees(lat, lng)).parent(15)

    # get player profile call (single command example)
    # ----------------------
    response_dict = api.get_player()
    print('Response dictionary (get_player): \n\r{}'.format(
        pprint.PrettyPrinter(indent=4).pformat(response_dict)))

    # sleep due to server-side throttling
    time.sleep(0.2)
コード例 #6
0
ファイル: pokecli.py プロジェクト: Brenza/pgoapi
def main():
    # log settings
    # log format
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    # new authentication initialitation
    api.set_authentication(provider=config.auth_service,
                           username=config.username,
                           password=config.password)

    # provide the path for your encrypt dll
    api.activate_signature("encrypt.dll")

    # print get maps object
    cell_ids = util.get_cell_ids(position[0], position[1])
    timestamps = [
        0,
    ] * len(cell_ids)
    response_dict = api.get_map_objects(latitude=position[0],
                                        longitude=position[1],
                                        since_timestamp_ms=timestamps,
                                        cell_id=cell_ids)
    print('Response dictionary (get_player): \n\r{}'.format(
        pprint.PrettyPrinter(indent=4).pformat(response_dict)))
コード例 #7
0
ファイル: main.py プロジェクト: Boren/PokemonGO-IV-Renamer
    def get_location(self):
        # use lat/lng directly if matches such a pattern
        prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
        res = prog.match(self.config.location)
        if res:
            print "Using coordinates from CLI directly"
            self.position = (float(res.group(1)), float(res.group(2)), 0)
        else:
            print "Looking up coordinates in API"
            self.position = util.get_pos_by_name(self.config.location)

        self.get_elevation_for_position()
コード例 #8
0
ファイル: main.py プロジェクト: tegob/PokemonGO-IV-Renamer
    def get_location(self):
        # use lat/lng directly if matches such a pattern
        prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
        res = prog.match(self.config.location)
        if res:
            print "Using coordinates from CLI directly..."
            self.position = (float(res.group(1)), float(res.group(2)), 0)
        else:
            print "Looking up coordinates using API..."
            self.position = util.get_pos_by_name(self.config.location)

        self.get_elevation_for_position()
コード例 #9
0
ファイル: pokecli.py プロジェクト: Eth4nZ/pgoapi
def main():
    # log settings
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)


    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service, config.username, config.password, app_simulation = True):
        return

    # get player profile call (single command example)
    # ----------------------
    response_dict = api.get_player()
    print('Response dictionary (get_player): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
    
    # sleep 200ms due to server-side throttling
    time.sleep(0.2)

    # get player profile + inventory call (thread-safe/chaining example)
    # ----------------------
    req = api.create_request()
    req.get_player()
    req.get_inventory()
    response_dict = req.call()
    print('Response dictionary (get_player + get_inventory): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
コード例 #10
0
ファイル: pokesearch.py プロジェクト: jpchen/WhereMewtwoAt
def main():
    # log settings
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # Load pokemon
    pokemon_list = []
    pokemon_dict = json.load(open('examples/pogo-optimizer/data/pokemon.json'))

    # set player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service, config.username, config.password, app_simulation = True):
        return

    lat,lng,x = api.get_position()
    origin = LatLng.from_degrees(lat,lng)
    parent = CellId.from_lat_lng(LatLng.from_degrees(lat, lng)).parent(15)

    # get player profile call (single command example)
    # ----------------------
    response_dict = api.get_player()
    print('Response dictionary (get_player): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))

    # sleep due to server-side throttling
    time.sleep(0.2)
コード例 #11
0
ファイル: search.py プロジェクト: gsabran/PokemonGo-Map
def search(args, i):
    num_steps = args.step_limit
    total_steps = (3 * (num_steps**2)) - (3 * num_steps) + 1

    position = get_pos_by_name(args.location)
    position = (position[0], position[1], 0)

    api = args.api
    if api._auth_provider and api._auth_provider._ticket_expire:
        remaining_time = api._auth_provider._ticket_expire/1000 - time.time()

        if remaining_time > 60:
            log.info("Skipping Pokemon Go login process since already logged in for another {:.2f} seconds".format(remaining_time))
        else:
            login(args, position)
    else:
        login(args, position)

    for step, step_location in enumerate(generate_location_steps(position, num_steps), 1):
        log.info('Scanning step {:d} of {:d}.'.format(step, total_steps))
        log.info('Scan location is {:f}, {:f}'.format(step_location[0], step_location[1]))
        update_player_position(args, step_location[0], step_location[1])

        response_dict = {}
        failed_consecutive = 0
        while not response_dict:
            if player_has_reset_initial_position(args):
                # we stop this loop and start another one
                player_id = get_player_id(args)
                player = Player.select().where(Player.player_id == player_id)[0]
                args.location = str(player.start_latitude) + ' ' + str(player.start_longitude)
                search_loop(args)
                return

            response_dict = send_map_request(api, step_location)
            if response_dict:
                try:
                    parse_map(response_dict, i, step, step_location)
                except KeyError:
                    log.error('Scan step {:d} failed. Response dictionary key error.'.format(step))
                    failed_consecutive += 1
                    if(failed_consecutive >= config['REQ_MAX_FAILED']):
                        log.error('Niantic servers under heavy load. Waiting before trying again')
                        time.sleep(config['REQ_HEAVY_SLEEP'])
                        failed_consecutive = 0
            else:
                log.info('Map Download failed. Trying again.')

        log.info('Completed {:5.2f}% of scan.'.format(float(step) / num_steps**2*100))
        time.sleep(config['REQ_SLEEP'])
    return True
コード例 #12
0
ファイル: pokecli.py プロジェクト: chenditc/pgoapi
def main():
    # log settings
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)


    # instantiate pgoapi
    api = pgoapi.PGoApi()
    if config.proxy:
        api.set_proxy({'http': config.proxy, 'https': config.proxy})

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    # new authentication initialitation
    if config.proxy:
        api.set_authentication(provider = config.auth_service, username = config.username, password =  config.password, proxy_config = {'http': config.proxy, 'https': config.proxy})
    else:
        api.set_authentication(provider = config.auth_service, username = config.username, password =  config.password)

    # provide the path for your encrypt dll
    api.activate_signature("/usr/lib/libencrypt.so")

    # print get maps object
    cell_ids = util.get_cell_ids(position[0], position[1])
    timestamps = [0,] * len(cell_ids)
    response_dict = api.get_map_objects(latitude =position[0], longitude = position[1], since_timestamp_ms = timestamps, cell_id = cell_ids)
    print('Response dictionary (get_player): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
コード例 #13
0
def main():
    enable_led(LED_PIN1, False)
    enable_led(LED_PIN2, False)
    enable_led(LED_PIN3, False)
    # log format
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    # new authentication initialitation
    api.set_authentication(provider=config.auth_service,
                           username=config.username,
                           password=config.password)

    # provide the FULL PATH for your encrypt .so
    api.activate_signature("/home/pi/pgoapi/libencrypt.so")

    while True:
        find_poi(api, position[0], position[1])
        time.sleep(30)
コード例 #14
0
ファイル: runserver.py プロジェクト: SkOODaT/RocketMap
def extract_coordinates(location):
    # Use lat/lng directly if matches such a pattern.
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(location)

    if position is None or not any(position):
        log.error("Location not found: '{}'".format(location))
        sys.exit()
    return position
コード例 #15
0
 def login(self):
     if self["config"]["location"] is None:
         print("Required location not provided")
         return
     #set location
     position = util.get_pos_by_name(self["config"]["location"])
     if not position:
         print("Invalid location")
         return
     self["api"].set_position(*position)
     # login
     if not self["api"].login(self["config"]["auth_service"], self["config"]["username"], self["config"]["password"], app_simulation = True):
         print("error logging in...")
     else:
         self.update_player_and_inventory()
         self.init_info()
コード例 #16
0
ファイル: search.py プロジェクト: gsabran/PokemonGo-Map
def initialize_player_position(args):
    player_id = get_player_id(args)
    start_position = get_pos_by_name(args.location)

    if len(Player.select().where(Player.player_id == player_id)) == 0:
        Player.create(
            player_id=player_id,
            name=args.username,
            enabled=True,
            last_modified=datetime.now(),
            start_latitude=start_position[0],
            start_longitude=start_position[1]
        )
    else:
        Player.update(
            last_modified=datetime.now(),
            start_latitude=start_position[0],
            start_longitude=start_position[1]
        ).where(Player.player_id == player_id).execute()
コード例 #17
0
def main():

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service,
                     config.username,
                     config.password,
                     app_simulation=True):
        return

    # get player profile + inventory call (thread-safe/chaining example)
    # ----------------------
    req = api.create_request()
    req.get_player()
    req.get_inventory()
    response_dict = req.call()
    print json.dumps(response_dict)
コード例 #18
0
ファイル: scan.py プロジェクト: benkauffman/pogom
    def update_scan_locations(self, scan_locations):
        location_names = set([])
        # Add new locations
        for scan_location in scan_locations:
            if scan_location['location'] not in self.SCAN_LOCATIONS:
                if ('latitude' not in scan_location or
                        'longitude' not in scan_location or
                        'altitude' not in scan_location):
                    lat, lng, alt = get_pos_by_name(scan_location['location'])
                    log.info('Parsed location is: {:.4f}/{:.4f}/{:.4f} '
                             '(lat/lng/alt)'.format(lat, lng, alt))
                    scan_location['latitude'] = lat
                    scan_location['longitude'] = lng
                    scan_location['altitude'] = alt
                self.SCAN_LOCATIONS[scan_location['location']] = scan_location
            location_names.add(scan_location['location'])

        # Remove old locations
        for location_name in self.SCAN_LOCATIONS:
            if location_name not in location_names:
                del self.SCAN_LOCATIONS[location_name]

        self._update_cover()
コード例 #19
0
    def update_scan_locations(self, scan_locations):
        location_names = set([])
        # Add new locations
        for scan_location in scan_locations:
            if scan_location['location'] not in self.SCAN_LOCATIONS:
                if ('latitude' not in scan_location
                        or 'longitude' not in scan_location
                        or 'altitude' not in scan_location):
                    lat, lng, alt = get_pos_by_name(scan_location['location'])
                    log.info('Parsed location is: {:.4f}/{:.4f}/{:.4f} '
                             '(lat/lng/alt)'.format(lat, lng, alt))
                    scan_location['latitude'] = lat
                    scan_location['longitude'] = lng
                    scan_location['altitude'] = alt
                self.SCAN_LOCATIONS[scan_location['location']] = scan_location
            location_names.add(scan_location['location'])

        # Remove old locations
        for location_name in self.SCAN_LOCATIONS:
            if location_name not in location_names:
                del self.SCAN_LOCATIONS[location_name]

        self._update_cover()
コード例 #20
0
def main():

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)


    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service, config.username, config.password, app_simulation = True):
        return

    # get player profile + inventory call (thread-safe/chaining example)
    # ----------------------
    req = api.create_request()
    req.get_player()
    req.get_inventory()
    response_dict = req.call()
    print json.dumps(response_dict)
コード例 #21
0
ファイル: runserver.py プロジェクト: korejwo/michales
def main():
    # Check if we have the proper encryption library file and get its path
    encryption_lib_path = get_encryption_lib_path()
    if encryption_lib_path is "":
        sys.exit(1)

    args = get_args()

    if args.debug:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # These are very noisey, let's shush them up a bit
    logging.getLogger('peewee').setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.pgoapi').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.rpc_api').setLevel(logging.INFO)
    logging.getLogger('werkzeug').setLevel(logging.ERROR)

    config['parse_pokemon'] = not args.no_pokemon
    config['parse_pokestops'] = not args.no_pokestops
    config['parse_gyms'] = not args.no_gyms

    # Turn these back up if debugging
    if args.debug:
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)

    # use lat/lng directly if matches such a pattern
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    if not any(position):
        log.error('Could not get a position by name, aborting')
        sys.exit()

    log.info('Parsed location is: %.4f/%.4f/%.4f (lat/lng/alt)',
             position[0], position[1], position[2])

    if args.no_pokemon:
        log.info('Parsing of Pokemon disabled')
    if args.no_pokestops:
        log.info('Parsing of Pokestops disabled')
    if args.no_gyms:
        log.info('Parsing of Gyms disabled')

    config['LOCALE'] = args.locale
    config['CHINA'] = args.china

    app = Pogom(__name__)
    db = init_database(app)
    if args.clear_db:
        log.info('Clearing database')
        if args.db_type == 'mysql':
            drop_tables(db)
        elif os.path.isfile(args.db):
            os.remove(args.db)
    create_tables(db)

    app.set_current_location(position)

    # Control the search status (running or not) across threads
    pause_bit = Event()
    pause_bit.clear()

    # Setup the location tracking queue and push the first location on
    new_location_queue = Queue()
    new_location_queue.put(position)

    if not args.only_server:
        # Gather the pokemons!
        if not args.mock:
            # check the sort of scan
            if not args.spawnpoint_scanning:
                log.debug('Starting a real search thread')
                search_thread = Thread(target=search_overseer_thread, args=(args, new_location_queue, pause_bit, encryption_lib_path))
            # using -ss
            else:
                if args.dump_spawnpoints:
                    with open(args.spawnpoint_scanning, 'w+') as file:
                        log.info('exporting spawns')
                        spawns = Pokemon.get_spawnpoints_in_hex(position, args.step_limit)
                        file.write(json.dumps(spawns))
                        file.close()
                        log.info('Finished exporting spawns')
                # start the scan sceduler
                search_thread = Thread(target=search_overseer_thread_ss, args=(args, new_location_queue, pause_bit, encryption_lib_path))
        else:
            log.debug('Starting a fake search thread')
            insert_mock_data(position)
            search_thread = Thread(target=fake_search_loop)

        search_thread.daemon = True
        search_thread.name = 'search_thread'
        search_thread.start()

    if args.cors:
        CORS(app)

    # No more stale JS
    init_cache_busting(app)

    app.set_search_control(pause_bit)
    app.set_location_queue(new_location_queue)

    config['ROOT_PATH'] = app.root_path
    config['GMAPS_KEY'] = args.gmaps_key

    if args.no_server:
        # This loop allows for ctrl-c interupts to work since flask won't be holding the program open
        while search_thread.is_alive():
            time.sleep(60)
    else:
        ssl_context = None
        if args.ssl_certificate and args.ssl_privatekey \
                and os.path.exists(args.ssl_certificate) and os.path.exists(args.ssl_privatekey):
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_context.load_cert_chain(args.ssl_certificate, args.ssl_privatekey)
            log.info('Web server in SSL mode.')

        app.run(threaded=True, use_reloader=False, debug=args.debug, host=args.host, port=args.port, ssl_context=ssl_context)
コード例 #22
0
ファイル: gymclient.py プロジェクト: DraykeC/PoGOGymMembers
def main():
    # log settings
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    data_path = os.path.join(os.path.dirname(__file__), "data")
    gyms_path = os.path.join(data_path, "gyms")
    if not os.path.exists(data_path):
        os.makedirs(data_path)
    if not os.path.exists(gyms_path):
        os.makedirs(gyms_path)

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    #if not api.login(config.auth_service, config.username, config.password, app_simulation = True):
    #    return
        
    # new authentication initialitation
    api.set_authentication(provider = config.auth_service, username = config.username, password =  config.password)

    # provide the path for your encrypt dll
    encryptPath = get_encryption_lib_path()
    api.activate_signature(encryptPath)

    # get player profile call (single command example)
    # ----------------------
    #response_dict = api.get_player()
    #print('Response dictionary (get_player): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
    
    # sleep 200ms due to server-side throttling
    #time.sleep(0.2)

    # get player profile + inventory call (thread-safe/chaining example)
    # ----------------------
    #req = api.create_request()
    #req.get_player()
    #req.get_inventory()
    #response_dict = req.call()
    #print('Response dictionary (get_player + get_inventory): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
	
    # sleep 200ms due to server-side throttling
    #time.sleep(0.2)
	
    #print('cellids:\n\r{}'.format(util.get_cell_ids(position[0],position[1])))
    #get gym info
    lat= position[0]
    lng= position[1]
    cell_ids = util.get_cell_ids(lat, lng)
    timestamps = [0,] * len(cell_ids)
    response_dict = api.get_map_objects(latitude = util.f2i(lat), longitude = util.f2i(lng), since_timestamp_ms = timestamps, cell_id = cell_ids)

    response_json = os.path.join(data_path, "response_dict.json")
    with open(response_json, 'w') as outfile:
        outfile.truncate()
        json.dump(response_dict, outfile)
    
    
    map_objects = response_dict.get('responses', {}).get('GET_MAP_OBJECTS', {})
    status = map_objects.get('status', None)
    cells = map_objects['map_cells']

    time.sleep(2);
    
    #insert detail info about gym to fort
    for cell in cells:
        if 'forts' in cell:
            for fort in cell['forts']:
                print ('id {} type {} points {}'.format(fort.get('id'),fort.get('type'),fort.get('gym_points')))
                #if fort.get('type') != 1:
                if 'gym_points' in fort:
                    req = api.create_request()
                    req.get_gym_details(gym_id=fort.get('id'),
                                             player_latitude=lng,
                                             player_longitude=lat,
                                             gym_latitude=fort.get('latitude'),
                                             gym_longitude=fort.get('longitude'))
                    response_gym_details = req.call()
                    fort['gym_details'] = response_gym_details.get('responses', {}).get('GET_GYM_DETAILS', None)
                    if ('name' in fort['gym_details']):
                        gym_data_cells = os.path.join(gyms_path, "gym_{}.json".format(fort['id']))
                        with open(gym_data_cells, 'w') as outfile:
                            json.dump(fort['gym_details'], outfile)
                    else:
                        print('***NO GYM DETAILS - HANDLE WHY?');
                        print('{}'.format(pprint.PrettyPrinter(indent=1).pformat(fort['gym_details'])));
                        print('{}'.format(pprint.PrettyPrinter(indent=1).pformat(fort)));
                        print('***NO GYM DETAILS - HANDLE WHY?');
                    time.sleep(2);
    user_data_cells = os.path.join(data_path, "cells.json")
    with open(user_data_cells, 'w') as outfile:
        outfile.truncate()
        json.dump(cells, outfile)
コード例 #23
0
    def fullmap(self):
        args = get_args()
        fixed_display = "none" if args.fixed_location else "inline"
        search_display = "inline" if args.search_control else "none"

        llat = Vars.get("TempLat")
        llon = Vars.get("TempLon")
        if not llat:
            llat = self.current_location[0]
            llon = self.current_location[1]
        if request.args.get('lat'):
            llat = request.args.get('lat')
            llon = request.args.get('lon')
        if request.args.get('loc'):
            loc = request.args.get('loc')
            if re.match("[0-9abcdef]{28,35}\.1", loc):
                ll = Pokestop.get_stop(loc)
                loc = "%f,%f" % (ll['latitude'], ll['longitude'])
            if not len(re.sub("[0-9\.\-]+", "", loc)):
                loc = re.sub("([0-9\.]+)([0-9\.\-])", "\g<1>,\g<2>",
                             request.args.get('loc'), 1)
            tcords = util.get_pos_by_name(loc)
            llat = tcords[0]
            llon = tcords[1]
        if request.args.get('setmyloc'):
            loc = request.args.get('setmyloc')
            if re.match("[0-9abcdef]{28,35}\.1", loc):
                ll = Pokestop.get_stop(loc)
                loc = "%f,%f" % (ll['latitude'], ll['longitude'])
            if not len(re.sub("[0-9\.\-]+", "", loc)):
                loc = re.sub("([0-9\.]+)([0-9\.\-])", "\g<1>,\g<2>",
                             request.args.get('loc'), 1)
            tcords = util.get_pos_by_name(loc)
            llat = tcords[0]
            llon = tcords[1]
            Vars.set("TempLat", llat)
            Vars.set("TempLon", llon)
        if request.args.get('center'):
            loc = request.args.get('center')
            if re.match("[0-9abcdef]{28,35}\.1", loc):
                ll = Pokestop.get_stop(loc)
                loc = "%f,%f" % (ll['latitude'], ll['longitude'])
            if not len(re.sub("[0-9\.\-]+", "", loc)):
                loc = re.sub("([0-9\.]+)([0-9\.\-])", "\g<1>,\g<2>",
                             request.args.get('loc'), 1)
            tcords = util.get_pos_by_name(loc)
            llat = tcords[0]
            llon = tcords[1]

        z = request.args.get('zoom')
        zoom = 16
        if z:
            zoom = z

        return render_template('map.html',
                               lat=llat,
                               lng=llon,
                               gmaps_key=config['GMAPS_KEY'],
                               lang=config['LOCALE'],
                               z=zoom,
                               is_fixed=fixed_display,
                               search_control=search_display)
コード例 #24
0
def get_all_player_data(config, custom_location=None, allow_debug=True):
    # log settings
    if allow_debug:
        # log format
        logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)11s] [%(levelname)5s] %(message)s')
        # log level for http request class
        logging.getLogger("requests").setLevel(logging.WARNING)
        # log level for main pgoapi class
        logging.getLogger("pgoapi").setLevel(logging.INFO)
        # log level for internal pgoapi class
        logging.getLogger("rpc_api").setLevel(logging.INFO)

    if custom_location:
        config.location = custom_location

    # debug mode
    if allow_debug and config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    # get current timestamp
    now = str(int(time.time()))

    # get root dir
    app_root_dir = os.path.dirname(os.path.realpath(__file__))

    # declare output files location
    web_inventory_user = os.path.join(app_root_dir, 'web/playerdata/inventory-%s.json' % config.username)
    web_player_user = os.path.join(app_root_dir, 'web/playerdata/player-%s.json' % config.username)
    web_settings_user = os.path.join(app_root_dir, 'web/playerdata/settings-%s.json' % config.username)

    # declare output request log
    web_log_api = os.path.join(app_root_dir, 'web/playerdata/api-request.' + now + '.log')

    # instantiate pgoapi
    api = pgoapi.PGoApi()
    if config.proxy:
        api.set_proxy({'http': config.proxy, 'https': config.proxy})

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position and allow_debug:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    # new authentication initialisation
    if config.proxy:
        api.set_authentication(provider=config.auth_service, username=config.username, password=config.password,
                               proxy_config={'http': config.proxy, 'https': config.proxy})
    else:
        api.set_authentication(provider=config.auth_service, username=config.username, password=config.password)

    # provide the path for your encrypt dll, see http://pgoapi.com/
    if os.path.isfile('libencrypt.dll'):
        api.activate_signature("libencrypt.dll")
    elif os.path.isfile('libencrypt.so'):
        api.activate_signature("libencrypt.so")

    # create thread-safe request
    # ----------------------
    req = api.create_request()

    # get player profile call
    # ----------------------
    req.get_player()

    # get inventory call
    # ----------------------
    req.get_inventory()

    # get download setting call
    # ----------------------
    req.download_settings()

    # execute the RPC call after 2 seconds
    time.sleep(2)
    response_dict = req.call()

    # backup latest output files
    if os.path.isfile(web_inventory_user):
        copyfile(web_inventory_user, web_inventory_user + '.' + now)
    if os.path.isfile(web_player_user):
        copyfile(web_player_user, web_player_user + '.' + now)
    if os.path.isfile(web_settings_user):
        copyfile(web_settings_user, web_settings_user + '.' + now)

    # write the output inventory file
    inventory_dict = response_dict['responses']['GET_INVENTORY']['inventory_delta']
    with open(web_inventory_user, 'w') as output_file:
        json.dump(inventory_dict, output_file, indent=2, cls=util.JSONByteEncoder)

    # write the output player file
    player_dict = response_dict['responses']['GET_PLAYER']['player_data']
    with open(web_player_user, 'w') as output_file:
        json.dump(player_dict, output_file, indent=2, cls=util.JSONByteEncoder)

    # write the output setting file
    setting_dict = response_dict['responses']['DOWNLOAD_SETTINGS']['settings']
    with open(web_settings_user, 'w') as output_file:
        json.dump(setting_dict, output_file, indent=2, cls=util.JSONByteEncoder)

    # log the request
    config.password = '******'
    with open(web_log_api, 'w') as output_file:
        output_file.write(repr(config))
コード例 #25
0
ファイル: runserver.py プロジェクト: crandrade/PokemonGo-Map
def main():
    # Check if we have the proper encryption library file and get its path
    encryption_lib_path = get_encryption_lib_path()
    if encryption_lib_path is "":
        sys.exit(1)

    args = get_args()

    if args.debug:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # Let's not forget to run Grunt / Only needed when running with webserver
    if not args.no_server:
        if not os.path.exists(
                os.path.join(os.path.dirname(__file__), 'static/dist')):
            log.critical(
                'Missing front-end assets (static/dist) -- please run "npm install && npm run build" before starting the server'
            )
            sys.exit()

    # These are very noisey, let's shush them up a bit
    logging.getLogger('peewee').setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.pgoapi').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.rpc_api').setLevel(logging.INFO)
    logging.getLogger('werkzeug').setLevel(logging.ERROR)

    config['parse_pokemon'] = not args.no_pokemon
    config['parse_pokestops'] = not args.no_pokestops
    config['parse_gyms'] = not args.no_gyms

    # Turn these back up if debugging
    if args.debug:
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)

    # use lat/lng directly if matches such a pattern
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    # Use the latitude and longitude to get the local altitude from Google
    try:
        url = 'https://maps.googleapis.com/maps/api/elevation/json?locations={},{}'.format(
            str(position[0]), str(position[1]))
        altitude = requests.get(url).json()[u'results'][0][u'elevation']
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    except (requests.exceptions.RequestException, IndexError, KeyError):
        log.error('Unable to retrieve altitude from Google APIs; setting to 0')

    if not any(position):
        log.error('Could not get a position by name, aborting')
        sys.exit()

    log.info('Parsed location is: %.4f/%.4f/%.4f (lat/lng/alt)', position[0],
             position[1], position[2])

    if args.no_pokemon:
        log.info('Parsing of Pokemon disabled')
    if args.no_pokestops:
        log.info('Parsing of Pokestops disabled')
    if args.no_gyms:
        log.info('Parsing of Gyms disabled')

    config['LOCALE'] = args.locale
    config['CHINA'] = args.china

    app = Pogom(__name__)
    db = init_database(app)
    if args.clear_db:
        log.info('Clearing database')
        if args.db_type == 'mysql':
            drop_tables(db)
        elif os.path.isfile(args.db):
            os.remove(args.db)
    create_tables(db)

    app.set_current_location(position)

    # Control the search status (running or not) across threads
    pause_bit = Event()
    pause_bit.clear()

    # Setup the location tracking queue and push the first location on
    new_location_queue = Queue()
    new_location_queue.put(position)

    # DB Updates
    db_updates_queue = Queue()

    # Thread(s) to process database updates
    for i in range(args.db_threads):
        log.debug('Starting db-updater worker thread %d', i)
        t = Thread(target=db_updater,
                   name='db-updater-{}'.format(i),
                   args=(args, db_updates_queue))
        t.daemon = True
        t.start()

    # db clearner; really only need one ever
    t = Thread(target=clean_db_loop, name='db-cleaner', args=(args, ))
    t.daemon = True
    t.start()

    # WH Updates
    wh_updates_queue = Queue()

    # Thread to process webhook updates
    for i in range(args.wh_threads):
        log.debug('Starting wh-updater worker thread %d', i)
        t = Thread(target=wh_updater,
                   name='wh-updater-{}'.format(i),
                   args=(args, wh_updates_queue))
        t.daemon = True
        t.start()

    if not args.only_server:
        # Gather the pokemons!
        argset = (args, new_location_queue, pause_bit, encryption_lib_path,
                  db_updates_queue, wh_updates_queue)
        # check the sort of scan
        if not args.spawnpoint_scanning:
            log.debug('Starting a hex search thread')
            search_thread = Thread(target=search_overseer_thread, args=argset)
        # using -ss
        else:
            log.debug('Starting a sp search thread')
            if args.dump_spawnpoints:
                with open(args.spawnpoint_scanning, 'w+') as file:
                    log.info('Exporting spawns')
                    spawns = Pokemon.get_spawnpoints_in_hex(
                        position, args.step_limit)
                    file.write(json.dumps(spawns))
                    file.close()
                    log.info('Finished exporting spawns')
            # start the scan sceduler
            search_thread = Thread(target=search_overseer_thread_ss,
                                   args=argset)

        search_thread.daemon = True
        search_thread.name = 'search-overseer'
        search_thread.start()

    if args.cors:
        CORS(app)

    # No more stale JS
    init_cache_busting(app)

    app.set_search_control(pause_bit)
    app.set_location_queue(new_location_queue)

    config['ROOT_PATH'] = app.root_path
    config['GMAPS_KEY'] = args.gmaps_key

    if args.no_server:
        # This loop allows for ctrl-c interupts to work since flask won't be holding the program open
        while search_thread.is_alive():
            time.sleep(60)
    else:
        ssl_context = None
        if args.ssl_certificate and args.ssl_privatekey \
                and os.path.exists(args.ssl_certificate) and os.path.exists(args.ssl_privatekey):
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_context.load_cert_chain(args.ssl_certificate,
                                        args.ssl_privatekey)
            log.info('Web server in SSL mode.')

        app.run(threaded=True,
                use_reloader=False,
                debug=args.debug,
                host=args.host,
                port=args.port,
                ssl_context=ssl_context)
コード例 #26
0
ファイル: runserver.py プロジェクト: p0psicles/PokemonGo-Map
def main():
    args = get_args()

    # Check for depreciated argumented
    if args.debug:
        log.warning(
            '--debug is depreciated. Please use --verbose instead.  Enabling --verbose'
        )
        args.verbose = 'nofile'

    # Add file logging if enabled
    if args.verbose and args.verbose != 'nofile':
        filelog = logging.FileHandler(args.verbose)
        filelog.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(threadName)16s][%(module)14s][%(levelname)8s] %(message)s'
            ))
        logging.getLogger('').addHandler(filelog)
    if args.very_verbose and args.very_verbose != 'nofile':
        filelog = logging.FileHandler(args.very_verbose)
        filelog.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(threadName)16s][%(module)14s][%(levelname)8s] %(message)s'
            ))
        logging.getLogger('').addHandler(filelog)

    # Check if we have the proper encryption library file and get its path
    encryption_lib_path = get_encryption_lib_path(args)
    if encryption_lib_path is "":
        sys.exit(1)

    if args.verbose or args.very_verbose:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # Let's not forget to run Grunt / Only needed when running with webserver
    if not args.no_server:
        if not os.path.exists(
                os.path.join(os.path.dirname(__file__), 'static/dist')):
            log.critical(
                'Missing front-end assets (static/dist) -- please run "npm install && npm run build" before starting the server'
            )
            sys.exit()

    # These are very noisey, let's shush them up a bit
    logging.getLogger('peewee').setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.pgoapi').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.rpc_api').setLevel(logging.INFO)
    logging.getLogger('werkzeug').setLevel(logging.ERROR)

    config['parse_pokemon'] = not args.no_pokemon
    config['parse_pokestops'] = not args.no_pokestops
    config['parse_gyms'] = not args.no_gyms

    # Turn these back up if debugging
    if args.verbose or args.very_verbose:
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
    if args.very_verbose:
        logging.getLogger('peewee').setLevel(logging.DEBUG)
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi.pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi.rpc_api').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)
        logging.getLogger('werkzeug').setLevel(logging.DEBUG)

    # use lat/lng directly if matches such a pattern
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    # Use the latitude and longitude to get the local altitude from Google
    try:
        url = 'https://maps.googleapis.com/maps/api/elevation/json?locations={},{}'.format(
            str(position[0]), str(position[1]))
        altitude = requests.get(url).json()[u'results'][0][u'elevation']
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    except (requests.exceptions.RequestException, IndexError, KeyError):
        log.error('Unable to retrieve altitude from Google APIs; setting to 0')

    if not any(position):
        log.error('Could not get a position by name, aborting')
        sys.exit()

    log.info('Parsed location is: %.4f/%.4f/%.4f (lat/lng/alt)', position[0],
             position[1], position[2])

    if args.no_pokemon:
        log.info('Parsing of Pokemon disabled')
    if args.no_pokestops:
        log.info('Parsing of Pokestops disabled')
    if args.no_gyms:
        log.info('Parsing of Gyms disabled')

    config['LOCALE'] = args.locale
    config['CHINA'] = args.china

    app = Pogom(__name__)
    db = init_database(app)
    if args.clear_db:
        log.info('Clearing database')
        if args.db_type == 'mysql':
            drop_tables(db)
        elif os.path.isfile(args.db):
            os.remove(args.db)
    create_tables(db)

    app.set_current_location(position)

    # Control the search status (running or not) across threads
    pause_bit = Event()
    pause_bit.clear()

    # Setup the location tracking queue and push the first location on
    new_location_queue = Queue()
    new_location_queue.put(position)

    # DB Updates
    db_updates_queue = Queue()

    # Thread(s) to process database updates
    for i in range(args.db_threads):
        log.debug('Starting db-updater worker thread %d', i)
        t = Thread(target=db_updater,
                   name='db-updater-{}'.format(i),
                   args=(args, db_updates_queue))
        t.daemon = True
        t.start()

    # db clearner; really only need one ever
    t = Thread(target=clean_db_loop, name='db-cleaner', args=(args, ))
    t.daemon = True
    t.start()

    # WH Updates
    wh_updates_queue = Queue()

    # Thread to process webhook updates
    for i in range(args.wh_threads):
        log.debug('Starting wh-updater worker thread %d', i)
        t = Thread(target=wh_updater,
                   name='wh-updater-{}'.format(i),
                   args=(args, wh_updates_queue))
        t.daemon = True
        t.start()

    # WH Updates
    catch_pokemon_queue = Queue()

    # Attach queue to Flask App
    app.catch_pokemon_queue = catch_pokemon_queue

    # Setup the message object
    message_obj = LogMessages()
    app.log_message_object = message_obj

    # Thread to process catch_pokemon requests
    log.debug('Starting catch_pokemon worker thread')
    t = Thread(target=catch_pokemon_worker,
               name='catch-pokemon',
               args=(args, catch_pokemon_queue, message_obj,
                     encryption_lib_path))
    t.daemon = True
    t.start()

    if not args.only_server:
        # Gather the pokemons!

        # check the sort of scan
        if args.spawnpoint_scanning:
            mode = 'sps'
        else:
            mode = 'hex'

        # attempt to dump the spawn points (do this before starting threads of endure the woe)
        if args.spawnpoint_scanning and args.spawnpoint_scanning != 'nofile' and args.dump_spawnpoints:
            with open(args.spawnpoint_scanning, 'w+') as file:
                log.info('Saving spawn points to %s', args.spawnpoint_scanning)
                spawns = Pokemon.get_spawnpoints_in_hex(
                    position, args.step_limit)
                file.write(json.dumps(spawns))
                log.info('Finished exporting spawn points')

        argset = (args, mode, new_location_queue, pause_bit,
                  encryption_lib_path, db_updates_queue, wh_updates_queue)

        log.debug('Starting a %s search thread', mode)
        search_thread = Thread(target=search_overseer_thread,
                               name='search-overseer',
                               args=argset)
        search_thread.daemon = True
        search_thread.start()

    if args.cors:
        CORS(app)

    # No more stale JS
    init_cache_busting(app)

    app.set_search_control(pause_bit)
    app.set_location_queue(new_location_queue)

    # Add login
    # set the secret key.  keep this really secret:
    app.secret_key = 'DuEkd*7d83Df3@#$)9fkD(8Fd(&d^ @#4kllz3'

    # Prepare the LoginManager
    login_manager = LoginManager()
    login_manager.init_app(app)

    @login_manager.user_loader
    def load_user(uid):
        return User.get_user(uid)

    login_manager.login_view = 'login'

    config['ROOT_PATH'] = app.root_path
    config['GMAPS_KEY'] = args.gmaps_key

    if args.no_server:
        # This loop allows for ctrl-c interupts to work since gevent won't be holding the program open
        while search_thread.is_alive():
            time.sleep(60)
    else:
        # run gevent server
        gevent_log = None
        if args.verbose or args.very_verbose:
            gevent_log = log
        if args.ssl_certificate and args.ssl_privatekey \
                and os.path.exists(args.ssl_certificate) and os.path.exists(args.ssl_privatekey):
            http_server = pywsgi.WSGIServer((args.host, args.port),
                                            app,
                                            log=gevent_log,
                                            error_log=log,
                                            keyfile=args.ssl_privatekey,
                                            certfile=args.ssl_certificate,
                                            ssl_version=ssl.PROTOCOL_TLSv1_2)
            log.info('Web server in SSL mode, listening at https://%s:%d',
                     args.host, args.port)
        else:
            http_server = pywsgi.WSGIServer((args.host, args.port),
                                            app,
                                            log=gevent_log,
                                            error_log=log)
            log.info('Web server listening at http://%s:%d', args.host,
                     args.port)
        # run it
        try:
            http_server.serve_forever()
        except KeyboardInterrupt:
            pass
コード例 #27
0
def main():
    # Patch threading to make exceptions catchable.
    install_thread_excepthook()

    # Make sure exceptions get logged.
    sys.excepthook = handle_exception

    args = get_args()

    # Add file logging if enabled.
    if args.verbose and args.verbose != 'nofile':
        filelog = logging.FileHandler(args.verbose)
        filelog.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(threadName)16s][%(module)14s][%(levelname)8s] '
                + '%(message)s'))
        logging.getLogger('').addHandler(filelog)
    if args.very_verbose and args.very_verbose != 'nofile':
        filelog = logging.FileHandler(args.very_verbose)
        filelog.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(threadName)16s][%(module)14s][%(levelname)8s] '
                + '%(message)s'))
        logging.getLogger('').addHandler(filelog)

    if args.verbose or args.very_verbose:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # Let's not forget to run Grunt / Only needed when running with webserver.
    if not args.no_server:
        if not os.path.exists(
                os.path.join(os.path.dirname(__file__), 'static/dist')):
            log.critical(
                'Missing front-end assets (static/dist) -- please run ' +
                '"npm install && npm run build" before starting the server.')
            sys.exit()

        # You need custom image files now.
        if not os.path.isfile(
                os.path.join(os.path.dirname(__file__),
                             'static/icons-sprite.png')):
            log.info('Sprite files not present, extracting bundled ones...')
            extract_sprites()
            log.info('Done!')

    # Beehive configuration
    beehive_workers = [args.workers]
    if args.beehive > 0:
        beehive_size = 1
        # Calculate number of hives required ( -bh 2 => i:1, i:2 )
        for i in range(1, args.beehive + 1):
            beehive_size += i * 6

        # Initialize worker distribution list
        beehive_workers = [0 for x in range(beehive_size)]
        skip_indexes = []
        hives_ignored = 0
        workers_forced = 0
        log.debug('-bhw --beehive-workers: %s', args.beehive_workers)

        # Parse beehive configuration
        for i in range(0, len(args.beehive_workers)):
            bhw = args.beehive_workers[i].split(':')
            bhw_index = int(bhw[0])
            bhw_workers = int(bhw[1])
            if (bhw_index >= 0) and (bhw_index < beehive_size):
                if bhw_index in skip_indexes:
                    log.warning(
                        'Duplicate hive index found in -bhw ' +
                        '--beehive-workers: %d', bhw_index)
                    continue
                if bhw_workers <= 0:
                    skip_indexes.append(bhw_index)
                    beehive_workers[bhw_index] = 0
                    hives_ignored += 1
                else:
                    skip_indexes.append(bhw_index)
                    beehive_workers[bhw_index] = bhw_workers
                    workers_forced += bhw_workers
            else:
                log.warning(
                    'Invalid hive index found in -bhw ' +
                    '--beehive-workers: %d', bhw_index)
        # Check if we have enough workers for beehive setup.
        workers_required = workers_forced
        if args.workers_per_hive > 0:
            count = beehive_size - len(skip_indexes)
            workers_required += count * args.workers_per_hive

        log.info(
            'Beehive size: %d (%d hives ignored). Workers forced: ' +
            '%d. Workers required: %d', beehive_size, hives_ignored,
            workers_forced, workers_required)
        if args.workers < workers_required:
            log.critical('Not enough workers to fill the beehive. ' +
                         'Increase -w --workers, decrease -bh --beehive ' +
                         'or decrease -wph --workers-per-hive')
            sys.exit()

        # Assign remaining workers to available hives.
        remaining_workers = args.workers - workers_forced
        populate_index = 0
        while remaining_workers > 0:
            beehive_index = populate_index % beehive_size
            if beehive_index in skip_indexes:
                populate_index += 1
                continue

            beehive_workers[beehive_index] += 1
            populate_index += 1
            remaining_workers -= 1
        log.debug('Beehive worker distribution: %s', beehive_workers)

    # These are very noisy, let's shush them up a bit.
    logging.getLogger('peewee').setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.pgoapi').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.rpc_api').setLevel(logging.INFO)
    logging.getLogger('werkzeug').setLevel(logging.ERROR)

    config['parse_pokemon'] = not args.no_pokemon
    config['parse_pokestops'] = not args.no_pokestops
    config['parse_gyms'] = not args.no_gyms

    # Turn these back up if debugging.
    if args.verbose or args.very_verbose:
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
    if args.very_verbose:
        logging.getLogger('peewee').setLevel(logging.DEBUG)
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi.pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi.rpc_api').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)
        logging.getLogger('werkzeug').setLevel(logging.DEBUG)

    # Web access logs.
    if args.access_logs:
        logger = logging.getLogger('werkzeug')
        handler = logging.FileHandler('access.log')
        logger.setLevel(logging.INFO)
        logger.addHandler(handler)

    # Use lat/lng directly if matches such a pattern.
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    if position is None or not any(position):
        log.error("Location not found: '{}'".format(args.location))
        sys.exit()

    # Use the latitude and longitude to get the local altitude from Google.
    (altitude, status) = get_gmaps_altitude(position[0], position[1],
                                            args.gmaps_key)
    if altitude is not None:
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    else:
        if status == 'REQUEST_DENIED':
            log.error(
                'Google API Elevation request was denied. You probably ' +
                'forgot to enable elevation api in https://console.' +
                'developers.google.com/apis/api/elevation_backend/')
            sys.exit()
        else:
            log.error('Unable to retrieve altitude from Google APIs' +
                      'setting to 0')

    log.info('Parsed location is: %.4f/%.4f/%.4f (lat/lng/alt)', position[0],
             position[1], position[2])

    if args.no_pokemon:
        log.info('Parsing of Pokemon disabled.')
    if args.no_pokestops:
        log.info('Parsing of Pokestops disabled.')
    if args.no_gyms:
        log.info('Parsing of Gyms disabled.')
    if args.encounter:
        log.info('Encountering pokemon enabled.')

    config['LOCALE'] = args.locale
    config['CHINA'] = args.china

    # if we're clearing the db, do not bother with the blacklist
    if args.clear_db:
        args.disable_blacklist = True
    app = Pogom(__name__)
    app.before_request(app.validate_request)

    db = init_database(app)
    if args.clear_db:
        log.info('Clearing database')
        if args.db_type == 'mysql':
            drop_tables(db)
        elif os.path.isfile(args.db):
            os.remove(args.db)
    create_tables(db)
    if args.clear_db:
        log.info("Drop and recreate is complete. Now remove -cd and restart.")
        sys.exit()

    app.set_current_location(position)

    # Control the search status (running or not) across threads.
    pause_bit = Event()
    pause_bit.clear()
    if args.on_demand_timeout > 0:
        pause_bit.set()

    heartbeat = [now()]

    # Setup the location tracking queue and push the first location on.
    new_location_queue = Queue()
    new_location_queue.put(position)

    # DB Updates
    db_updates_queue = Queue()
    app.set_db_updates_queue(db_updates_queue)

    # Thread(s) to process database updates.
    for i in range(args.db_threads):
        log.debug('Starting db-updater worker thread %d', i)
        t = Thread(target=db_updater,
                   name='db-updater-{}'.format(i),
                   args=(args, db_updates_queue, db))
        t.daemon = True
        t.start()

    # db cleaner; really only need one ever.
    if not args.disable_clean:
        t = Thread(target=clean_db_loop, name='db-cleaner', args=(args, ))
        t.daemon = True
        t.start()

    # WH updates queue & WH unique key LFU caches.
    # The LFU caches will stop the server from resending the same data an
    # infinite number of times. The caches will be instantiated in the
    # webhook's startup code.
    wh_updates_queue = Queue()
    wh_key_cache = {}
    app.set_wh_updates_queue(wh_updates_queue)

    # Thread to process webhook updates.
    for i in range(args.wh_threads):
        log.debug('Starting wh-updater worker thread %d', i)
        t = Thread(target=wh_updater,
                   name='wh-updater-{}'.format(i),
                   args=(args, wh_updates_queue, wh_key_cache))
        t.daemon = True
        t.start()

    if not args.only_server:

        # Abort if we don't have a hash key set
        if not args.hash_key:
            log.critical('Hash key is required for scanning. Exiting.')
            sys.exit()

        # Processing proxies if set (load from file, check and overwrite old
        # args.proxy with new working list)
        args.proxy = check_proxies(args)

        # Run periodical proxy refresh thread
        if (args.proxy_file is not None) and (args.proxy_refresh > 0):
            t = Thread(target=proxies_refresher,
                       name='proxy-refresh',
                       args=(args, ))
            t.daemon = True
            t.start()
        else:
            log.info('Periodical proxies refresh disabled.')

        # Gather the Pokemon!

        # Attempt to dump the spawn points (do this before starting threads of
        # endure the woe).
        if (args.spawnpoint_scanning and args.spawnpoint_scanning != 'nofile'
                and args.dump_spawnpoints):
            with open(args.spawnpoint_scanning, 'w+') as file:
                log.info('Saving spawn points to %s', args.spawnpoint_scanning)
                spawns = Pokemon.get_spawnpoints_in_hex(
                    position, args.step_limit)
                file.write(json.dumps(spawns))
                log.info('Finished exporting spawn points')

        argset = (args, beehive_workers, new_location_queue, pause_bit,
                  heartbeat, db_updates_queue, wh_updates_queue)

        log.debug('Starting a %s search thread', args.scheduler)
        search_thread = Thread(target=search_overseer_thread,
                               name='search-overseer',
                               args=argset)
        search_thread.daemon = True
        search_thread.start()

    if args.cors:
        CORS(app)

    # No more stale JS.
    init_cache_busting(app)

    app.set_search_control(pause_bit)
    app.set_heartbeat_control(heartbeat)
    app.set_location_queue(new_location_queue)

    config['ROOT_PATH'] = app.root_path
    config['GMAPS_KEY'] = args.gmaps_key

    if args.no_server:
        # This loop allows for ctrl-c interupts to work since flask won't be
        # holding the program open.
        while search_thread.is_alive():
            time.sleep(60)
    else:
        ssl_context = None
        if (args.ssl_certificate and args.ssl_privatekey
                and os.path.exists(args.ssl_certificate)
                and os.path.exists(args.ssl_privatekey)):
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_context.load_cert_chain(args.ssl_certificate,
                                        args.ssl_privatekey)
            log.info('Web server in SSL mode.')
        if args.verbose or args.very_verbose:
            app.run(threaded=True,
                    use_reloader=False,
                    debug=True,
                    host=args.host,
                    port=args.port,
                    ssl_context=ssl_context)
        else:
            app.run(threaded=True,
                    use_reloader=False,
                    debug=False,
                    host=args.host,
                    port=args.port,
                    ssl_context=ssl_context)
コード例 #28
0
ファイル: runserver.py プロジェクト: butscherFFF/RocketMap
def main():
    # Patch threading to make exceptions catchable.
    install_thread_excepthook()

    # Make sure exceptions get logged.
    sys.excepthook = handle_exception

    args = get_args()

    # Add file logging if enabled.
    if args.verbose and args.verbose != 'nofile':
        filelog = logging.FileHandler(args.verbose)
        filelog.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(threadName)16s][%(module)14s][%(levelname)8s] '
                + '%(message)s'))
        logging.getLogger('').addHandler(filelog)
    if args.very_verbose and args.very_verbose != 'nofile':
        filelog = logging.FileHandler(args.very_verbose)
        filelog.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(threadName)16s][%(module)14s][%(levelname)8s] '
                + '%(message)s'))
        logging.getLogger('').addHandler(filelog)

    if args.verbose or args.very_verbose:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # Let's not forget to run Grunt / Only needed when running with webserver.
    if not args.no_server:
        if not os.path.exists(
                os.path.join(os.path.dirname(__file__), 'static/dist')):
            log.critical(
                'Missing front-end assets (static/dist) -- please run ' +
                '"npm install && npm run build" before starting the server.')
            sys.exit()

        # You need custom image files now.
        if not os.path.isfile(
                os.path.join(os.path.dirname(__file__),
                             'static/icons-sprite.png')):
            log.critical('Missing sprite files.')
            sys.exit()

    # These are very noisy, let's shush them up a bit.
    logging.getLogger('peewee').setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.pgoapi').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.rpc_api').setLevel(logging.INFO)
    logging.getLogger('werkzeug').setLevel(logging.ERROR)

    config['parse_pokemon'] = not args.no_pokemon
    config['parse_pokestops'] = not args.no_pokestops
    config['parse_gyms'] = not args.no_gyms

    # Turn these back up if debugging.
    if args.verbose or args.very_verbose:
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
    if args.very_verbose:
        logging.getLogger('peewee').setLevel(logging.DEBUG)
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi.pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi.rpc_api').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)
        logging.getLogger('werkzeug').setLevel(logging.DEBUG)

    # Use lat/lng directly if matches such a pattern.
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    if position is None or not any(position):
        log.error("Location not found: '{}'".format(args.location))
        sys.exit()

    # Use the latitude and longitude to get the local altitude from Google.
    (altitude, status) = get_gmaps_altitude(position[0], position[1],
                                            args.gmaps_key)
    if altitude is not None:
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    else:
        if status == 'REQUEST_DENIED':
            log.error(
                'Google API Elevation request was denied. You probably ' +
                'forgot to enable elevation api in https://console.' +
                'developers.google.com/apis/api/elevation_backend/')
            sys.exit()
        else:
            log.error('Unable to retrieve altitude from Google APIs' +
                      'setting to 0')

    log.info('Parsed location is: %.4f/%.4f/%.4f (lat/lng/alt)', position[0],
             position[1], position[2])

    if args.no_pokemon:
        log.info('Parsing of Pokemon disabled.')
    if args.no_pokestops:
        log.info('Parsing of Pokestops disabled.')
    if args.no_gyms:
        log.info('Parsing of Gyms disabled.')
    if args.encounter:
        log.info('Encountering pokemon enabled.')

    config['LOCALE'] = args.locale
    config['CHINA'] = args.china

    app = Pogom(__name__)
    app.before_request(app.validate_request)

    db = init_database(app)
    if args.clear_db:
        log.info('Clearing database')
        if args.db_type == 'mysql':
            drop_tables(db)
        elif os.path.isfile(args.db):
            os.remove(args.db)
    create_tables(db)

    app.set_current_location(position)

    # Control the search status (running or not) across threads.
    pause_bit = Event()
    pause_bit.clear()
    if args.on_demand_timeout > 0:
        pause_bit.set()

    heartbeat = [now()]

    # Setup the location tracking queue and push the first location on.
    new_location_queue = Queue()
    new_location_queue.put(position)

    # DB Updates
    db_updates_queue = Queue()

    # Thread(s) to process database updates.
    for i in range(args.db_threads):
        log.debug('Starting db-updater worker thread %d', i)
        t = Thread(target=db_updater,
                   name='db-updater-{}'.format(i),
                   args=(args, db_updates_queue, db))
        t.daemon = True
        t.start()

    # db cleaner; really only need one ever.
    if not args.disable_clean:
        t = Thread(target=clean_db_loop, name='db-cleaner', args=(args, ))
        t.daemon = True
        t.start()

    # WH updates queue & WH gym/pokéstop unique key LFU cache.
    # The LFU cache will stop the server from resending the same data an
    # infinite number of times.
    # TODO: Rework webhooks entirely so a LFU cache isn't necessary.
    wh_updates_queue = Queue()
    wh_key_cache = LFUCache(maxsize=args.wh_lfu_size)

    # Thread to process webhook updates.
    for i in range(args.wh_threads):
        log.debug('Starting wh-updater worker thread %d', i)
        t = Thread(target=wh_updater,
                   name='wh-updater-{}'.format(i),
                   args=(args, wh_updates_queue, wh_key_cache))
        t.daemon = True
        t.start()

    if not args.only_server:

        # Processing proxies if set (load from file, check and overwrite old
        # args.proxy with new working list)
        args.proxy = check_proxies(args)

        # Run periodical proxy refresh thread
        if (args.proxy_file is not None) and (args.proxy_refresh > 0):
            t = Thread(target=proxies_refresher,
                       name='proxy-refresh',
                       args=(args, ))
            t.daemon = True
            t.start()
        else:
            log.info('Periodical proxies refresh disabled.')

        # Gather the Pokemon!

        # Attempt to dump the spawn points (do this before starting threads of
        # endure the woe).
        if (args.spawnpoint_scanning and args.spawnpoint_scanning != 'nofile'
                and args.dump_spawnpoints):
            with open(args.spawnpoint_scanning, 'w+') as file:
                log.info('Saving spawn points to %s', args.spawnpoint_scanning)
                spawns = Pokemon.get_spawnpoints_in_hex(
                    position, args.step_limit)
                file.write(json.dumps(spawns))
                log.info('Finished exporting spawn points')

        argset = (args, new_location_queue, pause_bit, heartbeat,
                  db_updates_queue, wh_updates_queue)

        log.debug('Starting a %s search thread', args.scheduler)
        search_thread = Thread(target=search_overseer_thread,
                               name='search-overseer',
                               args=argset)
        search_thread.daemon = True
        search_thread.start()

    if args.cors:
        CORS(app)

    # No more stale JS.
    init_cache_busting(app)

    app.set_search_control(pause_bit)
    app.set_heartbeat_control(heartbeat)
    app.set_location_queue(new_location_queue)

    config['ROOT_PATH'] = app.root_path
    config['GMAPS_KEY'] = args.gmaps_key

    if args.no_server:
        # This loop allows for ctrl-c interupts to work since flask won't be
        # holding the program open.
        while search_thread.is_alive():
            time.sleep(60)
    else:
        ssl_context = None
        if (args.ssl_certificate and args.ssl_privatekey
                and os.path.exists(args.ssl_certificate)
                and os.path.exists(args.ssl_privatekey)):
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_context.load_cert_chain(args.ssl_certificate,
                                        args.ssl_privatekey)
            log.info('Web server in SSL mode.')
        if args.verbose or args.very_verbose:
            app.run(threaded=True,
                    use_reloader=False,
                    debug=True,
                    host=args.host,
                    port=args.port,
                    ssl_context=ssl_context)
        else:
            app.run(threaded=True,
                    use_reloader=False,
                    debug=False,
                    host=args.host,
                    port=args.port,
                    ssl_context=ssl_context)
コード例 #29
0
def main():
    # log settings
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    pokemonsJSON = json.load(open('data/pokemon.json'))
    movesJSON = json.load(open('data/moves.json'))
    
    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)


    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return
        
    with open('data/response_dict.json') as data_file:    
        response_dict = json.load(data_file)
        
    map_objects = response_dict.get('responses', {}).get('GET_MAP_OBJECTS', {})
    status = map_objects.get('status', None)
    cells = map_objects['map_cells']

    #insert detail info about gym to fort
    # for cell in cells:
        # if 'forts' in cell:
            # for fort in cell['forts']:
                # if fort.get('type') != 1:
                    # req = api.create_request()
                    # req.get_gym_details(gym_id=fort.get('id'),
                                             # player_latitude=lng,
                                             # player_longitude=lat,
                                             # gym_latitude=fort.get('latitude'),
                                             # gym_longitude=fort.get('longitude'))
                    # response_gym_details = req.call()
                    # fort['gym_details'] = response_gym_details.get('responses', {}).get('GET_GYM_DETAILS', None)
                    # gym_data_cells = "data/forts.json"
                    # with open(gym_data_cells, 'a') as outfile:
                        # json.dump(cell['forts'], outfile)
                        
    # gym_details_loaded = []
    # with open('data/gym_details.json') as data_file:    
        # for line in data_file:
            # gym_details_loaded.append(json.load(line))
    
    # with open('data/gym_details.json', 'r') as data_file:    
        # gym_details_loaded = json.load(data_file)
    gym_details_loaded = []
    for filename in os.listdir("data/gyms/"):
        if 'gym_' in filename:
            with open("data/gyms/{}".format(filename)) as gym_file:
                gym_details_loaded.append(json.load(gym_file))
    
    # if ('name' in fort['gym_details']):
        # gym_data_cells = "data/gyms/gym_{}.json".format(fort['id'])
        # with open(gym_data_cells, 'w') as outfile:
            # json.dump(fort['gym_details'], outfile)
    
    #print('Response dictionary (loaded): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(cells)))
    #print('***********************************')
    #print('***********************************')
    #print('Response dictionary (loaded): \n\r{}'.format(pprint.PrettyPrinter(indent=2).pformat(gym_details_loaded)))

    #gyms = {}
    #gyms[Fort.FortId] = [Fort.Team, Fort.Latitude,Fort.Longitude, Fort.GymPoints]
    
    
    for gym in gym_details_loaded:
        print("Gym is: {} - {} with picture from {}").format(gym.get('name','GYM WITHOUT NAME??'), gym.get('description', '-'), gym.get('urls',['-'])[0])
        fortdata = gym['gym_state']['fort_data']
        #print("It's owned by {} and has prestige {} [ID: {}]").format(numbertoteam[fortdata['owned_by_team']], fortdata['gym_points'], fortdata['id'])
        gymLevel=prestige_to_level(fortdata['gym_points'])
        teamID = fortdata.get('owned_by_team',0)
        print("It's owned by {} and has prestige {} (Level {}) [ID: {}]").format(numToTeamCol[teamID], fortdata['gym_points'], gymLevel, fortdata['id'])
        
        colour = numbertocolour[fortdata['owned_by_team']]

        webpage = "{web_dir}/gym_{id}.html".format(web_dir=web_dir, id=fortdata['id'])
        with open(webpage, 'w') as outfile:
            outfile.truncate()
            outfile.writelines("""<!DOCTYPE html>
            <html>
            <head>
            <link rel="stylesheet" href="styles.css">
            </head>
            <body id="{owner}">""".format(owner=numbertoteam[teamID]))
            outfile.writelines("<h1>{gymName}</h1>".format(gymName=gym.get('name','GYM WITHOUT NAME??')))
            outfile.writelines("<h2>{gymDesc}</h2>".format(gymDesc=gym.get('description','(no description)')))
            outfile.writelines('<img id="main" src="{img}" width="25%" />'.format(img=gym.get('urls',['-'])[0]))
            outfile.writelines('<h2 style="{gym_colour}">Level {gym_level} {owner} gym</h2>'.format(gym_colour=numbertocolour[teamID],gym_level=gymLevel,owner=numbertoteam[teamID]))
            outfile.writelines('<table cols="7" rows="{tableRows}" id="t01">'.format(tableRows=gymLevel+1))
            outfile.writelines('<tr><th>Pokemon</th><th>Nickname</th><th>CP</th><th>Owner</th><th>Quick move</th><th>Charge move</th></tr>')

        #print("colour: {}".format(colour))
        #for mem in gym['gym_state']['memberships']:
        for index in range(len(gym['gym_state']['memberships'])):
            mem = gym['gym_state']['memberships'][index]
            poke = mem['pokemon_data']
            trainer = mem['trainer_public_profile']
            pokename = pokemonsJSON[str(poke['pokemon_id'])]
            pokenick = (poke.get('nickname', pokename)).encode('utf-8')
            
            move1 = '-'
            move1type = '-'
            move2 = '-'
            move2type = '-'
            
            for move in movesJSON:
                if move['id'] == poke['move_1']:
                    move1 = move.get('name','UNKNOWN')
                    move1type = move.get('type','UNKNOWN') #need a better moves.json with types (and damage/dps)
                if move['id'] == poke['move_2']:
                    move2 = move.get('name','UNKNOWN')
                    move2type = move.get('type','UNKNOWN') #need a better moves.json with types (and damage/dps)
            IVAtk=poke.get('individual_attack',0)
            IVDef=poke.get('individual_defense',0)
            IVSta=poke.get('individual_stamina',0)
            Percent = ((IVAtk+IVDef+IVSta)*100)/45
            #print("Member {} {}".format(index, pprint.PrettyPrinter(indent=2).pformat(mem)))
            print("Pokemon {num}:\n\r{pokemon_id} ({cp}CP) {nick} owned by {trainer_id} (L{trainer_level})".format(num=index+1, pokemon_id=pokename,cp=poke['cp'],trainer_id=trainer['name'],trainer_level=trainer['level'], nick=pokenick))
            print("HP: {hp} IVs {Percent}%: {IVAtk}Atk {IVDef}Def {IVSta}Sta".format(hp=poke['stamina']/2,IVAtk=IVAtk,IVDef=IVDef,IVSta=IVSta,Percent=Percent))
            print("Moves: {mv1} ({mv1t}), {mv2} ({mv2t})".format(mv1=move1, mv1t=move1type, mv2=move2, mv2t=move2type))
            
            with open(webpage, 'a') as outfile:
                outfile.writelines('<tr>\n\r<td><img src="pokemon/{pokeid:>03}.gif" alt="{poketype}"></td>'.format(pokeid=poke.get('pokemon_id','Egg'), poketype=pokename))
                outfile.writelines('<td>{pokenick}</td><td>{cp}</td><td>{owner} (L.{trainer_level})</td><td>{mv1} ({mv1t})</td><td>{mv2} ({mv2t})</td></tr>'.format(pokenick=pokenick, cp=poke.get('cp','0'), owner=trainer.get('name','MISSINGNO.'), trainer_level=trainer.get('level','0'),mv1=move1, mv1t=move1type, mv2=move2, mv2t=move2type))
            
            
# >>> from string import Template
# >>> s = Template('$who likes $what')
# >>> s.substitute(who='tim', what='kung pao')
# 'tim likes kung pao'
# >>> d = dict(who='tim')
# >>> Template('Give $who $100').substitute(d)
# Traceback (most recent call last):
# ...
# ValueError: Invalid placeholder in string: line 1, col 11
# >>> Template('$who likes $what').substitute(d)
# Traceback (most recent call last):
# ...
# KeyError: 'what'
# >>> Template('$who likes $what').safe_substitute(d)
# 'tim likes $what'
            
        print("*********\n\r\n\r")
コード例 #30
0
def main():
    # Patch threading to make exceptions catchable.
    install_thread_excepthook()

    # Make sure exceptions get logged.
    sys.excepthook = handle_exception

    args = get_args()

    # Add file logging if enabled.
    if args.verbose and args.verbose != 'nofile':
        filelog = logging.FileHandler(args.verbose)
        filelog.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(threadName)16s][%(module)14s][%(levelname)8s] %(message)s'
            ))
        logging.getLogger('').addHandler(filelog)
    if args.very_verbose and args.very_verbose != 'nofile':
        filelog = logging.FileHandler(args.very_verbose)
        filelog.setFormatter(
            logging.Formatter(
                '%(asctime)s [%(threadName)16s][%(module)14s][%(levelname)8s] %(message)s'
            ))
        logging.getLogger('').addHandler(filelog)

    if args.verbose or args.very_verbose:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # Let's not forget to run Grunt / Only needed when running with webserver.
    if not args.no_server:
        if not os.path.exists(
                os.path.join(os.path.dirname(__file__), 'static/dist')):
            log.critical(
                'Missing front-end assets (static/dist) -- please run "npm install && npm run build" before starting the server.'
            )
            sys.exit()

    # These are very noisy, let's shush them up a bit.
    logging.getLogger('peewee').setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.pgoapi').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.rpc_api').setLevel(logging.INFO)
    logging.getLogger('werkzeug').setLevel(logging.ERROR)

    config['parse_pokemon'] = not args.no_pokemon
    config['parse_pokestops'] = not args.no_pokestops
    config['parse_gyms'] = not args.no_gyms

    # Turn these back up if debugging.
    if args.verbose or args.very_verbose:
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
    if args.very_verbose:
        logging.getLogger('peewee').setLevel(logging.DEBUG)
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi.pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi.rpc_api').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)
        logging.getLogger('werkzeug').setLevel(logging.DEBUG)

    # Use lat/lng directly if matches such a pattern.
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    # Use the latitude and longitude to get the local altitude from Google.
    try:
        url = 'https://maps.googleapis.com/maps/api/elevation/json?locations={},{}'.format(
            str(position[0]), str(position[1]))
        altitude = requests.get(url).json()[u'results'][0][u'elevation']
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    except (requests.exceptions.RequestException, IndexError, KeyError):
        log.error('Unable to retrieve altitude from Google APIs; setting to 0')

    if not any(position):
        log.error('Could not get a position by name, aborting!')
        sys.exit()

    log.info('Parsed location is: %.4f/%.4f/%.4f (lat/lng/alt)', position[0],
             position[1], position[2])

    if args.no_pokemon:
        log.info('Parsing of Pokemon disabled.')
    if args.no_pokestops:
        log.info('Parsing of Pokestops disabled.')
    if args.no_gyms:
        log.info('Parsing of Gyms disabled.')
    if args.encounter:
        log.info('Encountering pokemon enabled.')

    config['LOCALE'] = args.locale
    config['CHINA'] = args.china

    app = Pogom(__name__)
    db = init_database(app)
    if args.clear_db:
        log.info('Clearing database')
        if args.db_type == 'mysql':
            drop_tables(db)
        elif os.path.isfile(args.db):
            os.remove(args.db)
    create_tables(db)

    app.set_current_location(position)

    # Control the search status (running or not) across threads.
    pause_bit = Event()
    pause_bit.clear()
    if args.on_demand_timeout > 0:
        pause_bit.set()

    heartbeat = [now()]

    # Setup the location tracking queue and push the first location on.
    new_location_queue = Queue()
    new_location_queue.put(position)

    # DB Updates
    db_updates_queue = Queue()

    # Thread(s) to process database updates.
    for i in range(args.db_threads):
        log.debug('Starting db-updater worker thread %d', i)
        t = Thread(target=db_updater,
                   name='db-updater-{}'.format(i),
                   args=(args, db_updates_queue))
        t.daemon = True
        t.start()

    # db cleaner; really only need one ever.
    if not args.disable_clean:
        t = Thread(target=clean_db_loop, name='db-cleaner', args=(args, ))
        t.daemon = True
        t.start()

    # WH Updates.
    wh_updates_queue = Queue()

    # Thread to process webhook updates.
    for i in range(args.wh_threads):
        log.debug('Starting wh-updater worker thread %d', i)
        t = Thread(target=wh_updater,
                   name='wh-updater-{}'.format(i),
                   args=(args, wh_updates_queue))
        t.daemon = True
        t.start()

    if not args.only_server:

        # Check all proxies before continue so we know they are good.
        if args.proxy and not args.proxy_skip_check:

            # Overwrite old args.proxy with new working list.
            args.proxy = check_proxies(args)

        # Gather the Pokemon!

        # Attempt to dump the spawn points (do this before starting threads of endure the woe).
        if args.spawnpoint_scanning and args.spawnpoint_scanning != 'nofile' and args.dump_spawnpoints:
            with open(args.spawnpoint_scanning, 'w+') as file:
                log.info('Saving spawn points to %s', args.spawnpoint_scanning)
                spawns = Pokemon.get_spawnpoints_in_hex(
                    position, args.step_limit)
                file.write(json.dumps(spawns))
                log.info('Finished exporting spawn points')

        argset = (args, new_location_queue, pause_bit, heartbeat,
                  db_updates_queue, wh_updates_queue)

        log.debug('Starting a %s search thread', args.scheduler)
        search_thread = Thread(target=search_overseer_thread,
                               name='search-overseer',
                               args=argset)
        search_thread.daemon = True
        search_thread.start()

    if args.cors:
        CORS(app)

    # No more stale JS.
    init_cache_busting(app)

    app.set_search_control(pause_bit)
    app.set_heartbeat_control(heartbeat)
    app.set_location_queue(new_location_queue)

    config['ROOT_PATH'] = app.root_path
    config['GMAPS_KEY'] = args.gmaps_key

    if args.no_server:
        # This loop allows for ctrl-c interupts to work since flask won't be holding the program open.
        while search_thread.is_alive():
            time.sleep(60)
    else:
        ssl_context = None
        if args.ssl_certificate and args.ssl_privatekey \
                and os.path.exists(args.ssl_certificate) and os.path.exists(args.ssl_privatekey):
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_context.load_cert_chain(args.ssl_certificate,
                                        args.ssl_privatekey)
            log.info('Web server in SSL mode.')
        if args.verbose or args.very_verbose:
            app.run(threaded=True,
                    use_reloader=False,
                    debug=True,
                    host=args.host,
                    port=args.port,
                    ssl_context=ssl_context)
        else:
            app.run(threaded=True,
                    use_reloader=False,
                    debug=False,
                    host=args.host,
                    port=args.port,
                    ssl_context=ssl_context)
コード例 #31
0
ファイル: test.py プロジェクト: tetsunosuke/pgoapi
def main():
    # log settings
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return

    if config.test:
        return

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # provide player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service, config.username, config.password):
        return

    # chain subrequests (methods) into one RPC call

    # get player profile call
    # ----------------------
    #api.get_player()

    # get inventory call
    # ----------------------
    #api.get_inventory()

    # get map objects call
    # repeated fields (e.g. cell_id and since_timestamp_ms in get_map_objects) can be provided over a list
    # ----------------------
    #cell_ids = util.get_cell_ids(position[0], position[1])
    cell_ids = get_cell_ids(position[0], position[1])
    timestamps = [0,] * len(cell_ids)
    response_dict = api.get_map_objects(latitude = position[0], longitude = position[1], since_timestamp_ms = timestamps, cell_id = cell_ids)
    #response_dict = api.call()
    with open("forts.json", "w") as f:
        json.dump(response_dict,f, indent=4, sort_keys=True)
    fort = response_dict["responses"]["GET_MAP_OBJECTS"]["map_cells"][0]["forts"][0]
    #cells_0 = response_dict["responses"]["GET_MAP_OBJECTS"]["map_cells"][0]

    # spin a fort
    # ----------------------
    #fortid = '<your fortid>'
    #lng = <your longitude>
    #lat = <your latitude>
    #api.fort_search(fort_id=fortid, fort_latitude=lat, fort_longitude=lng, player_latitude=f2i(position[0]), player_longitude=f2i(position[1]))
    if fort["enabled"]:
        if fort.has_key("cooldown_complete_timestamp_ms"):
            if fort["cooldown_complete_timestamp_ms"] > response_dict["responses"]["GET_MAP_OBJECTS"]["map_cells"][0]["current_timestamp_ms"]:
                return
        fortid = fort["id"]
        lng = fort["longitude"]
        lat = fort["latitude"]
        response_dict = api.fort_details(fort_id=fortid, latitude=lat, longitude=lng)
        #, player_latitude=util.f2i(position[0]), player_longitude=util.f2i(position[1]))
        #response_dict = api.call()
        with open("fort_details.json", "w") as f:
            json.dump(response_dict,f, indent=4, sort_keys=True)
        if not response_dict["responses"].has_key("FORT_DETAILS"):
            print "failed"
            with open("fail_fort.json", "w") as f:
                json.dump(fort, f, indent=4, sort_keys=True)
            return

        detailed = response_dict["responses"]["FORT_DETAILS"]
        response_dict = api.fort_search(fort_id=detailed["fort_id"], fort_latitude=detailed["latitude"], fort_longitude=detailed["longitude"], player_latitude=util.f2i(position[0]), player_longitude=util.f2i(position[1]))
        time.sleep(1)
        #response_dict = api.call()
        with open("fort_search.json", "w") as f:
            json.dump(response_dict,f, indent=4, sort_keys=True)
コード例 #32
0
ファイル: search.py プロジェクト: gsabran/PokemonGo-Map
def player_has_reset_initial_position(args):
    player_id = get_player_id(args)
    player = Player.select().where(Player.player_id == player_id)[0]
    start_position = get_pos_by_name(args.location)
    return not (almost_equals(player.start_latitude, start_position[0]) and almost_equals(player.start_longitude, start_position[1]))
コード例 #33
0
    # Turn these back up if debugging
    if args.debug:
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)

    # use lat/lng directly if matches such a pattern
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates directly from CLI')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    # Use the latitude and longitude to get the local altitude from Google
    try:
        url = 'https://maps.googleapis.com/maps/api/elevation/json?locations={},{}'.format(
            str(position[0]), str(position[1]))
        altitude = requests.get(url).json()[u'results'][0][u'elevation']
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    except (requests.exceptions.RequestException, IndexError, KeyError):
        log.error('Unable to retrieve altitude from Google APIs; setting to 0')

    if not any(position):
        log.error('Could not get a position by name, aborting')
        sys.exit()
コード例 #34
0
ファイル: views.py プロジェクト: misosoph/PogoManager
def login():
    if request.method == 'POST':
        pokemon_list = json.load(open('app/pokemon.json'))
        pokemon_data = json.load(open('app/pogodata.json'))
        pokemon_map = {pokemon_list[str(int(re.match(r"V([0-9]+).*",x['Pokemon']['UniqueId']).group(1)))] : x['Pokemon'] for x in pokemon_data['Items'] if 'Pokemon' in x}
        pokemon_evo_map = {pokemon_list[str(int(re.match(r"V([0-9]+).*",x['Pokemon']['ParentId']).group(1)))] : pokemon_list[str(int(re.match(r"V([0-9]+).*",x['Pokemon']['UniqueId']).group(1)))] for x in pokemon_data['Items'] if 'Pokemon' in x and 'ParentId' in x['Pokemon']}

        if request.form['type'] == "Show Raw Game Data":
            def my_safe_repr(object, context, maxlevels, level):
                typ = pprint._type(object)
                if typ is unicode:
                    object = str(unicode(object).encode("utf-8"))
                return pprint._safe_repr(object, context, maxlevels, level)

            printer = pprint.PrettyPrinter(indent=2)
            printer.format = my_safe_repr
            contents = ('StaticData: \n\r{}'.format(printer.pformat(sorted([x.items()[0] for x in pokemon_data['Items']], key=lambda x:x[0]))))    
            return render_template('results.html', contents=contents)
        # instantiate pgoapi
        api = pgoapi.PGoApi()

    # parse position
        position = util.get_pos_by_name('New York, NY')
        if not position:
            error = 'Your given location could not be found by name'
            return render_template('results.html', contents=error)
    
        # set player position on the earth
        api.set_position(*position)
    
        if not api.login(request.form['auth'], request.form['username'], request.form['password'], app_simulation = True):
            error = 'Login failed for '+request.form['username']
            return render_template('results.html', contents=error)
        else:
            # get player profile + inventory call (thread-safe/chaining example)
            # ----------------------
            req = api.create_request()
            #req.get_player()
            req.get_inventory()
            response_dict = req.call()
            #print(contents)
            
            pokemons = []
            inventory_items = response_dict['responses'] \
                                           ['GET_INVENTORY'] \
                                           ['inventory_delta'] \
                                           ['inventory_items']

            
            for item in inventory_items:
                try:
                    reduce(dict.__getitem__, ["inventory_item_data", "pokemon_data"], item)
                except KeyError:
                    pass
                else:
                    try:
                        pokemon = item['inventory_item_data']['pokemon_data']
    
                        pid = pokemon['id']
                        num = int(pokemon['pokemon_id'])
                        name = pokemon_list[str(num)]
    
                        attack = pokemon.get('individual_attack', 0)
                        defense = pokemon.get('individual_defense', 0)
                        stamina = pokemon.get('individual_stamina', 0)
                        iv_percent = (float(attack + defense + stamina) / 45) * 100.0
                        
                        cp_mult = pokemon.get('cp_multiplier', 0)
                        addtl_cp_mult = pokemon.get('additional_cp_multiplier', 0)
    
                        nickname = pokemon.get('nickname', 'NONE')
                        combat_power = pokemon.get('cp', 0)
                        
                        base_attack = pokemon_map[name]['Stats']['BaseAttack']
                        base_defense = pokemon_map[name]['Stats']['BaseDefense']
                        base_stamina = pokemon_map[name]['Stats']['BaseStamina']
                        
                        cap_time = datetime.datetime.fromtimestamp(int(pokemon.get('creation_time_ms', 0)/1000))

                        max_cp_mult = 0.7903001
                        
                        
                        def compute_cp(base_attack, attack, base_defense, defense, base_stamina, stamina, cp_mult, addtl_cp_mult):
                            return ((base_attack + attack) * 
                                        pow(base_defense + defense, .5) * 
                                        pow(base_stamina + stamina, .5) *
                                        pow(cp_mult + addtl_cp_mult, 2))/10
                        
                        computed_cp = compute_cp(base_attack, attack, base_defense, defense, base_stamina, stamina, cp_mult, addtl_cp_mult)
                                        
                        min_final_cp = compute_cp(base_attack, 0, base_defense, 0, base_stamina, 0, max_cp_mult, 0)
                        this_final_cp = compute_cp(base_attack, attack, base_defense, defense, base_stamina, stamina, max_cp_mult, 0)
                        max_final_cp = compute_cp(base_attack, 15, base_defense, 15, base_stamina, 15, max_cp_mult, 0)
                        
                        next_evo = pokemon_evo_map.get(name, '')
                        if next_evo != '':
                            next_evo = pokemon_evo_map.get(next_evo, next_evo)
                        next_evo_stats = pokemon_map.get(next_evo, {}).get('Stats', {})
                        next_evo_cp=''
                        next_evo_sf=''
                        if next_evo != '':
                            next_evo_cp = round(compute_cp(next_evo_stats['BaseAttack'], attack, next_evo_stats['BaseDefense'], defense, next_evo_stats['BaseStamina'], stamina, cp_mult, addtl_cp_mult),2)
                            final_evo_max_cp = compute_cp(next_evo_stats['BaseAttack'], 15, next_evo_stats['BaseDefense'], 15, next_evo_stats['BaseStamina'], 15, cp_mult, addtl_cp_mult)
                            next_evo_sf = round(100 * (1-next_evo_cp/final_evo_max_cp), 2)
                        
                        newRow = collections.OrderedDict([
                            ('name', name),
                            ('cp', combat_power),
                            ('nickname', nickname),
                            ('num', num),
                            ('cap_date', cap_time.strftime('%x')),
                            ('cap_time', cap_time.strftime('%X')),
                            ('attack', attack),
                            ('defense', defense),
                            ('stamina', stamina),
                            ('iv_percent', round(iv_percent,2)),
                            ('cp_mult', round(cp_mult,2)),
                            ('adtl_cp_mult', round(addtl_cp_mult,2)),
                            ('min_final_cp', round(min_final_cp,2)),
                            ('max_final_cp', round(max_final_cp,2)),
                            ('ths_final_cp', round(this_final_cp,2)),
                            ('max_iv_imp', round((max_final_cp/min_final_cp - 1.0)*100,2)),
                            ('this_iv_imp', round((this_final_cp/min_final_cp - 1.0)*100,2)),
                            ('iv_max_sf', round(((max_final_cp-this_final_cp)/min_final_cp)*100,2)),
                            ('final_evo', next_evo),
                            ('final_evo_cp', next_evo_cp),
                            ('final_evo_sf', next_evo_sf)
                            ])
                        pokemons.append(newRow)
                    except KeyError:
                        pass        
            pokemons.sort(key = lambda x:x['cp'], reverse=True)
            if request.form['type'] == "Show My Poke":
                contents = 'Player Pokemon\n\n'
                for key in pokemons[1].keys():
                    contents += '%-10s\t' % key
                for row in pokemons:
                    contents += "\n"
                    for key,value in row.items():
                        contents += '%-10s\t' % (unicode(value).encode("utf-8"))
                return render_template('results.html', contents=contents.decode('utf-8'))
            elif request.form['type'] == "Show My Raw Data":
                contents = ('Response dictionary (get_player + get_inventory): \n\r{}'.format(pprint.PrettyPrinter(indent=2).pformat(sorted([x['inventory_item_data'] for x in inventory_items], key=lambda x:x.keys()[0]))))    
                return render_template('results.html', contents=contents)
            else:
                
                si = StringIO.StringIO()
                cw = csv.writer(si)
                cw.writerow(pokemons[1].keys())
                for row in pokemons:
                    cw.writerow([unicode(x).encode("utf-8") for x in row.values()])
                output = make_response(si.getvalue())
                output.headers["Content-Disposition"] = "attachment; filename=pokemon.csv"
                output.headers["Content-type"] = "text/csv"
                return output
            
    return render_template('index.html')
コード例 #35
0
ファイル: pokecli.py プロジェクト: 0z4ck/pgoapi
def main():
    # log settings
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return

    if config.test:
        return

    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # provide player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service, config.username, config.password):
        return

    # chain subrequests (methods) into one RPC call

    # get player profile call
    # ----------------------
    api.get_player()

    # get inventory call
    # ----------------------
    api.get_inventory()

    # get map objects call
    # repeated fields (e.g. cell_id and since_timestamp_ms in get_map_objects) can be provided over a list
    # ----------------------
    #cell_ids = util.get_cell_ids(position[0], position[1])
    #timestamps = [0,] * len(cell_ids)
    #api.get_map_objects(latitude = position[0], longitude = position[1], since_timestamp_ms = timestamps, cell_id = cell_ids)

    # spin a fort
    # ----------------------
    #fortid = '<your fortid>'
    #lng = <your longitude>
    #lat = <your latitude>
    #api.fort_search(fort_id=fortid, fort_latitude=lat, fort_longitude=lng, player_latitude=f2i(position[0]), player_longitude=f2i(position[1]))

    # release/transfer a pokemon and get candy for it
    # ----------------------
    #api.release_pokemon(pokemon_id = <your pokemonid>)

    # evolve a pokemon if you have enough candies
    # ----------------------
    #api.evolve_pokemon(pokemon_id = <your pokemonid>)

    # get download settings call
    # ----------------------
    #api.download_settings(hash="05daf51635c82611d1aac95c0b051d3ec088a930")

    # execute the RPC call
    response_dict = api.call()

    # print the response dict
    print('Response dictionary: \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
コード例 #36
0
ファイル: pokecli.py プロジェクト: PogoHop/pgoapi-hsvr
def main():
    # log settings
    # log format
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)

    device_info = {
        'device_id': uuid.uuid4().hex,
        'device_brand': 'Apple',
        'device_model': 'iPhone',
        'device_comms_model': 'iPhone8,2',
        'hardware_manufacturer': 'Apple',
        'hardware_model': 'N66AP',
        'firmware_brand': 'iPhone OS',
        'firmware_type': '9.3.3'
    }
    # instantiate pgoapi
    api = pgoapi.PGoApi(device_info=device_info)
    if config.proxy:
        api.set_proxy({'http': config.proxy, 'https': config.proxy})

    # parse position TODO
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    #set player position on the earth
    api.set_position(*position)

    # new authentication initialitation
    if config.proxy:
        api.set_authentication(provider=config.auth_service,
                               username=config.username,
                               password=config.password,
                               proxy_config={
                                   'http': config.proxy,
                                   'https': config.proxy
                               })
    else:
        api.set_authentication(provider=config.auth_service,
                               username=config.username,
                               password=config.password)

    # provide the path for your encrypt dll
    api.activate_signature(
        os.path.join(os.path.dirname(os.path.realpath(__file__)),
                     "libencrypt.so"))
    api.app_simulation_login()

    time.sleep(10)

    cell_ids = util.get_cell_ids(position[0], position[1])
    timestamps = [
        0,
    ] * len(cell_ids)
    request = api.create_request()
    request.get_map_objects(latitude=position[0],
                            longitude=position[1],
                            since_timestamp_ms=timestamps,
                            cell_id=cell_ids)
    request.check_challenge()
    #request.get_hatched_eggs()
    #request.get_inventory()
    #request.check_awarded_badges()
    #request.download_settings(hash="8631c515a4c084ef30ef4d6eee8f5f5b250db697")#old54b359c97e46900f87211ef6e6dd0b7f2a3ea1f5
    #request.get_buddy_walked()
    response_dict = request.call()
    #print('Response dictionary (map_objects): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))

    if 'status' not in response_dict['responses']['GET_MAP_OBJECTS']:
        raise ValueError("Invalid response: {}".format(
            json.dumps(response_dict, indent=2)))
    if response_dict['responses']['GET_MAP_OBJECTS']['status'] == 1:
        for cell in response_dict['responses']['GET_MAP_OBJECTS']['map_cells']:
            if 'nearby_pokemons' in cell:
                print "nearby_pokemons in cell: " + cell['s2_cell_id']
                print cell['nearby_pokemons']
            if 'wild_pokemons' in cell:
                pprint.PrettyPrinter(indent=2).pformat(cell['wild_pokemons'])

        print response_dict['responses']['CHECK_CHALLENGE']
    """
コード例 #37
0
def main():
    # Patch threading to make exceptions catchable.
    install_thread_excepthook()

    # Make sure exceptions get logged.
    sys.excepthook = handle_exception

    args = get_args()

    # Abort if status name is not alphanumeric.
    if not str(args.status_name).isalnum():
        log.critical('Status name must be alphanumeric.')
        sys.exit(1)

    set_log_and_verbosity(log)

    config['parse_pokemon'] = not args.no_pokemon
    config['parse_pokestops'] = not args.no_pokestops
    config['parse_gyms'] = not args.no_gyms
    config['parse_raids'] = not args.no_raids

    # Let's not forget to run Grunt / Only needed when running with webserver.
    if not args.no_server and not validate_assets(args):
        sys.exit(1)

    # Use lat/lng directly if matches such a pattern.
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    if position is None or not any(position):
        log.error("Location not found: '{}'".format(args.location))
        sys.exit()

    # Use the latitude and longitude to get the local altitude from Google.
    (altitude, status) = get_gmaps_altitude(position[0], position[1],
                                            args.gmaps_key)
    if altitude is not None:
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    else:
        if status == 'REQUEST_DENIED':
            log.error(
                'Google API Elevation request was denied. You probably ' +
                'forgot to enable elevation api in https://console.' +
                'developers.google.com/apis/api/elevation_backend/')
            sys.exit()
        else:
            log.error('Unable to retrieve altitude from Google APIs' +
                      'setting to 0')

    log.info('Parsed location is: %.4f/%.4f/%.4f (lat/lng/alt)',
             position[0], position[1], position[2])

    if args.no_pokemon:
        log.info('Parsing of Pokemon disabled.')
    if args.no_pokestops:
        log.info('Parsing of Pokestops disabled.')
    if args.no_gyms:
        log.info('Parsing of Gyms disabled.')
    if args.encounter:
        log.info('Encountering pokemon enabled.')

    config['LOCALE'] = args.locale
    config['CHINA'] = args.china

    # if we're clearing the db, do not bother with the blacklist
    if args.clear_db:
        args.disable_blacklist = True
    app = Pogom(__name__)
    app.before_request(app.validate_request)

    db = init_database(app)
    if args.clear_db:
        log.info('Clearing database')
        if args.db_type == 'mysql':
            drop_tables(db)
        elif os.path.isfile(args.db):
            os.remove(args.db)

    verify_database_schema(db)

    create_tables(db)

    # fixing encoding on present and future tables
    verify_table_encoding(db)

    if args.clear_db:
        log.info(
            'Drop and recreate is complete. Now remove -cd and restart.')
        sys.exit()

    app.set_current_location(position)

    # Control the search status (running or not) across threads.
    control_flags = {
      'on_demand': Event(),
      'api_watchdog': Event(),
      'search_control': Event()
    }

    for flag in control_flags.values():
        flag.clear()

    if args.on_demand_timeout > 0:
        control_flags['on_demand'].set()

    heartbeat = [now()]

    # Setup the location tracking queue and push the first location on.
    new_location_queue = Queue()
    new_location_queue.put(position)

    # DB Updates
    db_updates_queue = Queue()

    # Thread(s) to process database updates.
    for i in range(args.db_threads):
        log.debug('Starting db-updater worker thread %d', i)
        t = Thread(target=db_updater, name='db-updater-{}'.format(i),
                   args=(args, db_updates_queue, db))
        t.daemon = True
        t.start()

    # db cleaner; really only need one ever.
    if not args.disable_clean:
        t = Thread(target=clean_db_loop, name='db-cleaner', args=(args,))
        t.daemon = True
        t.start()

    # WH updates queue & WH unique key LFU caches.
    # The LFU caches will stop the server from resending the same data an
    # infinite number of times. The caches will be instantiated in the
    # webhook's startup code.
    wh_updates_queue = Queue()
    wh_key_cache = {}

    # Thread to process webhook updates.
    for i in range(args.wh_threads):
        log.debug('Starting wh-updater worker thread %d', i)
        t = Thread(target=wh_updater, name='wh-updater-{}'.format(i),
                   args=(args, wh_updates_queue, wh_key_cache))
        t.daemon = True
        t.start()

    config['ROOT_PATH'] = app.root_path
    config['GMAPS_KEY'] = args.gmaps_key

    if not args.only_server:
        # Check if we are able to scan.
        if not can_start_scanning(args):
            sys.exit(1)

        # Processing proxies if set (load from file, check and overwrite old
        # args.proxy with new working list).
        args.proxy = load_proxies(args)

        if args.proxy and not args.proxy_skip_check:
            args.proxy = check_proxies(args, args.proxy)

        # Run periodical proxy refresh thread.
        if (args.proxy_file is not None) and (args.proxy_refresh > 0):
            t = Thread(target=proxies_refresher,
                       name='proxy-refresh', args=(args,))
            t.daemon = True
            t.start()
        else:
            log.info('Periodical proxies refresh disabled.')

        # Update player locale if not set correctly, yet.
        args.player_locale = PlayerLocale.get_locale(args.location)
        if not args.player_locale:
            args.player_locale = gmaps_reverse_geolocate(
                args.gmaps_key,
                args.locale,
                str(position[0]) + ', ' + str(position[1]))
            db_player_locale = {
                'location': args.location,
                'country': args.player_locale['country'],
                'language': args.player_locale['country'],
                'timezone': args.player_locale['timezone'],
            }
            db_updates_queue.put((PlayerLocale, {0: db_player_locale}))
        else:
            log.debug(
                'Existing player locale has been retrieved from the DB.')

        # Gather the Pokemon!

        # Attempt to dump the spawn points (do this before starting threads of
        # endure the woe).
        if (args.spawnpoint_scanning and
                args.spawnpoint_scanning != 'nofile' and
                args.dump_spawnpoints):
            with open(args.spawnpoint_scanning, 'w+') as file:
                log.info(
                    'Saving spawn points to %s', args.spawnpoint_scanning)
                spawns = SpawnPoint.get_spawnpoints_in_hex(
                    position, args.step_limit)
                file.write(json.dumps(spawns))
                log.info('Finished exporting spawn points')

        argset = (args, new_location_queue, control_flags,
                  heartbeat, db_updates_queue, wh_updates_queue)

        log.debug('Starting a %s search thread', args.scheduler)
        search_thread = Thread(target=search_overseer_thread,
                               name='search-overseer', args=argset)
        search_thread.daemon = True
        search_thread.start()

    if args.cors:
        CORS(app)

    # No more stale JS.
    init_cache_busting(app)

    app.set_search_control(control_flags['search_control'])
    app.set_heartbeat_control(heartbeat)
    app.set_location_queue(new_location_queue)

    if args.no_server:
        # This loop allows for ctrl-c interupts to work since flask won't be
        # holding the program open.
        while search_thread.is_alive():
            time.sleep(60)
    else:
        ssl_context = None
        if (args.ssl_certificate and args.ssl_privatekey and
                os.path.exists(args.ssl_certificate) and
                os.path.exists(args.ssl_privatekey)):
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_context.load_cert_chain(
                args.ssl_certificate, args.ssl_privatekey)
            log.info('Web server in SSL mode.')
        if args.verbose:
            app.run(threaded=True, use_reloader=False, debug=True,
                    host=args.host, port=args.port, ssl_context=ssl_context)
        else:
            app.run(threaded=True, use_reloader=False, debug=False,
                    host=args.host, port=args.port, ssl_context=ssl_context)
コード例 #38
0
ファイル: runserver.py プロジェクト: Zoruk/PokemonGo-Map
def main():
    args = get_args()

    # Check if we have the proper encryption library file and get its path
    encryption_lib_path = get_encryption_lib_path(args)
    if encryption_lib_path is "":
        sys.exit(1)

    if args.debug:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # Let's not forget to run Grunt / Only needed when running with webserver
    if not args.no_server:
        if not os.path.exists(os.path.join(os.path.dirname(__file__), 'static/dist')):
            log.critical('Missing front-end assets (static/dist) -- please run "npm install && npm run build" before starting the server')
            sys.exit()

    # These are very noisey, let's shush them up a bit
    logging.getLogger('peewee').setLevel(logging.INFO)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.pgoapi').setLevel(logging.WARNING)
    logging.getLogger('pgoapi.rpc_api').setLevel(logging.INFO)
    logging.getLogger('werkzeug').setLevel(logging.ERROR)

    config['parse_pokemon'] = not args.no_pokemon
    config['parse_pokestops'] = not args.no_pokestops
    config['parse_gyms'] = not args.no_gyms

    # Turn these back up if debugging
    if args.debug:
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)

    # use lat/lng directly if matches such a pattern
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    # Use the latitude and longitude to get the local altitude from Google
    try:
        url = 'https://maps.googleapis.com/maps/api/elevation/json?locations={},{}'.format(
            str(position[0]), str(position[1]))
        altitude = requests.get(url).json()[u'results'][0][u'elevation']
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    except (requests.exceptions.RequestException, IndexError, KeyError):
        log.error('Unable to retrieve altitude from Google APIs; setting to 0')

    if not any(position):
        log.error('Could not get a position by name, aborting')
        sys.exit()

    log.info('Parsed location is: %.4f/%.4f/%.4f (lat/lng/alt)',
             position[0], position[1], position[2])

    if args.no_pokemon:
        log.info('Parsing of Pokemon disabled')
    if args.no_pokestops:
        log.info('Parsing of Pokestops disabled')
    if args.no_gyms:
        log.info('Parsing of Gyms disabled')

    config['LOCALE'] = args.locale
    config['CHINA'] = args.china

    app = Pogom(__name__)
    db = init_database(app)
    if args.clear_db:
        log.info('Clearing database')
        if args.db_type == 'mysql':
            drop_tables(db)
        elif os.path.isfile(args.db):
            os.remove(args.db)
    create_tables(db)

    app.set_current_location(position)

    # Control the search status (running or not) across threads
    pause_bit = Event()
    pause_bit.clear()

    # Setup the location tracking queue and push the first location on
    new_location_queue = Queue()
    new_location_queue.put(position)

    # DB Updates
    db_updates_queue = Queue()

    # Thread(s) to process database updates
    for i in range(args.db_threads):
        log.debug('Starting db-updater worker thread %d', i)
        t = Thread(target=db_updater, name='db-updater-{}'.format(i), args=(args, db_updates_queue))
        t.daemon = True
        t.start()

    # db clearner; really only need one ever
    t = Thread(target=clean_db_loop, name='db-cleaner', args=(args,))
    t.daemon = True
    t.start()

    # WH Updates
    wh_updates_queue = Queue()

    # Thread to process webhook updates
    for i in range(args.wh_threads):
        log.debug('Starting wh-updater worker thread %d', i)
        t = Thread(target=wh_updater, name='wh-updater-{}'.format(i), args=(args, wh_updates_queue))
        t.daemon = True
        t.start()

    if not args.only_server:
        # Gather the pokemons!

        # check the sort of scan
        if args.spawnpoint_scanning:
            mode = 'sps'
        else:
            mode = 'hex'

        # attempt to dump the spawn points (do this before starting threads of endure the woe)
        if args.spawnpoint_scanning and args.spawnpoint_scanning != 'nofile' and args.dump_spawnpoints:
            with open(args.spawnpoint_scanning, 'w+') as file:
                log.info('Sawing spawn points to %s', args.spawnpoint_scanning)
                spawns = Pokemon.get_spawnpoints_in_hex(position, args.step_limit)
                file.write(json.dumps(spawns))
                log.info('Finished exporting spawn points')

        argset = (args, mode, new_location_queue, pause_bit, encryption_lib_path, db_updates_queue, wh_updates_queue)

        log.debug('Starting a %s search thread', mode)
        search_thread = Thread(target=search_overseer_thread, name='search-overseer', args=argset)
        search_thread.daemon = True
        search_thread.start()

    if args.cors:
        CORS(app)

    # No more stale JS
    init_cache_busting(app)

    app.set_search_control(pause_bit)
    app.set_location_queue(new_location_queue)

    config['ROOT_PATH'] = app.root_path
    config['GMAPS_KEY'] = args.gmaps_key

    if args.no_server:
        # This loop allows for ctrl-c interupts to work since flask won't be holding the program open
        while search_thread.is_alive():
            time.sleep(60)
    else:
        ssl_context = None
        if args.ssl_certificate and args.ssl_privatekey \
                and os.path.exists(args.ssl_certificate) and os.path.exists(args.ssl_privatekey):
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_context.load_cert_chain(args.ssl_certificate, args.ssl_privatekey)
            log.info('Web server in SSL mode.')

        app.run(threaded=True, use_reloader=False, debug=args.debug, host=args.host, port=args.port, ssl_context=ssl_context)
コード例 #39
0
ファイル: runserver.py プロジェクト: weissi1994/Pokemap
    # Turn these back up if debugging
    if args.debug:
        logging.getLogger('requests').setLevel(logging.DEBUG)
        logging.getLogger('pgoapi').setLevel(logging.DEBUG)
        logging.getLogger('rpc_api').setLevel(logging.DEBUG)

    # use lat/lng directly if matches such a pattern
    prog = re.compile("^(\-?\d+\.\d+),?\s?(\-?\d+\.\d+)$")
    res = prog.match(args.location)
    if res:
        log.debug('Using coordinates from CLI directly')
        position = (float(res.group(1)), float(res.group(2)), 0)
    else:
        log.debug('Looking up coordinates in API')
        position = util.get_pos_by_name(args.location)

    # Use the latitude and longitude to get the local altitude from Google
    try:
        url = 'https://maps.googleapis.com/maps/api/elevation/json?locations={},{}'.format(
            str(position[0]), str(position[1]))
        altitude = requests.get(url).json()[u'results'][0][u'elevation']
        log.debug('Local altitude is: %sm', altitude)
        position = (position[0], position[1], altitude)
    except (requests.exceptions.RequestException, IndexError, KeyError):
        log.error('Unable to retrieve altitude from Google APIs; setting to 0')

    if not any(position):
        log.error('Could not get a position by name, aborting')
        sys.exit()
コード例 #40
0
ファイル: pokecli.py プロジェクト: self103/ivs
def main():
    # log settings
    # log format
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
    # log level for http request class
    logging.getLogger("requests").setLevel(logging.WARNING)
    # log level for main pgoapi class
    logging.getLogger("pgoapi").setLevel(logging.INFO)
    # log level for internal pgoapi class
    logging.getLogger("rpc_api").setLevel(logging.INFO)

    config = init_config()
    if not config:
        return

    if config.debug:
        logging.getLogger("requests").setLevel(logging.DEBUG)
        logging.getLogger("pgoapi").setLevel(logging.DEBUG)
        logging.getLogger("rpc_api").setLevel(logging.DEBUG)


    # instantiate pgoapi
    api = pgoapi.PGoApi()

    # parse position
    position = util.get_pos_by_name(config.location)
    if not position:
        log.error('Your given location could not be found by name')
        return
    elif config.test:
        return

    # set player position on the earth
    api.set_position(*position)

    if not api.login(config.auth_service, config.username, config.password, app_simulation = True):
        return

    # get player profile call (single command example)
    # ----------------------
    response_dict = api.get_player()
    print('Response dictionary (get_player): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
    
    # sleep 200ms due to server-side throttling
    time.sleep(0.2)

    # get player profile + inventory call (thread-safe/chaining example)
    # ----------------------
    req = api.create_request()
    req.get_player()
    req.get_inventory()
    response_dict = req.call()
    #print('Response dictionary (get_player + get_inventory): \n\r{}'.format(pprint.PrettyPrinter(indent=4).pformat(response_dict)))
    #print response_dict


    all_my_pokemon = {}
    with open('myPokemon.txt', 'w') as file:
        for i in response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']:#['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']['inventory_item_data']['pokemon_data']:
            try:
                stats = {}
                attack = 0
                defense = 0
                stamina = 0
                unique_id = int(i['inventory_item_data']['pokemon_data']['id'])

                number = int(i['inventory_item_data']['pokemon_data']['pokemon_id'])
                #stats.update({'number' : number})

                name = pokemon_list[number - 1]
                stats.update({'name' : name})

                cp = int(i['inventory_item_data']['pokemon_data']['cp'])
                stats.update({'cp' : cp})

                attack = int(i['inventory_item_data']['pokemon_data']['individual_attack'])
                stats.update({'attack' : attack})

                defense = int(i['inventory_item_data']['pokemon_data']['individual_defense'])
                stats.update({'defense' : defense})

                stamina = int(i['inventory_item_data']['pokemon_data']['individual_stamina'])
                stats.update({'stamina' : stamina})

                if number<10:
                    elong_num = str("00{}".format(str(number)))
                elif number<100:
                    elong_num = str("0{}".format(str(number)))
                else:
                    elong_num = number

                picture = str("http://eternia.fr/public/media/go/sprites/{0}.png".format(elong_num))
                #picture = str("/sprites/{0}.png".format(elong_num))
                stats.update({'picture' : picture})


                pocket_mon = {unique_id : stats}
                all_my_pokemon.update(pocket_mon)


            except:
                pass


        for j in all_my_pokemon:
            file.write(prettyTable(all_my_pokemon[j]))
            #file.write(all_my_pokemon[j]['name'])
            #file.write('\n')

            #file.write(str(all_my_pokemon[j]['image']))
            #file.write('\n\n')

    #print all_my_pokemon


    file.close()