Beispiel #1
0
def get_stats():
    cache_valid = (
        CACHE['data'] and
        CACHE['generated_at'] > datetime.now() - timedelta(minutes=15)
    )
    if cache_valid:
        return CACHE['data']
    session = db.Session()
    forts = db.get_forts(session)
    session.close()
    count = {t.value: 0 for t in db.Team}
    strongest = {t.value: None for t in db.Team}
    guardians = {t.value: {} for t in db.Team}
    top_guardians = {t.value: None for t in db.Team}
    percentages = {}
    last_date = 0
    for fort in forts:
        if fort['last_modified'] > last_date:
            last_date = fort['last_modified']
        team = fort['team']
        count[team] = count[team] + 1
        if team != 0:
            # Strongest gym
            existing = strongest[team]
            should_replace = (
                existing is not None and
                fort['prestige'] > existing[0] or
                existing is None
            )
            pokemon_id = fort['guard_pokemon_id']
            if should_replace:
                strongest[team] = (
                    fort['prestige'],
                    pokemon_id,
                    pokemon_names[str(pokemon_id)],
                )
            # Guardians
            guardian_value = guardians[team].get(pokemon_id, 0)
            guardians[team][pokemon_id] = guardian_value + 1
    for team in db.Team:
        percentages[team.value] = count.get(team.value) / len(forts) * 100
        if guardians[team.value]:
            pokemon_id = sorted(
                guardians[team.value],
                key=guardians[team.value].__getitem__,
                reverse=True
            )[0]
            top_guardians[team.value] = pokemon_names[str(pokemon_id)]
    CACHE['generated_at'] = datetime.now()
    CACHE['data'] = {
        'order': sorted(count, key=count.__getitem__, reverse=True),
        'count': count,
        'total_count': len(forts),
        'strongest': strongest,
        'percentages': percentages,
        'last_date': last_date,
        'top_guardians': top_guardians,
        'generated_at': CACHE['generated_at'],
    }
    return CACHE['data']
Beispiel #2
0
def get_pokemarkers():
    markers = []
    session = db.Session()
    
    if args.after == 0: 
        pokemons = db.get_sightings(session)
    else:
	pokemons = db.get_sightings_after(session, args.after)
    
    forts = db.get_forts(session)
    pokestops = db.get_pokestops(session)

    session.close()

    for pokemon in pokemons:
        markers.append({
            'id': 'pokemon-{}'.format(pokemon.id),
            'type': 'pokemon',
            'trash': pokemon.pokemon_id in config.TRASH_IDS,
            'name': POKEMON_NAMES[pokemon.pokemon_id],
            'pokemon_id': pokemon.pokemon_id,
            'lat': pokemon.lat,
            'lon': pokemon.lon,
            'expires_at': pokemon.expire_timestamp,
        })
    for fort in forts:
        if fort['guard_pokemon_id']:
            pokemon_name = POKEMON_NAMES[fort['guard_pokemon_id']]
        else:
            pokemon_name = 'Empty'
        markers.append({
            'id': 'fort-{}'.format(fort['fort_id']),
            'sighting_id': fort['id'],
            'type': 'fort',
            'prestige': fort['prestige'],
            'pokemon_id': fort['guard_pokemon_id'],
            'pokemon_name': pokemon_name,
            'team': fort['team'],
            'lat': fort['lat'],
            'lon': fort['lon'],
        })

    for pokestop in pokestops:
        markers.append({
            'id': 'stop-{}'.format(pokestop['id']),
            'type': 'pokestop',
            'lat': pokestop['lat'],
            'lon': pokestop['lon'],
        })
        

    return markers
Beispiel #3
0
def get_pokemarkers():
    markers = []
    session = db.Session()
    pokemons = db.get_sightings(session)
    forts = db.get_forts(session)
    stops = db.get_stops(session)
    session.close()

    for pokemon in pokemons:
        markers.append({
            'id': 'pokemon-{}'.format(pokemon.id),
            'type': 'pokemon',
            'trash': pokemon.pokemon_id in config.TRASH_IDS,
            'name': pokemon_names[str(pokemon.pokemon_id)],
            'pokemon_id': pokemon.pokemon_id,
            'lat': pokemon.lat,
            'lon': pokemon.lon,
            'expires_at': pokemon.expire_timestamp,
        })
    for fort in forts:
        if fort['guard_pokemon_id']:
            pokemon_name = pokemon_names[str(fort['guard_pokemon_id'])]
        else:
            pokemon_name = 'Empty'
        markers.append({
            'id': 'fort-{}'.format(fort['fort_id']),
            'sighting_id': fort['id'],
            'type': 'fort',
            'prestige': fort['prestige'],
            'pokemon_id': fort['guard_pokemon_id'],
            'pokemon_name': pokemon_name,
            'team': fort['team'],
            'lat': fort['lat'],
            'lon': fort['lon'],
        })
    for stop in stops:
        if stop['active_pokemon_id'] != 0:
            pokemon_name = pokemon_names[str(stop['active_pokemon_id'])]
        else:
            pokemon_name = 'No Lure'
        markers.append({
            'id': 'stop-{}'.format(stop['stop_id']),
            'sighting_id': stop['id'],
            'type': 'stop',
            'lure_expires_timestamp': stop['lure_expires_timestamp_ms'],
            'pokemon_id': stop['active_pokemon_id'],
            'pokemon_name': pokemon_name,
            'lat': stop['lat'],
            'lon': stop['lon'],            
        })
    return markers
