Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New data updating ~20 seconds late #75

Open
ChicknTurtle opened this issue May 4, 2024 · 0 comments
Open

New data updating ~20 seconds late #75

ChicknTurtle opened this issue May 4, 2024 · 0 comments

Comments

@ChicknTurtle
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant