Exemple #1
0
def get_captured_lat_lng(pokemon):
    lat, lng = 0, 0
    if pokemon.get('captured_cell_id'):
        cell = CellId(pokemon.get('captured_cell_id'))
        lat, lng = cell.to_lat_lng().lat().degrees, cell.to_lat_lng().lng(
        ).degrees
    return {'lat': lat, 'lng': lng}
 def post(self):
     log.msg("Got the TestKariosHandler req")
     from_tm = 0
     to_tm = 0
     #loc = []
     topic = "location"
     try:
         from_tm = self.request.arguments['start_time']
         to_tm = self.request.arguments['end_time']
     except Exception:
         log.msg("No time given, will give all location")
     if (from_tm != 0 and to_tm != 0):
         loc = self.get_data.kdb_get_data(topic, from_tm, to_tm)
     else:
         log.msg("inside else")
         r = yield self.conn.read_absolute(self, ["location"], 0)
         length = r['queries'][0]['sample_size']
         if length:
             if (length > 100):
                 n = 100
             else:
                 n = length
             res = r['queries'][0]['results'][0]['values']
             for i in range(0, n):
                 val = CellId(res[i][1])
                 lat = val.to_lat_lng().lat().degrees
                 longt = val.to_lat_lng().lng().degrees
                 tm = res[i][0] / 1000
                 v = {"latitude": lat, "longitude": longt, "timestamp": tm}
                 loc.append(v)
             data = loc
     self.write({"resp_code": True, "location": loc})
Exemple #3
0
def sub_cell(cell,i=0,dist=25):
    
    g = Geodesic.WGS84  # @UndefinedVariable
    olat = CellId.to_lat_lng(cell).lat().degrees
    olng = CellId.to_lat_lng(cell).lng().degrees

    p = g.Direct(olat, olng,(45+(90*i)),dist)
    c = CellId.from_lat_lng(LatLng.from_degrees(p['lat2'],p['lon2']))
    
    return c.parent(cell.level()+1)
Exemple #4
0
def sub_cell(cell,i=0,dist=25):
    
    g = Geodesic.WGS84  # @UndefinedVariable
    olat = CellId.to_lat_lng(cell).lat().degrees
    olng = CellId.to_lat_lng(cell).lng().degrees

    p = g.Direct(olat, olng,(45+(90*i)),dist)
    c = CellId.from_lat_lng(LatLng.from_degrees(p['lat2'],p['lon2']))
    
    return c.parent(cell.level()+1)
 def kdb_get_data(self, topic, from_tm, to_tm):
     loc = []
     result = []
     temp = 0
     j = k = 0
     max_speed = 0
     avg_speed = 0
     dist = 0
     if (from_tm != 0):
         r = yield self.conn.read_absolute(self, [topic], from_tm, to_tm)
         length = r['queries'][0]['sample_size']
         if length:
             res = r['queries'][0]['results'][0]['values']
             for rs in res:
                 val = CellId(rs[1])
                 lat = val.to_lat_lng().lat().degrees
                 longt = val.to_lat_lng().lng().degrees
                 tm = rs[0] / 1000
                 v = {
                     "latitude": lat,
                     "longitude": longt,
                     "timestamp": tm,
                     "speed": 0,
                     'distance': -1
                 }
                 loc.append(v)
             es = r['queries'][0]['results'][0]['tags']['speed']
             if len(es):
                 for x in es:
                     x = float(x)
                     loc[j]['speed'] = x
                     j = j + 1
                     temp = temp + x
                 max_speed = max(es)
                 avg_speed = temp / len(es)
                 avg_speed = float(format(avg_speed, '.2f'))
                 result.append(avg_speed)
                 result.append(float(max_speed))
             try:
                 ds = r['queries'][0]['results'][0]['tags']['distance']
                 if len(ds):
                     for x in ds:
                         x = float(x)
                         dist = dist + x
                         k = k + 1
                         if (dist == 0.0):
                             continue
                         loc[k]['distance'] = dist
             except Exception, e:
                 log.msg("No distance recorded in DB for this vehicle")
             result.append(loc)
             data = result
         else:
             data = "NULL"
 def kdb_get_data(self, topic, from_tm, to_tm):
     loc = []
     r = yield self.conn.read_absolute(self, [topic], from_tm, to_tm)
     log.msg("test_read_absolute_without_tags: ", r)
     length = r['queries'][0]['sample_size']
     if length:
         res = r['queries'][0]['results'][0]['values']
         for rs in res:
             val = CellId(rs[1])
             lat = val.to_lat_lng().lat().degrees
             longt = val.to_lat_lng().lng().degrees
             tm = rs[0] / 1000
             v = {"latitude": lat, "longitude": longt, "timestamp": tm}
             loc.append(v)
     returnValue(loc)
