forked from SakulFlee/GarlandTools-PIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
66 lines (54 loc) · 2.82 KB
/
sample.py
File metadata and controls
66 lines (54 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import logging
from pathlib import Path
from typing import TYPE_CHECKING
import aiohttp
from aiohttp_client_cache.session import CachedSession
from async_garlandtools import GarlandToolsAsync as GarlandTools, IconType, Job, Language, Object
if TYPE_CHECKING:
from async_garlandtools._types import GearResponse, ItemResponse
local_data_path: Path = Path(__file__).parent
LOGGER: logging.Logger = logging.getLogger(__name__)
lan = Language.English
session = CachedSession()
# First will be as a context manager.
async def context_sample() -> None:
# Also supports providing your own `aiohttp.ClientSession`;
# but that will not allow the cache to be used unless you make a `CachedSession` object
# from `aiohttp_client_cache.session`.
async with GarlandTools(cache_location=local_data_path, language=lan) as garland_tools:
job = Job.DANCER
gear: GearResponse = await garland_tools.leveling_gear(job=job)
# You can access any relevant information via dict keys.
print(gear["equip"])
item_id = 10373
item: ItemResponse = await garland_tools.item(item_id=item_id)
# You can access any relevant information via dict keys.
print(item["item"], item["ingredients"])
# Also supports providing your own `aiohttp.ClientSession`;
# but that will not allow the cache to be used unless you make a `CachedSession` object
# from `aiohttp_client_cache.session`.
# session = aiohttp.ClientSession()
async def sample() -> None:
# This GarlandTools object will not be able to cache as I provided an `aiohttp.ClientSession()`.
garland_tools = GarlandTools(session=session)
zone = "La Noscea/Lower La Noscea"
map_resp: Object = await garland_tools.map_zone(zone)
# Given `map_zone` used to return a binary PNG, that has been turned into a generic NamedTuple.
# You can access the raw bytes via the `data` attribute.
zone_raw: bytes = map_resp.data
# Maybe you want the direct url, well here ya go.
zone_url: str = map_resp.url
# You can access the original `zone` parameter you passed into the function.
zone_name: str = map_resp.zone
# -------------------------------------------
# Here is how to use the new `icon` endpoint.
# https://www.garlandtools.org/files/icons/achievement/2565.png
icon_id = 2565 # Achievement, "To Crush Your Enemies IV"
icon_type = IconType.achievement
# You have access to the same attributes as before as it's another `Object`.
icon_resp: Object = await garland_tools.icon(icon_id, icon_type)
# We also provide the original Enum to the response object for ease via `icon_type`.
print(icon_resp.url, icon_resp.icon_type)
# Since I provided my own `aiohttp.ClientSession()` I can either close it here via..
await garland_tools.close()
# or leave it open if I am using the Session elsewhere.