-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from LlmKira/dev
feat:LoginCredential | user/login
- Loading branch information
Showing
23 changed files
with
554 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# NOTICE OF THIS PROJECT | ||
|
||
## MIT License | ||
|
||
The MIT license applies to the files in: | ||
|
||
file: "src/novelai_python/sdk/ai/generate_image.py" from https://github.com/HanaokaYuzu/NovelAI-API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/2/7 下午12:07 | ||
# @Author : sudoskys | ||
# @File : login.py | ||
# @Software: PyCharm | ||
import asyncio | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
from loguru import logger | ||
|
||
from novelai_python import APIError | ||
from novelai_python import Login, LoginResp | ||
|
||
load_dotenv() | ||
|
||
|
||
async def main(): | ||
try: | ||
_res = await Login.build(user_name=os.getenv("NOVELAI_USER"), password=os.getenv("NOVELAI_PASS") | ||
).request() | ||
except APIError as e: | ||
logger.exception(e) | ||
print(e.__dict__) | ||
return | ||
|
||
_res: LoginResp | ||
print(_res) | ||
print(f"Access Token: {_res.accessToken}") | ||
|
||
|
||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "novelai-python" | ||
version = "0.1.8" | ||
version = "0.1.9" | ||
description = "Novelai Python Binding With Pydantic" | ||
authors = [ | ||
{ name = "sudoskys", email = "[email protected]" }, | ||
|
@@ -17,6 +17,7 @@ dependencies = [ | |
"fastapi>=0.109.0", | ||
"uvicorn[standard]>=0.27.0.post1", | ||
"numpy>=1.24.4", | ||
"argon2-cffi>=23.1.0", | ||
] | ||
requires-python = ">=3.8" | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/2/7 上午11:57 | ||
# @Author : sudoskys | ||
# @File : login.py | ||
# @Software: PyCharm | ||
from pydantic import BaseModel | ||
|
||
|
||
class LoginResp(BaseModel): | ||
accessToken: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2024/2/7 下午12:14 | ||
# @Author : sudoskys | ||
# @File : UserAuth.py | ||
# @Software: PyCharm | ||
import time | ||
from typing import Optional | ||
|
||
from curl_cffi.requests import AsyncSession | ||
from pydantic import SecretStr, Field | ||
|
||
from ._base import CredentialBase | ||
|
||
|
||
class LoginCredential(CredentialBase): | ||
""" | ||
JwtCredential is the base class for all credential. | ||
""" | ||
username: str = Field(None, description="username") | ||
password: SecretStr = Field(None, description="password") | ||
_session: Optional[AsyncSession] = None | ||
_update_at: Optional[int] = None | ||
|
||
async def get_session(self, timeout: int = 180): | ||
# 30 天有效期 | ||
if not self._session or int(time.time()) - self._update_at > 29 * 24 * 60 * 60: | ||
from ..sdk import Login | ||
resp = await Login.build(user_name=self.username, password=self.password.get_secret_value()).request() | ||
self._session = AsyncSession(timeout=timeout, headers={ | ||
"Authorization": f"Bearer {resp.accessToken}", | ||
"Content-Type": "application/json", | ||
"Origin": "https://novelai.net", | ||
"Referer": "https://novelai.net/", | ||
}, impersonate="chrome110") | ||
self._update_at = int(time.time()) | ||
return self._session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.