Exemple #7
0
    def _get_cell_id_from_latlong(self, radius=1000):
        # type: (Optional[int]) -> List[str]
        position_lat, position_lng, _ = self.api_wrapper.get_position()

        cells = get_cell_ids(position_lat, position_lng, radius)

        if self.config['debug']:
            self._log('Cells:', color='yellow')
            self._log('Origin: {},{}'.format(position_lat, position_lng), color='yellow')
            for cell in cells:
                cell_id = CellId(cell)
                lat_lng = cell_id.to_lat_lng()
                self._log('Cell  : {},{}'.format(lat_lng.lat().degrees, lat_lng.lng().degrees), color='yellow')

        return cells
Exemple #8
0
def main():

    config = init_config()
    if not config:
        return

    watchlist = get_watchlist('watch.txt')
    pokenames = get_pokenames('pokes.txt')

    db = sqlite3.connect(config.ndbfile)
    dbc = db.cursor()

    run = 1
    while run:

        _ccnt, y, z = 1, config.limit, (config.step - 1) * config.limit

        dbc.execute("SELECT cell_id FROM _queue ORDER BY cell_id LIMIT %d,%d" %
                    (z, y))
        del y
        # http://stackoverflow.com/questions/3614277/how-to-strip-from-python-pyodbc-sql-returns
        scan_queque = [x[0] for x in dbc.fetchall()]

        if config.regen or len(scan_queque) == 0:
            log.info('Generating scan queue...')
            if gen_que(config.ndbfile, config.fdbfile): continue
            else: return

        api = api_init(config)
        if api == None:
            log.error('Login failed!')
            return
        else:
            log.info('API online! starting Scan...')
        time.sleep(5)

        for queq in scan_queque:

            try:

                _ecnt = [0, 0]
                traverse = 0
                targets = []

                cell = CellId.from_token(queq)
                lat = CellId.to_lat_lng(cell).lat().degrees
                lng = CellId.to_lat_lng(cell).lng().degrees

                cell_ids = [cell.id()]

                response_dict = get_response(api, cell_ids, lat, lng)

                log.info('Scanning cell {} of {}.'.format(
                    _ccnt + z, z + (len(scan_queque))))

                for _map_cell in response_dict['responses']['GET_MAP_OBJECTS'][
                        'map_cells']:

                    if 'nearby_pokemons' in _map_cell:
                        for _poke in _map_cell['nearby_pokemons']:
                            _ecnt[0] += 1
                            _s = hex(_poke['encounter_id'])
                            _c = CellId(_map_cell['s2_cell_id']).to_token()
                            dbc.execute(
                                "INSERT OR IGNORE INTO encounters (encounter_id, cell_id, pokemon_id, encounter_time) VALUES ('{}','{}',{},{})"
                                "".format(
                                    _s.strip('L'), _c, _poke['pokemon_id'],
                                    int(_map_cell['current_timestamp_ms'] /
                                        1000)))

                            if _poke['pokemon_id'] in watchlist:
                                traverse = 1
                                targets.append(_poke['encounter_id'])
                                log.info('{} nearby!'.format(
                                    pokenames[_poke['pokemon_id']]))

                    if 'catchable_pokemons' in _map_cell:
                        for _poke in _map_cell['catchable_pokemons']:
                            _ecnt[1] += 1
                            _s = hex(_poke['encounter_id'])
                            dbc.execute(
                                "INSERT OR REPLACE INTO encounters (spawn_id, encounter_id, pokemon_id, encounter_time, expire_time) VALUES ('{}','{}',{},{},{})"
                                "".format(
                                    _poke['spawn_point_id'], _s.strip('L'),
                                    _poke['pokemon_id'],
                                    int(_map_cell['current_timestamp_ms'] /
                                        1000),
                                    int(_poke['expiration_timestamp_ms'] /
                                        1000)))

                db.commit()

                if traverse:
                    _remaining = len(targets)
                    log.info('Narrow search for %d Pokemon...' % len(targets))
                    time.sleep(config.delay)

                    _scnt = 1
                    subcells = susub_cells(cell)
                    for _sub in subcells:
                        log.debug('Scanning subcell {} of up to 16.'.format(
                            _scnt, (len(scan_queque))))

                        lat = CellId.to_lat_lng(_sub).lat().degrees
                        lng = CellId.to_lat_lng(_sub).lng().degrees
                        cell_ids = get_cell_ids(cover_circle(lat, lng, 100))

                        try:
                            response_dict = get_response(
                                api, cell_ids, lat, lng)
                        except NotLoggedInException:
                            api = None
                            api = api_init(config)
                            response_dict = get_response(
                                api, cell_ids, lat, lng)

                        for _map_cell in response_dict['responses'][
                                'GET_MAP_OBJECTS']['map_cells']:
                            if 'catchable_pokemons' in _map_cell:
                                for _poke in _map_cell['catchable_pokemons']:
                                    _ecnt[1] += 1
                                    _s = hex(_poke['encounter_id'])
                                    dbc.execute(
                                        "INSERT OR REPLACE INTO encounters (spawn_id, encounter_id, pokemon_id, encounter_time, expire_time) VALUES ('{}','{}',{},{},{})"
                                        "".format(
                                            _poke['spawn_point_id'],
                                            _s.strip('L'), _poke['pokemon_id'],
                                            int(_map_cell[
                                                'current_timestamp_ms'] /
                                                1000),
                                            int(_poke[
                                                'expiration_timestamp_ms'] /
                                                1000)))

                                    if _poke['encounter_id'] in targets:
                                        log.info('Tracked down {}!'.format(
                                            pokenames[_poke['pokemon_id']]))
                                        _remaining -= 1
                                        log.info('%d Pokemon remaining...' %
                                                 _remaining)

                        if _remaining <= 0: break
                        time.sleep(config.delay)
                        _scnt += 1

                db.commit()
                log.info("Encounters: {} coarse, {} fine...".format(*_ecnt))
                time.sleep(config.delay)
                _ccnt += 1

            except NotLoggedInException:
                api = None
                break

        log.info("Rinsing 'n' Repeating...")
