Mikubill / pixivpy-async

Pure Python 3 Async Pixiv API
The Unlicense
149 stars 18 forks source link

Client regularly stops responding to requests #2

Closed Tempystral closed 4 years ago

Tempystral commented 4 years ago

I'm building a discord bot which periodically accesses Pixiv using this API and it works great... for a few hours. Every time we restart the bot, it logs in and locates images just fine, but then it mysteriously stops working.

I suspect this is because we require a new token, but it's not clear how to obtain one when the old token expires. We're currently logging in using a username and password.

Would you be able to shed any light on this matter? I've been looking for documentation on the Pixiv API but it's rather sparse.

Mikubill commented 4 years ago

You can use api.login () to refresh the token for a period of time (such as two hours) after login.

async with PixivClient() as client:
    aapi = AppPixivAPI(client=client)
    await aapi.login(username, password)
    ...
    await aapi.login() # to refresh token

or use a function to complete this operation regularly

async def TokenRefresh(api):
    while True:
        await api.login()
        await asycnio.sleep(7200)
Tempystral commented 4 years ago

Oh, I see. I hadn't realized it was that simple. Thanks a bunch!