Skip to content

Commit

Permalink
Merge pull request #3 from offish/v1.1.0
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
offish authored Jan 10, 2020
2 parents 8e644c3 + bbbdf95 commit edb078c
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 121 deletions.
4 changes: 2 additions & 2 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 offish
Copyright (c) 2020 offish

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ 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.
SOFTWARE.
81 changes: 71 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# steam_community_market
[![Version](https://img.shields.io/pypi/v/steam_community_market.svg)](https://pypi.org/project/steam_community_market/)
[![License](https://img.shields.io/github/license/offish/steam_community_market.svg)](https://github.com/offish/steam_community_market/blob/master/LICENSE.txt)
[![License](https://img.shields.io/github/license/offish/steam_community_market.svg)](https://github.com/offish/steam_community_market/blob/master/LICENSE)
[![Stars](https://img.shields.io/github/stars/offish/steam_community_market.svg)](https://github.com/offish/steam_community_market/stargazers)
[![Issues](https://img.shields.io/github/issues/offish/steam_community_market.svg)](https://github.com/offish/steam_community_market/issues)
[![Size]('https://img.shields.io/github/repo-size/offish/steam_community_market.svg)]()
[![Discord](https://img.shields.io/discord/467040686982692865.svg)](https://discord.gg/t8nHSvA)
<br>
[![Steam Donate Button](https://img.shields.io/badge/donate-steam-green.svg)](https://steamcommunity.com/tradeoffer/new/?partner=293059984&token=0-l_idZR "Support this project via Steam")
Expand All @@ -28,11 +29,11 @@ pip install steam_community_market
Usage
=====
```python
from steam_community_market.prices import Prices
from steam_community_market.market import Market

market = Prices('USD')
market = Market('USD')
```
`'USD'` can either be `str` or `int`. Find the currencies supported [here](https://github.com/offish/steam_community_market/blob/master/steam_community_market/prices.py#L5).
`'USD'` can either be `str`, `int` or empty. Find the currencies supported [here](https://github.com/offish/steam_community_market/blob/master/steam_community_market/market.py#L5).



Expand All @@ -50,12 +51,44 @@ Methods

`app_id`: The AppID of the item.

### Get multiple items:
### Get multiple items with different AppIDs:

**get_prices(names: list, app_id: int)**
**get_prices(names: list, app_id: (int, list))**

```python
>>> items = ['Prisma Case', 'Danger Zone Case', 'Supermega Case']
>>> items = ['Prisma Case', 'Danger Zone Case', 'Spectrum 10 Case']
>>> appids = [730, 730, 440]
>>> market.get_prices(items, appids)
{
'Prisma Case': {
'success': True,
'lowest_price': '$0.39',
'volume': '59,613',
'median_price': '$0.41'
},

'Danger Zone Case': {
'success': True,
'lowest_price': '$0.20',
'volume': '56,664',
'median_price': '$0.22'
},

'Spectrum 10 Case': {
'success': False
}
}
```
`names`: A list of items how each item name (market_hash_name) appears on the Steam Community Market.

`app_id`: A list of AppIDs.

### Get multiple items with the same AppID:

**get_prices(names: list, app_id: (int, list))**

```python
>>> items = ['Prisma Case', 'Danger Zone Case', 'Spectrum 10 Case']
>>> market.get_prices(items, 730)
{
'Prisma Case': {
Expand All @@ -72,24 +105,52 @@ Methods
'median_price': '$0.22'
},

'Supermega Case': {
'Spectrum 10 Case': {
'success': False
}
}
```
`names`: A list of items how each item name (market_hash_name) appears on the Steam Community Market.

`app_id`: The AppID of the items.
`app_id`: The AppID of the items.

**All of the items listed in `names` must have the same `app_id`.**

### Get multiple items with different AppIDs from dict

**get_prices_from_dict(items: dict)**
```python
>>> items = {
"Mann Co. Supply Crate Key": {
"appid": 440
},
"AK-47 | Redline (Field-Tested)": {
"appid": 730
}
}
>>> market.get_prices_from_dict(items)
{
'Mann Co. Supply Crate Key': {
'success': True,
'lowest_price': '$2.50',
'volume': '6,489',
'median_price': '$2.45'
},
'AK-47 | Redline (Field-Tested)': {
'success': True,
'lowest_price': '$15.00',
'volume': '749',
'median_price': '$14.78'
}
}
```


License
=======
MIT License

Copyright (c) 2019 [offish]([email protected])
Copyright (c) 2020 [offish](mailto:[email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
52 changes: 47 additions & 5 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
from steam_community_market.prices import Prices
from steam_community_market.market import Market

market = Prices('USD')

items = ['Prisma Case', 'Danger Zone Case', 'Supermega Case']
item = market.get_prices(items, 730)

print(item)
market = Market('NOK') # Could either be: 'NOK', 'nok' or 9.
# For USD; leave it empty or use 'USD', 'usd' or 1.
# For all the currencies supported go here:
# https://github.com/offish/steam_community_market/blob/master/steam_community_market/market.py#L5



# Example using get_price.
# First parameter is the name of the item how it appears on the Steam Community Market.
# Second parameter is the AppID of the game the item is from.
market.get_price('Mann Co. Supply Crate Key', 440) # 440 is TF2's AppID.
# {'success': True, 'lowest_price': '22,12 kr', 'volume': '6,489', 'median_price': '21,75 kr'}



# Example using get_prices with the same AppID.
items = ['Mann Co. Supply Crate Key', 'Tour of Duty Ticket']
market.get_prices(items, 440)
# {'Mann Co. Supply Crate Key': {'success': True, 'lowest_price': '22,36 kr', 'volume': '6,489', 'median_price': '21,75 kr'},
# 'Tour of Duty Ticket': {'success': True, 'lowest_price': '9,21 kr', 'volume': '668', 'median_price': '9,61 kr'}}



# Example using get_prices with different AppIDs.
items = ['Mann Co. Supply Crate Key', 'AWP | Atheris (Field-Tested)']
appids = [440, 730] # 440 is TF2, 730 is CSGO.
# These two lists MUST have the same length. In this case they both have 2 elements.
market.get_prices(items, appids)
# {'Mann Co. Supply Crate Key': {'success': True, 'lowest_price': '22,01 kr', 'volume': '6,489', 'median_price': '21,75 kr'},
# 'AWP | Atheris (Field-Tested)': {'success': True, 'lowest_price': '47,68 kr', 'volume': '1,381', 'median_price': '45,39 kr'}}



# Example using get_prices_from_dict.
items = {
"Mann Co. Supply Crate Key": {
"appid": 440
},
"AK-47 | Redline (Field-Tested)": {
"appid": 730
} # Do not add a comma at the end of the last entry.
}

market.get_prices_from_dict(items)
# {'Mann Co. Supply Crate Key': {'success': True, 'lowest_price': '22,36 kr', 'volume': '6,489', 'median_price': '21,75 kr'},
# 'AK-47 | Redline (Field-Tested)': {'success': True, 'lowest_price': '136,51 kr', 'volume': '749', 'median_price': '131,36 kr'}}
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

setuptools.setup(
name="steam_community_market",
version="1.0.0",
version="1.1.0",
author="offish",
author_email="[email protected]",
description="Easily get item prices and volumes from the Steam Community Market using Python 3",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/offish/steam_community_market",
download_url='https://github.com/offish/steam_community_market/archive/v1.0.0.tar.gz',
download_url='https://github.com/offish/steam_community_market/archive/v1.1.0.tar.gz',
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
138 changes: 138 additions & 0 deletions steam_community_market/market.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
from steam_community_market.request import request
from typing import Union
import enum


class ESteamCurrency(enum.IntEnum):
USD = 1
GBP = 2
EUR = 3
CHF = 4
RUB = 5
PLN = 6
BRL = 7
JPY = 8
NOK = 9
IDR = 10
MYR = 11
PHP = 12
SGD = 13
THB = 14
VND = 15
KRW = 16
TRY = 17
UAH = 18
MXN = 19
CAD = 20
AUD = 21
NZD = 22
CNY = 23
INR = 24
CLP = 25
PEN = 26
COP = 27
ZAR = 28
HKD = 29
TWD = 30
SAR = 31
AED = 32


class Market:
url = 'http://steamcommunity.com/market/priceoverview'

def __init__(self, currency: (str, int) = 1):
"""
Sets the currency to be outputted.
:param currency: 1, 'USD' or leave empty for American Dollars. For other currencies take a look at the README.
"""

if isinstance(currency, str):
currency = currency.upper()

if currency in [i.name for i in ESteamCurrency]:
currency = ESteamCurrency[currency].value

if isinstance(currency, int):
if currency > 32 or currency < 1:
currency = 1

else:
currency = 1

self.currency = currency

def get_price(self, name: str, app_id: int) -> dict:
"""
Gets the price(s) and volume of an item.
:param name: The name of the item how it appears on the Steam Community Market.
:param app_id: The AppID of the item.
"""

if not isinstance(name, str):
raise TypeError('name must be str')

if not isinstance(app_id, int):
raise TypeError('app_id must be int')

if self.has_invalid_name(name):
name = self.fix_name(name)

payload = {'appid': app_id, 'market_hash_name': name,
'currency': self.currency}

return request(self.url, payload)

def get_prices(self, names: list, app_id: (int, list)) -> dict:
"""
Gets the price(s) and volume of each item in the list. If both are lists, then they need to have the same amount of elements.
:param names: A list of item names how each item appears on the Steam Community Market.
:param app_id: The AppID of the item(s). Either a list or int. For more information check the example.py file.
"""

prices = {}

if not isinstance(names, list):
raise TypeError('names must be list')

if isinstance(app_id, int):
for name in names:
prices[name] = self.get_price(name, app_id)

elif isinstance(app_id, list):
if len(names) == len(app_id):
for i in range(len(names)):
name = names[i]
prices[name] = self.get_price(name, app_id[i])
else:
raise IndexError('names and app_id needs to have the same len')

return prices

def get_prices_from_dict(self, items: dict) -> dict:
"""
Gets the price(s) and volume of each item in the list.
:param items: A dict including item names and AppIDs. Check example.py file for more information.
"""

prices = {}

if not isinstance(items, dict):
raise TypeError('items must be dict')

for item in items:
prices[item] = self.get_price(item, items[item]['appid'])
return prices

def has_invalid_name(self, name: str) -> bool:
if isinstance(name, str):
try:
return name.index('/') >= 0
except ValueError:
return False
return False

def fix_name(self, name: str):
if isinstance(name, str):
return name.replace('/', '-')
return False
Loading

0 comments on commit edb078c

Please sign in to comment.