Exemple #9
0
def main():
    
    config = init_config()
    if not config:
        log.error('Configuration Error!'); return
        
    db = sqlite3.connect(config.dbfile)
    db_cur = db.cursor()
    db_cur.execute("SELECT cell_id FROM '_queue' WHERE cell_level = %d ORDER BY cell_id" % config.level)
    _tstats = [0, 0, 0, 0]
    
    scan_queque = [x[0] for x in db_cur.fetchall()]
    # http://stackoverflow.com/questions/3614277/how-to-strip-from-python-pyodbc-sql-returns 
    if len(scan_queque) == 0: log.info('Nothing to scan!'); return
       
    api = api_init(config)
    if api == None:   
        log.error('Login failed!'); return
    else:
        log.info('API online! Scan starts in 5sec...')
    time.sleep(5)
            
    for que in scan_queque:    
                
        cell_ids = []
        _content = 0
        _tstats[0] += 1
        _cstats = [0, 0, 0]
        
        log.info('Scan {} of {}.'.format(_tstats[0],(len(scan_queque))))
        
        cell = CellId.from_token(que)
        _ll = CellId.to_lat_lng(cell)
        lat, lng, alt = _ll.lat().degrees, _ll.lng().degrees, 0
        
        if config.test:
            cell_ids = get_cell_ids(lat, lng, 1500)
        else:
            cells = susub_cells(cell)
            cell_ids = sorted([x.id() for x in cells])
        
        try:
            response_dict = get_response(cell_ids, lat, lng, alt, api,config)
        except NotLoggedInException:
            del api; api = api_init(config)
            response_dict = get_response(cell_ids, lat, lng, alt, api,config)  
                
        for _map_cell in response_dict['responses']['GET_MAP_OBJECTS']['map_cells']:
            _cell = CellId(_map_cell['s2_cell_id']).to_token()                        

            if 'forts' in _map_cell:
                for _frt in _map_cell['forts']:
                    if 'gym_points' in _frt:
                        _cstats[0]+=1
                        _type = 0
                        _content = set_bit(_content, 2)
                        db_cur.execute("INSERT OR IGNORE INTO forts (fort_id, cell_id, pos_lat, pos_lng, fort_enabled, fort_type, last_scan) "
                        "VALUES ('{}','{}',{},{},{},{},{})".format(_frt['id'],_cell,_frt['latitude'],_frt['longitude'], \
                        int(_frt['enabled']),0,int(_map_cell['current_timestamp_ms']/1000)))
                    else:
                        _type = 1; _cstats[1]+=1
                        _content = set_bit(_content, 1)
                        db_cur.execute("INSERT OR IGNORE INTO forts (fort_id, cell_id, pos_lat, pos_lng, fort_enabled, fort_type, last_scan) "
                        "VALUES ('{}','{}',{},{},{},{},{})".format(_frt['id'],_cell,_frt['latitude'],_frt['longitude'], \
                        int(_frt['enabled']),1,int(_map_cell['current_timestamp_ms']/1000)))
                                                             
            if 'spawn_points' in _map_cell:
                _content = set_bit(_content, 0)
                for _spwn in _map_cell['spawn_points']:
                    _cstats[2]+=1;
                    spwn_id = CellId.from_lat_lng(LatLng.from_degrees(_spwn['latitude'],_spwn['longitude'])).parent(20).to_token()
                    db_cur.execute("INSERT OR IGNORE INTO spawns (spawn_id, cell_id, pos_lat, pos_lng, last_scan) "
                    "VALUES ('{}','{}',{},{},{})".format(spwn_id,_cell,_spwn['latitude'],_spwn['longitude'],int(_map_cell['current_timestamp_ms']/1000)))
            if 'decimated_spawn_points' in _map_cell:
                _content = set_bit(_content, 0)
                for _spwn in _map_cell['decimated_spawn_points']:
                    _cstats[2]+=1;
                    spwn_id = CellId.from_lat_lng(LatLng.from_degrees(_spwn['latitude'],_spwn['longitude'])).parent(20).to_token()
                    db_cur.execute("INSERT OR IGNORE INTO spawns (spawn_id, cell_id, pos_lat, pos_lng, last_scan) "
                    "VALUES ('{}','{}',{},{},{})".format(spwn_id,_cell,_spwn['latitude'],_spwn['longitude'],int(_map_cell['current_timestamp_ms']/1000)))
                    
            db_cur.execute("INSERT OR IGNORE INTO cells (cell_id, content, last_scan) "
            "VALUES ('{}', {}, {})".format(_cell,_content,int(_map_cell['current_timestamp_ms']/1000)))
            
        _tstats[1] += _cstats[0]; _tstats[2] += _cstats[1]; _tstats[3] += _cstats[2]
        db_cur.execute("DELETE FROM _queue WHERE cell_id='{}'".format(cell.to_token()))
        log.info("Found {} Gyms, {} Pokestops, {} Spawns. Sleeping...".format(*_cstats))
        db.commit()
        time.sleep(int(config.delay))

    log.info('Scanned {} cells; got {} Gyms, {} Pokestops, {} Spawns.'.format(*_tstats))
