Skip to content

Commit

Permalink
Refactor Buff constructor to take full config object
Browse files Browse the repository at this point in the history
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 <hldh214@gmail.com>
  • Loading branch information
hldh214 committed Feb 26, 2024
1 parent 9845a51 commit 27cbac0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 1 addition & 2 deletions buff2steam/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
35 changes: 20 additions & 15 deletions buff2steam/provider/buff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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')
2 changes: 1 addition & 1 deletion buff_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"webgui_refresh_time": 1
},
"buff": {
"sort_by": "sell_num.desc",
"request_interval": 4,
"requests_kwargs": {
"headers": {
Expand Down

0 comments on commit 27cbac0

Please sign in to comment.