Skip to content

Commit 9b7eacb

Browse files
Merge pull request #1 from RandomProgramm3r/develop
feat: Add async parser version and restructure for modularity - Introduced async scraping logic in async_.py to support non-blocking operations. - Extracted shared functionality into common.py for reuse between sync and async versions. - Split original monolithic parser into separate modules under src/scraper/. - Moved data-related code to data/steam_data.py for better separation of concerns. - Added format_response() utility to format item data with game and currency names. - Updated requirements.txt. - Add new and update old tests. - Overhaul example section and add CI/license badges - Add MIT license
2 parents 5a05a70 + 758a052 commit 9b7eacb

File tree

12 files changed

+383
-105
lines changed

12 files changed

+383
-105
lines changed

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2025 RandomProgramm3r
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# Steam Market Scraper Parser
1+
# Steam Market Parser
22

33
[![Linting](https://github.com/RandomProgramm3r/Steam-Market-Scraper/actions/workflows/linting.yml/badge.svg)](https://github.com/RandomProgramm3r/Steam-Market-Scraper/actions)
4+
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
46
[<img src="https://steamcommunity.com/favicon.ico" width="20" alt="Steam Community Market" />](https://steamcommunity.com/market/)
57

68
## TOC
@@ -15,6 +17,8 @@
1517
- [🧩 Usage](#-usage)
1618
- [🔨 Function Signature](#-function-signature)
1719
- [📤 Example](#-example)
20+
- [🔁 Synchronous usage](#-synchronous-usage)
21+
- [⚡ Asynchronous usage](#-asynchronous-usage)
1822

1923

2024
## 📋 Description
@@ -77,13 +81,13 @@ For code consistency and quality checks, use Ruff - a unified linter/formatter:
7781

7882
```bash
7983
# Run linting checks.
80-
ruff check .
84+
ruff check
8185

8286
# Auto-fix fixable lint issues
83-
ruff check . --fix
87+
ruff check --fix
8488

8589
# Format code.
86-
ruff format .
90+
ruff format
8791
```
8892

8993

@@ -115,12 +119,12 @@ def market_scraper(
115119

116120
## 📤 Example
117121

122+
### 🔁 Synchronous usage
118123
```python
119124
import data
120125
import scraper
121126

122127
# Example usage: Fetch price information for 'Dreams & Nightmares Case' in USD for the CS2 app.
123-
# see more in examples.py
124128
print(
125129
scraper.market_scraper(
126130
'Dreams & Nightmares Case',
@@ -131,10 +135,58 @@ print(
131135
```
132136
#### Output json data:
133137
```json
138+
{
139+
"success": true,
140+
"lowest_price": "$2.19",
141+
"volume": "112,393",
142+
"median_price": "$2.16",
143+
"game_name": "CS2",
144+
"currency_name": "USD",
145+
"item_name": "Dreams & Nightmares Case"
146+
}
147+
```
148+
149+
### ⚡ Asynchronous usage
150+
151+
#### see more in examples.py
152+
```python
153+
async def main():
154+
items = [
155+
('Dreams & Nightmares Case', data.steam_data.Apps.CS2.value, data.steam_data.Currency.USD.value),
156+
('Mann Co. Supply Crate Key', data.steam_data.Apps.TEAM_FORTRESS_2.value, data.steam_data.Currency.EUR.value),
157+
...
158+
]
159+
160+
tasks = [
161+
src.scraper.async_.market_scraper_async(name, app_id, currency)
162+
for name, app_id, currency in items
163+
]
164+
results = await asyncio.gather(*tasks)
165+
for result in results:
166+
print(result)
167+
168+
if __name__ == '__main__':
169+
asyncio.run(main())
170+
```
171+
172+
#### Output json data:
173+
```json
174+
{
175+
"success": true,
176+
"lowest_price": "$2.19",
177+
"volume": "112,393",
178+
"median_price": "$2.16",
179+
"game_name": "CS2",
180+
"currency_name": "USD",
181+
"item_name": "Dreams & Nightmares Case"
182+
}
134183
{
135184
"success": true,
136-
"lowest_price": "$1.90",
137-
"volume": "77,555",
138-
"median_price": "$1.90"
185+
"lowest_price": "2,01€",
186+
"volume": "18,776",
187+
"median_price": "2,03€",
188+
"game_name": "TEAM_FORTRESS_2",
189+
"currency_name": "EUR",
190+
"item_name": "Mann Co. Supply Crate Key"
139191
}
140192
```

data/__init__.py

Whitespace-only changes.
File renamed without changes.

examples.py

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,57 @@
1-
import data
2-
import scraper
1+
import asyncio
32

4-
if __name__ == '__main__':
5-
print(
6-
scraper.market_scraper(
3+
import data.steam_data
4+
import src.scraper.async_
5+
import src.scraper.sync
6+
7+
# sync
8+
print(
9+
src.scraper.sync.market_scraper_sync(
10+
'Dreams & Nightmares Case',
11+
data.steam_data.Apps.CS2.value,
12+
data.steam_data.Currency.USD.value,
13+
),
14+
)
15+
16+
17+
# async
18+
async def main():
19+
items = [
20+
(
721
'Dreams & Nightmares Case',
8-
data.Apps.CS2.value,
9-
data.Currency.USD.value,
22+
data.steam_data.Apps.CS2.value,
23+
data.steam_data.Currency.USD.value,
1024
),
11-
)
12-
print(
13-
scraper.market_scraper(
25+
(
1426
'Mann Co. Supply Crate Key',
15-
data.Apps.TEAM_FORTRESS_2.value,
16-
data.Currency.EUR.value,
27+
data.steam_data.Apps.TEAM_FORTRESS_2.value,
28+
data.steam_data.Currency.EUR.value,
1729
),
18-
)
19-
print(
20-
scraper.market_scraper(
30+
(
2131
'Doomsday Hoodie',
22-
data.Apps.PUBG.value,
23-
data.Currency.GBP.value,
32+
data.steam_data.Apps.PUBG.value,
33+
data.steam_data.Currency.GBP.value,
2434
),
25-
)
26-
print(
27-
scraper.market_scraper(
35+
(
2836
'AWP | Neo-Noir (Factory New)',
29-
data.Apps.CS2.value,
30-
data.Currency.USD.value,
37+
data.steam_data.Apps.CS2.value,
38+
data.steam_data.Currency.USD.value,
3139
),
32-
)
33-
print(
34-
scraper.market_scraper(
40+
(
3541
'Snowcamo Jacket',
36-
data.Apps.RUST.value,
37-
data.Currency.CHF.value,
42+
data.steam_data.Apps.RUST.value,
43+
data.steam_data.Currency.CHF.value,
3844
),
39-
)
45+
]
46+
47+
tasks = [
48+
src.scraper.async_.market_scraper_async(name, app_id, currency)
49+
for name, app_id, currency in items
50+
]
51+
results = await asyncio.gather(*tasks)
52+
for result in results:
53+
print(result)
54+
55+
56+
if __name__ == '__main__':
57+
asyncio.run(main())

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
ruff==0.11.6
1+
aiohttp==3.11.18
2+
ruff==0.11.10

src/__init__.py

Whitespace-only changes.

src/scraper/__init__.py

Whitespace-only changes.

src/scraper/async_.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import asyncio
2+
import json
3+
4+
import aiohttp
5+
6+
import data.steam_data
7+
import src.scraper.common
8+
9+
10+
async def fetch_price(
11+
session: aiohttp.ClientSession,
12+
item_name: str,
13+
app_id: int,
14+
currency: int,
15+
) -> str:
16+
"""
17+
A request is sent asynchronously to the Steam Market and a formatted
18+
JSON response or error text is returned.
19+
"""
20+
encoded = src.scraper.common.encode_item_name(item_name)
21+
url = (
22+
f'{src.scraper.common.BASE_URL}'
23+
f'appid={app_id}'
24+
f'&currency={currency}'
25+
f'&market_hash_name={encoded}'
26+
)
27+
28+
try:
29+
async with session.get(url, timeout=5) as response:
30+
text = await response.text()
31+
parsed = json.loads(text)
32+
33+
return src.scraper.common.format_response(
34+
item_name,
35+
app_id,
36+
currency,
37+
parsed,
38+
)
39+
40+
except aiohttp.ClientResponseError as e:
41+
return f'Server error: {e.status}'
42+
except aiohttp.ClientConnectionError:
43+
return 'Network error. Please check your internet connection.'
44+
except asyncio.TimeoutError:
45+
return 'Timeout error.'
46+
except json.JSONDecodeError:
47+
return 'Error decoding JSON response from server.'
48+
49+
50+
async def market_scraper_async(
51+
item_name: str,
52+
app_id: int,
53+
currency: int = data.steam_data.Currency.USD.value,
54+
) -> str:
55+
"""
56+
Asynchronously fetch the market price for a given Steam item.
57+
58+
This coroutine validates the input parameters, opens an HTTP session,
59+
and delegates to the `fetch_price` helper to retrieve live pricing data
60+
from the Steam market.
61+
62+
Parameters:
63+
item_name (str): The exact name of the Steam marketplace item.
64+
app_id (int): The Steam application ID where the item is listed.
65+
currency (int, optional): The currency code for price conversion.
66+
Defaults to USD if not provided.
67+
68+
Returns:
69+
str: A formatted result, which may be a JSON string
70+
(via `format_response`) or an error message if the fetch fails.
71+
72+
Raises:
73+
ValueError: If any of `item_name`, `app_id`, or `currency` is invalid.
74+
aiohttp.ClientError: If the HTTP request to the Steam API fails.
75+
"""
76+
77+
src.scraper.common.validate_parameters(item_name, app_id, currency)
78+
async with aiohttp.ClientSession() as session:
79+
return await fetch_price(session, item_name, app_id, currency)

0 commit comments

Comments
 (0)