Exemple #10
0
def main():

    config = init_config()
    if not config:
        return

    if not os.path.isfile('db2.sqlite'):
        log.error('DB not found - please run nestgen.py!')
        return

    watchlist = get_watchlist('watch.txt')
    pokenames = get_pokenames('pokes.txt')

    log.info("DB ok. Loggin' in...")

    api = api_init(config)
    if api == None:
        log.error('Login failed!')
        return
    else:
        log.info('API online! Scan starts in 5sec...')
    time.sleep(5)

    db = sqlite3.connect('db2.sqlite')
    db_cur = db.cursor()

    run = 1
    while run:

        _ccnt = 1

        db_cur.execute(
            "SELECT cell_id FROM queque ORDER BY spawn_count LIMIT %d" %
            config.limit)
        # http://stackoverflow.com/questions/3614277/how-to-strip-from-python-pyodbc-sql-returns
        scan_queque = [x[0] for x in db_cur.fetchall()]

        if len(scan_queque) == 0:
            log.info('Nothing to scan!')
            return

        for queq in scan_queque:

            _ecnt = [0, 0]
            traverse = 0
            targets = []
            cell = CellId.from_token(queq)

            _ll = CellId.to_lat_lng(cell)
            lat, lng, alt = _ll.lat().degrees, _ll.lng().degrees, 0
            cell_ids = [cell.id()]

            try:
                response_dict = get_response(cell_ids, lat, lng, alt, api,
                                             config)
            except NotLoggedInException:
                del api
                api = api_init(config)
                response_dict = get_response(cell_ids, lat, lng, alt, api,
                                             config)

            log.info('Scanning macrocell {} of {}.'.format(
                _ccnt, (len(scan_queque))))

            for _map_cell in response_dict['responses']['GET_MAP_OBJECTS'][
                    'map_cells']:

                if 'nearby_pokemons' in _map_cell:
                    for _poke in _map_cell['nearby_pokemons']:
                        _ecnt[0] += 1
                        _s = hex(_poke['encounter_id'])
                        db_cur.execute(
                            "INSERT OR IGNORE INTO encounters (encounter_id, pokemon_id, encounter_time) VALUES ('{}',{},{})"
                            "".format(
                                _s.strip('L'), _poke['pokemon_id'],
                                int(_map_cell['current_timestamp_ms'] / 1000)))

                        if _poke['pokemon_id'] in watchlist:
                            traverse = 1
                            targets.append(_poke['encounter_id'])
                            log.info('{} nearby!'.format(
                                pokenames[_poke['pokemon_id']]))

                if 'catchable_pokemons' in _map_cell:
                    for _poke in _map_cell['catchable_pokemons']:
                        _ecnt[1] += 1
                        _s = hex(_poke['encounter_id'])
                        db_cur.execute(
                            "INSERT OR REPLACE INTO encounters (spawn_id, encounter_id, pokemon_id, encounter_time, expire_time) VALUES ('{}','{}',{},{},{})"
                            "".format(
                                _poke['spawn_point_id'], _s.strip('L'),
                                _poke['pokemon_id'],
                                int(_map_cell['current_timestamp_ms'] / 1000),
                                int(_poke['expiration_timestamp_ms'] / 1000)))

            db.commit

            if traverse:
                _remaining = len(targets)
                log.info('Narrow search for %d Pokemon...' % len(targets))
                time.sleep(config.delay)

                _scnt = 1
                subcells = susub_cells(cell)
                for _sub in subcells:
                    log.info('Scanning subcell {} of up to 16.'.format(
                        _scnt, (len(scan_queque))))

                    _ll = CellId.to_lat_lng(_sub)
                    lat, lng, alt = _ll.lat().degrees, _ll.lng().degrees, 0
                    cell_ids = get_cell_ids(lat, lng, 100)

                    try:
                        response_dict = get_response(cell_ids, lat, lng, alt,
                                                     api, config)
                    except NotLoggedInException:
                        del api
                        api = api_init(config)
                        response_dict = get_response(cell_ids, lat, lng, alt,
                                                     api, config)

                    for _map_cell in response_dict['responses'][
                            'GET_MAP_OBJECTS']['map_cells']:
                        if 'catchable_pokemons' in _map_cell:
                            for _poke in _map_cell['catchable_pokemons']:
                                _ecnt[1] += 1
                                _s = hex(_poke['encounter_id'])
                                db_cur.execute(
                                    "INSERT OR REPLACE INTO encounters (spawn_id, encounter_id, pokemon_id, encounter_time, expire_time) VALUES ('{}','{}',{},{},{})"
                                    "".format(
                                        _poke['spawn_point_id'], _s.strip('L'),
                                        _poke['pokemon_id'],
                                        int(_map_cell['current_timestamp_ms'] /
                                            1000),
                                        int(_poke['expiration_timestamp_ms'] /
                                            1000)))

                                if _poke['encounter_id'] in targets:
                                    log.info('Tracked down {}!'.format(
                                        pokenames[_poke['pokemon_id']]))
                                    _remaining -= 1
                                    log.info('%d Pokemon remaining...' %
                                             _remaining)

                    if _remaining <= 0: break
                    time.sleep(config.delay)
                    _scnt += 1

            db.commit()
            log.info("Encounters: {} coarse, {} fine...".format(*_ecnt))
            time.sleep(config.delay)
            _ccnt += 1

        log.info("Rinsing 'n' Repeating...")
                     if (dist == 0.0):
                         continue
                     loc[k]['distance'] = dist
         except Exception, e:
             log.msg("No distance recorded in DB for this vehicle")
         result.append(loc)
         data = result
     else:
         data = "NULL"
 else:
     r = yield self.conn.read_absolute(self, [topic], 0)
     n = r['queries'][0]['sample_size']
     if n:
         es = r['queries'][0]['results'][0]['values']
         val = CellId(es[n - 1][1])
         lat = val.to_lat_lng().lat().degrees
         longt = val.to_lat_lng().lng().degrees
         tm = es[n - 1][0] / 1000
         sp = r['queries'][0]['results'][0]['tags']['speed']
         x = len(sp)
         speed = sp[x - 1]
         try:
             sp = r['queries'][0]['results'][0]['tags']['distance']
             x = len(sp)
             dist = sp[x - 1]
         except Exception, e:
             log.msg("No distance recorded in DB for this vehicle")
             dist = -1
         v = {
             "latitude": lat,
             "longitude": longt,