專案 08

enka.py

enka.network Python 異步 API wrapper

Table of Contents

  1. Introduction

  2. Usage

  3. Questions, issues, contributions

Introduction

enka.py is an async API wrapper for enka.network written in Python.

[!NOTE]
The wrapper only supports fetching Genshin Impact showcase, for Honkai: Star Rail, use mihomo instead.

Features

  • Fully typed.
  • Provides direct icon URLs.
  • Fully asynchronous by using aiofiles, aiohttp, and asyncio.
  • Supports persistent caching using SQLite.
  • Supports Pydantic V2.
  • Seamlessly integrates with GenshinData.

Installation

# poetry
poetry add git+https://github.com/seriaati/enka-py

# pip
pip install git+https://github.com/seriaati/enka-py

Quick Example

import enka
import asyncio

async def main() -> None:
    async with enka.EnkaAPI() as api:
      response = await api.fetch_showcase(901211014)
      print(response.player.nickname)
      print(response.characters[0].name)

asyncio.run(main())

Full example can be found in example.py

Usage

Starting and closing the client properly

To use the client properly, you can either:
Manually call start() and close()

import enka
import asyncio

async def main() -> None:
    api = enka.EnkaAPI()
    await api.start()
    response = await api.fetch_showcase(901211014)
    await api.close()

asyncio.run(main())

Or use the async with syntax:

import enka
import asyncio

async def main() -> None:
   async with enka.EnkaAPI() as api:
     await api.fetch_showcase(901211014)

asyncio.run(main())

[!IMPORTANT]
You need to call start() or the api client will not function properly; the close() method closes the request session and database properly.

Updating assets

In your first use, enka.py will download all the necessary data files to a directory named .enka_py (if you're using git, you should add it to .gitignore). However, sometimes (often after a game update), the local data will be outdated, and you'd need to update them.
All assets are hosted in enka-py-assets, it uses GitHub action to automatically update data.
To update assets, use the update_assets() method:

import enka
import asyncio

async def main() -> None:
    async with enka.EnkaAPI() as api:
      await api.update_assets()

asyncio.run(main())

[!TIP] If you're running a Discord bot, you can setup a scheduled task that updates the assets everyday.

Client parameters

Currently, the EnkaAPI class allows you to pass in 4 parameters:

Language

This will affect the languages of names of weapon, character, constellations, etc. You can find all the languages here.

Headers

Custom headers used when requesting the Enka API, it is recommended to set a user agent, the default is {"User-Agent": "enka-py"}.

Cache TTL

Default is 60 seconds, the cache is evicted when this time expires. Note that setting longer TTL might result in inconsistent data.

Finding models' attributes

If you're using an IDE like VSCode or Pycharm, then you can see all the attributes and methods the model has in the autocomplete.

[!TIP] If you're using VSCode, alt + left click on the attribute, then the IDE will bring you to the source code of this wrapper, most classes and methods have docstrings.

Catching exceptions

All exception classes can be found in enka/exceptions.py, catch them with try-except.
Example can be found in example.py

Namecards and icons

Available after v1.2.0

Character icons are now Icon objects, you can access all types of icons from it:

The same goes for ShowcaseCharacter.costume_icon and Player.profile_picture_icon.

Namecards are now Namecard objects:

[!WARNING] Player.namecard_icon is renamed to Player.namecard. Also, Character.side_icon, Character.art are removed as they are merged into the Icon class, same for ShowcaseCharacter.costume_art and ShowcaseCharacter.costume_side_icon.

Stats

Available after v1.4.0

Stats refer to character, weapon, and artifact stats.
Internally, stats for characters are FightProp classes, while the others are Stat classes; they can be accessed in the same way, but their types are different (FightPropType and StatType).
For your convenience, there are stat.is_percentage and stat.formatted_value to use, for exmple:

  • If stat.type is StatType.FIGHT_PROP_CUR_ATTACK
    • stat.is_percentage = False
    • stat.formatted_value = '2300'
  • If stat.type is StatType.FIGHT_PROP_CRITICAL
    • stat.is_percentage = True
    • stat.formatted_value = '23.1%'

Constellations

Available after v1.7.0

After v1.7.0, all constellations that belong to a character will appear in Character.constellations, the Constellation.unlocked attribute will indicate whether the constellation is unlocked. Also, there is a new attribute Character.constellations_unlocked that indicates how many constellations the character has unlocked.

Questions, issues, contributions

For questions, you can contact me on Discord or open an issue.
To report issues with this wrapper, open an issue.
To contribute, fork this repo and submit a pull request.

↑↓ 選擇 · Enter 開啟 · Esc 關閉