From 27cbac0a4c089df4eb641eb5d2976b69bd21a04a Mon Sep 17 00:00:00 2001 From: hldh214 Date: Mon, 26 Feb 2024 16:20:47 +0900 Subject: [PATCH] Refactor Buff constructor to take full config object The constructor for the Buff class has been revised to take in the full config object instead of individual parameters. The constructor's parameters (request_interval and request_kwargs) have been replaced with a single config parameter. This helps to simplify code and improve the maintainability. Also, a new attribute 'sort_by' is added to the Buff class and the relevant configuration is provided(#28). Signed-off-by: hldh214 --- README.md | 1 + buff2steam/__main__.py | 3 +-- buff2steam/provider/buff.py | 35 ++++++++++++++++++++--------------- buff_scanner.py | 2 +- config.sample.json | 1 + 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index ab474db..642237f 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ docker run -it --name buff2steam --rm -v $PWD/config.json:/app/config.json ghcr. "webgui_refresh_time": 1 // update time of webgui in seconds or false, for none }, "buff": { + "sort_by": "sell_num.desc", // sort by Quantity (descending), Available options: null, "price.asc", "price.desc", "sell_num.desc" "request_interval": 4, // buff api request interval (in seconds) "requests_kwargs": { "headers": { diff --git a/buff2steam/__main__.py b/buff2steam/__main__.py index 9b65dac..2f52618 100644 --- a/buff2steam/__main__.py +++ b/buff2steam/__main__.py @@ -76,8 +76,7 @@ async def main(): async with Buff( game=config['main']['game'], game_appid=config['main']['game_appid'], - request_interval=config['buff']['request_interval'], - request_kwargs=config['buff']['requests_kwargs'], + config=config['buff'], ) as buff, Steam( game_appid=config['main']['game_appid'], request_interval=config['steam']['request_interval'], diff --git a/buff2steam/provider/buff.py b/buff2steam/provider/buff.py index 4b86541..c8223da 100644 --- a/buff2steam/provider/buff.py +++ b/buff2steam/provider/buff.py @@ -21,15 +21,15 @@ class Buff: csrf_pattern = re.compile(r'name="csrf_token"\s*content="(.+?)"') - def __init__(self, game='dota2', game_appid=570, request_interval=10, request_kwargs=None): - if request_kwargs is None: - request_kwargs = {} - - self.request_interval = request_interval - self.request_locks = {} # {url: [asyncio.Lock, last_request_time]} - self.request_kwargs = request_kwargs + def __init__(self, game, game_appid, config): self.game = game self.game_appid = game_appid + + self.request_interval = config.get('request_interval', 4) + self.request_kwargs = config.get('requests_kwargs', {}) + self.sort_by = config.get('sort_by', None) + + self.request_locks = {} # {url: [asyncio.Lock, last_request_time]} self.opener = httpx.AsyncClient(base_url=self.base_url, **self.request_kwargs) async def __aenter__(self): @@ -56,18 +56,23 @@ async def request(self, *args, **kwargs) -> dict: return response.json()['data'] - async def get_total_page(self): - response = await self.request('get', self.web_goods, params={ - 'page_num': 1, + def _build_web_goods_params(self, page_num): + params = { + 'page_num': page_num, 'game': self.game - }) + } + + if self.sort_by: + params['sort_by'] = self.sort_by + + return params + + async def get_total_page(self): + response = await self.request('get', self.web_goods, params=self._build_web_goods_params(1)) return response.get('total_page') async def get_items(self, page): - response = await self.request('get', self.web_goods, params={ - 'page_num': page, - 'game': self.game - }) + response = await self.request('get', self.web_goods, params=self._build_web_goods_params(page)) return response.get('items') diff --git a/buff_scanner.py b/buff_scanner.py index 64c93ca..be71c0f 100644 --- a/buff_scanner.py +++ b/buff_scanner.py @@ -32,7 +32,7 @@ async def main(): buff = Buff( config['main']['game'], config['main']['game_appid'], - config['buff']['requests_kwargs'] + config['buff'] ) total_page = await buff.get_total_page() diff --git a/config.sample.json b/config.sample.json index 05d8a8d..2e6c67f 100644 --- a/config.sample.json +++ b/config.sample.json @@ -11,6 +11,7 @@ "webgui_refresh_time": 1 }, "buff": { + "sort_by": "sell_num.desc", "request_interval": 4, "requests_kwargs": { "headers": {