Beispiel #4
0
def get_pokemarkers():
    markers = []
    session = db.Session(autoflush=False)
    pokemons = db.get_sightings(session)
    forts = db.get_forts(session)
    session.close()

    for pokemon in pokemons:
        markers.append({
            'id': 'pokemon-{}'.format(pokemon.id),
            'type': 'pokemon',
            'trash': pokemon.pokemon_id in config.TRASH_IDS,
            'name': POKEMON_NAMES[pokemon.pokemon_id],
            'pokemon_id': pokemon.pokemon_id,
            'lat': pokemon.lat,
            'lon': pokemon.lon,
            'expires_at': pokemon.expire_timestamp,
        })
    for fort in forts:
        if fort['guard_pokemon_id']:
            pokemon_name = POKEMON_NAMES[fort['guard_pokemon_id']]
        else:
            pokemon_name = 'Empty'
        markers.append({
            'id': 'fort-{}'.format(fort['fort_id']),
            'sighting_id': fort['id'],
            'type': 'fort',
            'prestige': fort['prestige'],
            'pokemon_id': fort['guard_pokemon_id'],
            'pokemon_name': pokemon_name,
            'team': fort['team'],
            'lat': fort['lat'],
            'lon': fort['lon'],
        })

    if config.MAP_WORKERS:
        # Worker stats
        markers.extend(get_worker_markers())
    return markers
Beispiel #5
0
def get_pokemarkers():
    markers = []
    session = db.Session()
    pokemons = db.get_sightings(session)
    forts = db.get_forts(session)
    session.close()

    for pokemon in pokemons:
        markers.append({
            'id': 'pokemon-{}'.format(pokemon.id),
            'type': 'pokemon',
            'trash': pokemon.pokemon_id in config.TRASH_IDS,
            'name': POKEMON_NAMES[pokemon.pokemon_id],
            'pokemon_id': pokemon.pokemon_id,
            'lat': pokemon.lat,
            'lon': pokemon.lon,
            'expires_at': pokemon.expire_timestamp,
        })
    for fort in forts:
        if fort['guard_pokemon_id']:
            pokemon_name = POKEMON_NAMES[fort['guard_pokemon_id']]
        else:
            pokemon_name = 'Empty'
        markers.append({
            'id': 'fort-{}'.format(fort['fort_id']),
            'sighting_id': fort['id'],
            'type': 'fort',
            'prestige': fort['prestige'],
            'pokemon_id': fort['guard_pokemon_id'],
            'pokemon_name': pokemon_name,
            'team': fort['team'],
            'lat': fort['lat'],
            'lon': fort['lon'],
        })

    return markers
