Aedial / novelai-api

Python API for the NovelAI REST API
https://aedial.github.io/novelai-api/
MIT License
132 stars 15 forks source link

novelai run in class error #5

Closed MrMirhan closed 2 years ago

MrMirhan commented 2 years ago

I'm trying to create Class for run functions in novella inside of flask application. But I have an error.

Traceback (most recent call last):
  File "/Users/mirhanmac/Desktop/Projects/noveapi/app.py", line 15, in <module>
    webServer = WebServer(flaskLogger, env, novelApi)
  File "/Users/mirhanmac/Desktop/Projects/noveapi/web.py", line 13, in __init__
    self.novelApi.run()
  File "/Users/mirhanmac/Desktop/Projects/noveapi/novel.py", line 26, in run
    asyncio.run(self.main())
  File "/opt/homebrew/Cellar/python@3.9/3.9.13_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/opt/homebrew/Cellar/python@3.9/3.9.13_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/Users/mirhanmac/Desktop/Projects/noveapi/novel.py", line 22, in main
    keystore = await api.high_level.get_keystore(key)
  File "/Users/mirhanmac/Desktop/Projects/noveapi/novelapi/lib/python3.9/site-packages/novelai_api/_high_level.py", line 78, in get_keystore
    keystore.decrypt(key)
  File "/Users/mirhanmac/Desktop/Projects/noveapi/novelapi/lib/python3.9/site-packages/novelai_api/Keystore.py", line 90, in decrypt
    keystore = loads(b64decode(self.data["keystore"]).decode())
KeyError: 'keystore'

This is my code:

from novelai_api import NovelAI_API
from novelai_api.utils import get_encryption_key, decrypt_user_data
from aiohttp import ClientSession
import asyncio

class NovelAPI:
    def __init__(self, logger, env):
        if "NAI_USERNAME" not in env or "NAI_PASSWORD" not in env:
            raise RuntimeError("Please ensure that NAI_USERNAME and NAI_PASSWORD are set in your environment")
        self.logger = logger
        self.username = env["NAI_USERNAME"]
        self.password = env["NAI_PASSWORD"]

    async def main(self):
        async with ClientSession() as session:
            api = NovelAI_API(session, logger = self.logger)
            login = await api.high_level.login(self.username, self.password)
            self.logger.info('Bearer ' + login)

            key = get_encryption_key(self.username, self.password)
            keystore = await api.high_level.get_keystore(key)
            self.logger.info(keystore)

    def run(self):
        asyncio.run(self.main())
Aedial commented 2 years ago

That's... peculiar. Has no story been ever created on this account ? It could be that the keystore never got initialized or has been initialized wrongly. It should at the very least be in the form {"keystore": null} as per https://api.novelai.net/docs/ (/user/keystore and GetKeystoreResponse), unless the documentation is outdated... I get no problem with my test account, so it might be a problem with yours. You can see the content by using the low level get_keystore function, but I suspect it will be {}.

Aedial commented 2 years ago

This issue has not received update for a whole month. As such, I will close it. Reopen it if you find anything that seems reproducible.