Source code for buff163_unofficial_api.buff163_unofficial_api

import logging
from typing import Iterator, Callable, Union
from buff163_unofficial_api.rest_adapter import RestAdapter
from buff163_unofficial_api.models import *
from buff163_unofficial_api.cs_enums import *


[docs] class Buff163API: def __init__( self, hostname: str = "buff.163.com/api", session_cookie: str = "", ssl_verify: bool = True, logger: logging.Logger = None, page_size: int = 20, ): """Buff163API default constructor. Args: hostname (str, optional): API url. Defaults to "buff.163.com/api". session_cookie (str, optional): Personal session cookie (like an api token). Defaults to "". ssl_verify (bool, optional): Set to false if having SSL/TLS cert validation issues. Defaults to True. logger (logging.Logger, optional): App logger. Defaults to None. page_size (int, optional): Items per page. Defaults to 20. """ self._rest_adapter = RestAdapter(hostname, session_cookie, ssl_verify, logger) self._page_size = page_size
[docs] def get_item_market( self, category: Union[Knife, Gun, Glove, Agent, Sticker, OtherItem], pageNum: int = 1, ) -> List[Item]: """Get specific item's market page. Args: category (enum): the specific category of cs items. pageNum (int, optional): Which page number to get. Defaults to 1. Returns: List[Item]: List of overview of items. """ if not isinstance(category, Enum): raise TypeError("Category must be an instance of an Enum.") result = self._rest_adapter.get( endpoint=f"/market/goods?game=csgo&page_num={pageNum}&category={category.value}" ) market = [Item(**item) for item in result.data["data"]["items"]] return market
[docs] def fetch_image_data(self, item: Item): """Fetches Item icon. Args: item (Item): Item from market. """ item.data = self._rest_adapter.fetch_data(url=item.goods_info.icon_url)
def _page( self, endpoint: str, model: Callable[..., Model], max_amt: int = 80 ) -> Iterator[Model]: """Pages through set number of pages. Args: endpoint (str): API endpoint requested. model (Callable[..., Model]): Specific model that will be paged. max_amt (int, optional): Max items to get from pages. Defaults to 80. Yields: Iterator[Model]: List of specific model. """ amt_yielded = 0 curr_page = last_page = 1 ep_params = { "game": "csgo", "page_size": self._page_size, } # Keep fetching pages until the last page while curr_page <= last_page: ep_params["page_num"] = curr_page result = self._rest_adapter.get(endpoint=endpoint, ep_params=ep_params) data = result.data["data"] # Increment curr_page by 1 and update the last_page based on header info returned last_page = data["total_page"] curr_page = data["page_num"] + 1 for datam in data["items"]: yield model(**datam) amt_yielded += 1 if amt_yielded >= max_amt: last_page = 0 break
[docs] def get_item(self, item_id: int) -> SpecificItem: """Gets the description/details of an item. Args: item_id (int): Specific item's goods_id. Returns: SpecificItem: Specific item. """ result = self._rest_adapter.get( endpoint=f"/market/goods/info?game=csgo&goods_id={item_id}" ) return SpecificItem(**result.data["data"])