Beispiel #6
0
def get_pokemarkers():
    markers = []
    session = db.Session()
    pokemons = db.get_sightings(session)
    forts = db.get_forts(session)
    session.close()
    global email_sent
    global tweets_sent
    TWITTER_1 = 0
    TWITTER_2 = 0
    TWITTER_3 = 0
    TWITTER_4 = 0
    PASSWORD = 0
    for pokemon in pokemons:
        markers.append({
            'id': 'pokemon-{}'.format(pokemon.id),
            'type': 'pokemon',
            'trash': pokemon.pokemon_id in config.TRASH_IDS,
            'name': POKEMON_NAMES[pokemon.pokemon_id],
            'pokemon_id': pokemon.pokemon_id,
            'lat': pokemon.lat,
            'lon': pokemon.lon,
            'expires_at': pokemon.expire_timestamp,
        })
        name = POKEMON_NAMES[pokemon.pokemon_id]
        datestr = datetime.fromtimestamp(pokemon.expire_timestamp)
        dateoutput = datestr.strftime("%H:%M:%S")
        am_or_pm = " AM"
        temp_dateoutput = datestr.strftime("%H:%M:%S")
        time_now = datetime.now()
        diff_in_time = datestr - time_now
        diff_in_time_str_useless = str(diff_in_time).split(".")
        diff_in_time_str = diff_in_time_str_useless[0].split(":")
        diff_in_time_int = [int(i) for i in diff_in_time_str]
        mins = ''
        secs = ''
        if diff_in_time_int[1] is 1:
            mins = 'min '
        else:
            mins = 'mins '
        if diff_in_time_int[2] is 1:
            secs = 'sec '
        else:
            secs = 'secs '
        time_left_str = str(diff_in_time_int[1]) + mins + str(
            diff_in_time_int[2]) + secs
        mod_date = temp_dateoutput.split(":")
        mod_date_int = int(mod_date[0])
        if mod_date_int > 24:
            mod_date_int = mod_date_int - 24
            am_or_pm = " AM"
        elif mod_date_int == 24:
            mod_date_int = mod_date_int - 12
            am_or_pm = " AM"
        elif mod_date_int > 12:
            mod_date_int = mod_date_int - 12
            am_or_pm = " PM"
        elif mod_date_int == 12:
            am_or_pm = " PM"
        actual_dateoutput = str(
            mod_date_int) + ":" + mod_date[1] + ":" + mod_date[2] + am_or_pm
        new_lat = "%.8f" % float(pokemon.lat)
        new_lon = "%.8f" % float(pokemon.lon)

        missing = [
            2, 3, 5, 6, 24, 28, 38, 51, 53, 57, 62, 65, 67, 68, 76, 78, 82, 83,
            85, 89, 101, 103, 107, 112, 113, 114, 115, 122, 130, 132, 139, 141,
            144, 145, 146, 149, 150, 151
        ]
        rare = [
            2, 3, 5, 6, 9, 24, 26, 28, 31, 34, 38, 40, 51, 53, 57, 59, 62, 65,
            67, 68, 71, 76, 78, 82, 83, 84, 85, 87, 88, 89, 91, 94, 101, 103,
            105, 106, 107, 110, 112, 113, 114, 115, 121, 122, 126, 130, 131,
            132, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
            146, 148, 149, 150, 151
        ]
        rare_alert = ""
        if pokemon.pokemon_id in rare:
            rare_alert = "Rare Alert! "
        if pokemon.pokemon_id in missing and pokemon.expire_timestamp not in email_sent:
            msg = MIMEMultipart()
            msg['Subject'] = rare_alert + str(name) + " here 'til " + str(
                actual_dateoutput) + ' with ' + time_left_str + 'left'
            msg['From'] = "*****@*****.**"
            msg['To'] = "*****@*****.**"
            message_to_send = 'https://www.google.com/maps/dir/Current+Location/' + str(
                pokemon.lat) + ',' + str(pokemon.lon)
            part1 = MIMEText(message_to_send, 'plain')
            msg.attach(part1)
            print msg.as_string()
            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.ehlo()
            server.starttls()
            server.login("*****@*****.**", PASSWORD)
            server.sendmail("*****@*****.**", "*****@*****.**",
                            msg.as_string())
            email_sent.append(pokemon.expire_timestamp)
        tweet_pokemon = [
            2, 3, 5, 6, 8, 9, 24, 26, 27, 28, 31, 34, 36, 37, 38, 40, 45, 50,
            51, 53, 55, 57, 59, 61, 62, 64, 65, 67, 68, 71, 73, 75, 76, 78, 80,
            81, 82, 83, 84, 85, 87, 88, 89, 91, 94, 95, 99, 101, 103, 104, 105,
            106, 107, 108, 110, 112, 113, 114, 115, 117, 121, 122, 123, 126,
            127, 130, 131, 132, 134, 135, 136, 137, 138, 139, 140, 141, 142,
            143, 144, 145, 146, 148, 149, 150, 151
        ]
        if pokemon.pokemon_id in tweet_pokemon and pokemon.expire_timestamp not in tweets_sent:
            message_to_send = rare_alert + str(name) + " here until " + str(
                actual_dateoutput
            ) + ' with ' + time_left_str + 'left! https://www.google.com/maps/dir/Current+Location/' + str(
                new_lat) + ',' + str(new_lon)
            auth = tweepy.OAuthHandler(TWITTER_1, TWITTER_2)
            auth.set_access_token(TWITTER_3, TWITTER_3)
            api = tweepy.API(auth)
            try:
                api.update_status(status=message_to_send)
                print message_to_send
                print len(message_to_send)
            except:
                print message_to_send
            tweets_sent.append(pokemon.expire_timestamp)
    for fort in forts:
        if fort['guard_pokemon_id']:
            pokemon_name = POKEMON_NAMES[fort['guard_pokemon_id']]
        else:
            pokemon_name = 'Empty'
        markers.append({
            'id': 'fort-{}'.format(fort['fort_id']),
            'sighting_id': fort['id'],
            'type': 'fort',
            'prestige': fort['prestige'],
            'pokemon_id': fort['guard_pokemon_id'],
            'pokemon_name': pokemon_name,
            'team': fort['team'],
            'lat': fort['lat'],
            'lon': fort['lon'],
        })

    return markers
Beispiel #7
0
def get_stats():
    cache_valid = (
        CACHE['data'] and
        CACHE['generated_at'] > datetime.now() - timedelta(minutes=15)
    )
    if cache_valid:
        return CACHE['data']
    session = db.Session()
    forts = db.get_forts(session)
    session.close()
    count = {t.value: 0 for t in db.Team}
    strongest = {t.value: None for t in db.Team}
    guardians = {t.value: {} for t in db.Team}
    top_guardians = {t.value: None for t in db.Team}
    prestige = {t.value: 0 for t in db.Team}
    percentages = {}
    prestige_percent = {}
    total_prestige = 0
    last_date = 0
    for fort in forts:
        if fort['last_modified'] > last_date:
            last_date = fort['last_modified']
        team = fort['team']
        count[team] = count[team] + 1
        if team != 0:
            # Strongest gym
            existing = strongest[team]
            should_replace = (
                existing is not None and
                fort['prestige'] > existing[0] or
                existing is None
            )
            pokemon_id = fort['guard_pokemon_id']
            if should_replace:
                strongest[team] = (
                    fort['prestige'],
                    pokemon_id,
                    POKEMON_NAMES[pokemon_id],
                )
            # Guardians
            guardian_value = guardians[team].get(pokemon_id, 0)
            guardians[team][pokemon_id] = guardian_value + 1
            # Prestige
            prestige[team] += fort['prestige']
    total_prestige = sum(prestige.values())
    for team in db.Team:
        # TODO: remove float(...) as soon as we move to Python 3
        percentages[team.value] = (
            count.get(team.value) / float(len(forts)) * 100
        )
        prestige_percent[team.value] = (
            prestige.get(team.value) / float(total_prestige) * 100
        )
        if guardians[team.value]:
            pokemon_id = sorted(
                guardians[team.value],
                key=guardians[team.value].__getitem__,
                reverse=True
            )[0]
            top_guardians[team.value] = POKEMON_NAMES[pokemon_id]
    CACHE['generated_at'] = datetime.now()
    CACHE['data'] = {
        'order': sorted(count, key=count.__getitem__, reverse=True),
        'count': count,
        'total_count': len(forts),
        'strongest': strongest,
        'prestige': prestige,
        'prestige_percent': prestige_percent,
        'percentages': percentages,
        'last_date': last_date,
        'top_guardians': top_guardians,
        'generated_at': CACHE['generated_at'],
    }
    return CACHE['data']
Beispiel #8
0
def get_stats():
    cache_valid = (
        CACHE['data']
        and CACHE['generated_at'] > datetime.now() - timedelta(minutes=15))
    if cache_valid:
        return CACHE['data']
    session = db.Session()
    forts = db.get_forts(session)
    session.close()
    count = {t.value: 0 for t in db.Team}
    strongest = {t.value: None for t in db.Team}
    guardians = {t.value: {} for t in db.Team}
    top_guardians = {t.value: None for t in db.Team}
    prestige = {t.value: 0 for t in db.Team}
    percentages = {}
    prestige_percent = {}
    total_prestige = 0
    last_date = 0
    for fort in forts:
        if fort['last_modified'] > last_date:
            last_date = fort['last_modified']
        team = fort['team']
        count[team] = count[team] + 1
        if team != 0:
            # Strongest gym
            existing = strongest[team]
            should_replace = (existing is not None
                              and fort['prestige'] > existing[0]
                              or existing is None)
            pokemon_id = fort['guard_pokemon_id']
            if should_replace:
                strongest[team] = (
                    fort['prestige'],
                    pokemon_id,
                    POKEMON_NAMES[pokemon_id],
                )
            # Guardians
            guardian_value = guardians[team].get(pokemon_id, 0)
            guardians[team][pokemon_id] = guardian_value + 1
            # Prestige
            prestige[team] += fort['prestige']
    total_prestige = sum(prestige.values())
    for team in db.Team:
        # TODO: remove float(...) as soon as we move to Python 3
        percentages[team.value] = (count.get(team.value) / float(len(forts)) *
                                   100)
        prestige_percent[team.value] = (prestige.get(team.value) /
                                        float(total_prestige) * 100)
        if guardians[team.value]:
            pokemon_id = sorted(guardians[team.value],
                                key=guardians[team.value].__getitem__,
                                reverse=True)[0]
            top_guardians[team.value] = POKEMON_NAMES[pokemon_id]
    CACHE['generated_at'] = datetime.now()
    CACHE['data'] = {
        'order': sorted(count, key=count.__getitem__, reverse=True),
        'count': count,
        'total_count': len(forts),
        'strongest': strongest,
        'prestige': prestige,
        'prestige_percent': prestige_percent,
        'percentages': percentages,
        'last_date': last_date,
        'top_guardians': top_guardians,
        'generated_at': CACHE['generated_at'],
    }
    return CACHE['data']