misenhower / splatoon3.ink

🦑 Splatoon 3 map rotations, Salmon Run schedules, SplatNet gear, and more
https://splatoon3.ink
MIT License
229 stars 21 forks source link

New data updating ~20 seconds late #75

Open ChicknTurtle opened 5 months ago

ChicknTurtle commented 5 months ago

I wrote a python script (part of a twitch bot) that will update the data if the current time is greater than the end time from the data. This works, but it fetches the data around 20 times instead of once. I fixed this by adding 20 seconds to the end time.

Here's my code:

async def fetchdata():
    splatnet_schedules = "https://splatoon3.ink/data/schedules.json"
    log("Fetching new data from https://splatoon3.ink/...")
    async with aiohttp.ClientSession() as session:
        async with session.get(splatnet_schedules) as response:
            if response.status == 200:
                bot.schedules = await response.json()
                with open('data/schedules.json', 'w') as file:
                    json.dump(bot.schedules, file)
            else:
                log.error("Failed to retrieve schedules!")
                # wait 5 mins before trying again
                await asyncio.sleep(5*60)

async def request_loop():
    # load data from file, or get new data
    with open('data/schedules.json', 'r') as file:
        try:
            bot.schedules = json.load(file)
            log("Loaded data from file.")
        except:
            await fetchdata()
    # check if it's time to fetch new data every few seconds
    while True:
        nextrotation = datetime.datetime.fromisoformat(bot.schedules['data']['regularSchedules']['nodes'][0]['endTime'])
        nextrotation += datetime.timedelta(seconds=20)
        if datetime.datetime.now(datetime.timezone.utc) > nextrotation:
            await fetchdata()
        await asyncio.sleep(1)