novelai_api.utils

argon_hash(email: str, password: str, size: int, domain: str) str[source]
get_access_key(email: str, password: str) str[source]
get_encryption_key(email: str, password: str) bytes[source]
decrypt_data(data: str | bytes, key: bytes, nonce: bytes | None = None) Tuple[str, bytes, bool] | Tuple[None, None, bool][source]
encrypt_data(data: str | bytes, key: bytes, nonce: bytes | None = None, is_compressed: bool = False) bytes[source]
decompress_user_data(items: List[Dict[str, Any]] | Dict[str, Any])[source]

Decompress the data of each item in :ref: items Doesn’t decrypt, but does a b64 to UTF8 translation

compress_user_data(items: List[Dict[str, Any]] | Dict[str, Any])[source]

Compress the data of each item in :ref: items Doesn’t encrypt, but does a UTF8 to b64 translation Must have been decompressed by decompress_user_data()

decrypt_user_data(items: List[Dict[str, Any]] | Dict[str, Any], keystore: novelai_api.Keystore.Keystore, uncompress_document: bool = False)[source]

Decrypt the data of each item in :ref: items If an item has already been decrypted, it won’t be decrypted a second time

Parameters:
  • items – Item or list of items to decrypt

  • keystore – Keystore retrieved with the get_keystore method

  • uncompress_document – If True, the document will be decompressed

encrypt_user_data(items: List[Dict[str, Any]] | Dict[str, Any], keystore: novelai_api.Keystore.Keystore)[source]

Encrypt the data of each item in :ref: items If an item has already been encrypted, it won’t be encrypted a second time Must have been decrypted by decrypt_user_data()

Parameters:
  • items – Item or list of items to encrypt

  • keystore – Keystore retrieved with the get_keystore method

link_content_to_story(stories: Dict[str, str | int | Dict[str, Any]], story_contents: List[Dict[str, Any]] | Dict[str, Any])[source]

Link the story content to each story in :ref: stories

unlink_content_from_story(stories: Dict[str, str | int | Dict[str, Any]])[source]

Remove the story content from each story in :ref: stories

get_decrypted_user_data(items: List[Dict[str, Any]]) List[Dict[str, Any]][source]

Filter out items that have not been decrypted

tokens_to_b64(tokens: Iterable[int], token_size: int = 2) str[source]

Encode a list of tokens into a base64 string that can be sent to the API

Parameters:
  • tokens – List of tokens to encode

  • token_size – Size of each token in bytes (Erato: 4, other models: 2)

Returns:

Base64 string representing the tokens

b64_to_tokens(b64: str, token_size: int = 2) List[int][source]

Decode a base64 string returned by the API into a list of tokens

Parameters:
  • b64 – Base64 string to decode

  • token_size – Size of each token in bytes (Erato: 4, other models: 2)

Returns:

List of tokens decoded from the base64 string

extract_preset_data(presets: List[Dict[str, Any]]) Dict[str, novelai_api.Preset.Preset][source]

Transform a list of preset data into a dict of Preset objects indexed by their id

tokenize_if_not(model: novelai_api.Preset.Model, o: str | List[int]) List[int][source]

Tokenize the string if it is not already tokenized

Parameters:
  • model – Model to use for tokenization

  • o – String to tokenize or list of tokens

Returns:

List of tokens if ‘o’ is a string, ‘o’ otherwise

async gather_asyncgenerator(agen: AsyncGenerator[Any, None] | AsyncIterable[Any]) List[Any][source]

Gather all the items of an async generator into a list