Skip to content
This repository was archived by the owner on Jan 18, 2021. It is now read-only.
This repository was 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

Description

@Bart274

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions