Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Proposal: Save expiration time of a quest in the db so only non expired quests are being used #43

Open
Bart274 opened this issue Nov 5, 2020 · 1 comment

Comments

@Bart274
Copy link

Bart274 commented Nov 5, 2020

You have the coordinates of a pokestop, based on those coordinates you can get the timezone and determine the local midnight time of the pokestop and know the expiration time of the quest because of it.
This way you can have a clean map, clean set of pokestops after midnight,...

In RocketMapPlusPlus the following logic was used:

                    utcnow_datetime = datetime.utcnow()
                    quest_json = fort_search_response_json["challengeQuest"]["quest"]
                    quest_pokestop = pokestops.get(quest_json["fortId"], Pokestop.get_stop(quest_json["fortId"]))
                    pokestop_timezone_offset = get_timezone_offset(quest_pokestop['latitude'], quest_pokestop['longitude'])
                    pokestop_localtime = utcnow_datetime + timedelta(minutes=pokestop_timezone_offset)
                    next_day_localtime = datetime(
                        year=pokestop_localtime.year,
                        month=pokestop_localtime.month,
                        day=pokestop_localtime.day
                    ) + timedelta(days=1)
                    next_day_utc = next_day_localtime - timedelta(minutes=pokestop_timezone_offset)

for the timezoneoffset, the following code was used:

def get_timezone_offset(lat, lng):
    args = get_args()

    (timezone_offset, status) = get_timezonefinder_timezone_offset(lat, lng)
    if status != "OK":
        (timezone_offset, status) = get_gmaps_timezone_offset(lat, lng, args.gmaps_key)
        if status != "OK":
            timezone_offset = args.quest_timezone_offset

    return timezone_offset


def get_timezonefinder_timezone_offset(lat, lng):
    from pytz import timezone
    import pytz

    utc = pytz.utc

    try:
        today = datetime.now()

        tz = tf.certain_timezone_at(lat=lat, lng=lng)
        if tz is None:
            tz = tf.timezone_at(lat=lat, lng=lng)
            if tz is None:
                tz = tf.closest_timezone_at(lat=lat, lng=lng)
                if tz is None:
                    return (0, "UNKNOWNN_TIMEZONE")

        tz_target = timezone(tz)
        today_target = tz_target.localize(today)
        today_utc = utc.localize(today)

        status = "OK"
        timezone_offset = int((today_utc - today_target).total_seconds() / 60)
    except Exception as e:
        log.exception('Unable to retrieve timezone from timezonefinder: %s.', e)
        status = 'UNKNOWN_ERROR'
        timezone_offset = None

    return (timezone_offset, status)

def get_gmaps_timezone_offset(lat, lng, gmaps_key):
    try:
        r_session = requests.Session()
        response = r_session.get((
            'https://maps.googleapis.com/maps/api/timezone/json?' +
            'location={},{}&timestamp={}&key={}').format(lat, lng, time.time(), gmaps_key),
            timeout=5)
        response = response.json()
        status = response['status']
        timezone_offset = (response.get('rawOffset', 0) + response.get('dstOffset', 0)) / 60
    except Exception as e:
        log.exception('Unable to retrieve timezone from Google APIs: %s.', e)
        status = 'UNKNOWN_ERROR'
        timezone_offset = None

    return (timezone_offset, status)
@Mygod
Copy link
Contributor

Mygod commented Dec 1, 2020

Another way to read timezone could be to use GetPlayerResponse.player_data.time_zone_offset_ms. However, there might be a disconnect from the time GetPlayer is requested until FortSearch is initiated, or if the player is on the borderline of two timezones... Never mind.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants