-
Notifications
You must be signed in to change notification settings - Fork 2
/
SteamInventory.py
110 lines (105 loc) · 5.09 KB
/
SteamInventory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from SteamLogin import SteamLogin
from utils import ignoreConnectionErrors
class Inventory(object):
def __init__(self,Login,**kwargs):
assert isinstance(Login,SteamLogin), "Login is not an instance of SteamLogin"
self.Login = Login
self.request = Login.request
self.custom_url = kwargs.get('custom_url')
self.steamID = kwargs.get('steamID')
self.items = {}
@ignoreConnectionErrors(echo=True)
def getInventory(self,appID,contextID,tradeable=True,**kwargs):
update = kwargs.get('update')
if update is None:
if appID in self.items.keys():
if contextID in self.items[appID].keys():
return
if tradeable:
params = {'trading':1}
else:
params = {'trading':0}
if (self.custom_url is None) and (self.steamID is None):
raise InventoryException("This inventory has no steam account associated")
if self.custom_url is not None:
url = "".join(["https://steamcommunity.com/id/",self.custom_url,"/inventory/json/",str(appID),"/",str(contextID)])
else:
url = "".join(["https://steamcommunity.com/profiles/",str(self.steamID),"/inventory/json/",str(appID),"/",str(contextID)])
response = self.request.get(url,params=params)
if response.status_code != 200:
raise Exception("Server Error")
data = response.json()
self.createItemsFromResponse(appID,contextID,data)
def __str__(self):
return("Steam Inventory for " + self.steamID + " on App: " + self.appID + " and Context: " + self.contextID)
def createItemsFromResponse(self,appID,contextID,jsonData):
if jsonData['success'] != True:
try:
error = jsonData['Error']
if(error == "This profile is private."):
raise InventoryPrivateException(steamID)
except KeyError:
raise InventoryException(appID,contextID,custom_url=self.custom_url,steamID=self.steamID)
items = {}
rgInventory = jsonData['rgInventory']
rgDescriptions = jsonData['rgDescriptions']
for x in rgInventory.keys():
item_id = int(x)
class_id = int(rgInventory[x]['classid'])
instance_id = int(rgInventory[x]['instanceid'])
amount = int(rgInventory[x]['amount'])
key = "_".join([str(class_id),str(instance_id)])
description = rgDescriptions[key]
items.update({item_id:Item(item_id,contextID,class_id,instance_id,amount,description)})
if appID not in self.items.keys():
self.items[appID] = {}
self.items[appID][contextID] = items
class Item(object):
def __init__(self,item_id,contextID,class_id,instance_id,amount,description):
self.itemid = item_id
self.classid = class_id
self.instanceid = instance_id
self.amount = amount
self.appid = description['appid']
self.contextid = contextID
self.icon_url = "".join(["http://cdn.steamcommunity.com/economy/image/",description['icon_url']])
self.icon_url_large = "".join(["http://cdn.steamcommunity.com/economy/image/",description['icon_url_large']]) if 'icon_url_large' in description.keys() else ""
self.icon_drag_url = description.get('icon_drag_url')
self.name = description.get('name')
self.market_hash_name = description.get('market_hash_name')
self.market_name = description.get('market_name')
self.name_color = description.get('name_color')
self.background_color = description.get('background_color')
self.type = description.get('type')
self.tradable = bool(description.get('tradable',True))
self.marketable = bool(description.get('marketable',True))
self.commodity = bool(description.get('commodity',True))
self.market_tradable_restriction = description.get('market_tradable_restriction')
self.descriptions = description.get('descriptions')
self.actions = description['actions'] if 'actions' in description.keys() else ""
self.market_actions = description['market_actions'] if 'market_actions' in description.keys() else ""
self.tags = description['tags']
def getMarketHashName(self):
if self.market_hash_name != "":
return self.market_hash_name
elif self.market_name != "":
return self.market_name
else:
return self.name
def __str__(self):
return unicode(self).encode('utf-8')
def __unicode__(self):
return self.getMarketHashName()
class InventoryException(Exception):
def __init__(self,appID,contextID,**kwargs):
self.steamID = kwargs.get('steamID')
self.custom_url = kwargs.get('custom_url')
self.appID = appID
self.contextID = contextID
def __str__(self):
return("The request did not return a valid response")
class InventoryPrivateException(Exception):
def __init__(self,steamID):
self.steamID = steamID
def __str__(self):
return " ".join(["The steam profile",steamID,"is private"])