-
Notifications
You must be signed in to change notification settings - Fork 1
/
inventory_utils.py
148 lines (102 loc) · 4.21 KB
/
inventory_utils.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# The code shown below is an excerpt from:
# https://github.com/woctezuma/steam-market/blob/master/inventory_utils.py
import json
import requests
from personal_info import (
get_cookie_dict,
update_and_save_cookie_to_disk_if_values_changed,
)
from utils import get_data_folder
def get_my_steam_profile_id():
my_profile_id = '76561198028705366'
return my_profile_id
def get_steam_inventory_url(profile_id=None, app_id=753, context_id=6):
if profile_id is None:
profile_id = get_my_steam_profile_id()
# References:
# https://github.com/Alex7Kom/node-steam-tradeoffers/issues/114
# https://dev.doctormckay.com/topic/332-identifying-steam-items/
# steam_inventory_url = 'https://steamcommunity.com/profiles/' + str(profile_id) + '/inventory/json/'
# Reference for the new API end-point: https://steamcommunity.com/discussions/forum/1/1736595227843280036/
steam_inventory_url = (
'https://steamcommunity.com/inventory/' + str(profile_id) + '/'
) # TODO
steam_inventory_url += str(app_id) + '/' + str(context_id) + '/'
return steam_inventory_url
def get_steam_inventory_file_name(profile_id):
steam_inventory_file_name = (
get_data_folder() + 'inventory_' + str(profile_id) + '.json'
)
return steam_inventory_file_name
def load_steam_inventory_from_disk(profile_id=None):
if profile_id is None:
profile_id = get_my_steam_profile_id()
try:
with open(get_steam_inventory_file_name(profile_id)) as f:
steam_inventory = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError) as e:
steam_inventory = download_steam_inventory(profile_id, save_to_disk=True)
return steam_inventory
def load_steam_inventory(profile_id=None, update_steam_inventory=False):
if profile_id is None:
profile_id = get_my_steam_profile_id()
if update_steam_inventory:
steam_inventory = download_steam_inventory(profile_id, save_to_disk=True)
else:
steam_inventory = load_steam_inventory_from_disk(profile_id=profile_id)
return steam_inventory
def download_steam_inventory(profile_id=None, save_to_disk=True, start_asset_id=None):
if profile_id is None:
profile_id = get_my_steam_profile_id()
cookie = get_cookie_dict()
has_secured_cookie = bool(len(cookie) > 0)
url = get_steam_inventory_url(profile_id=profile_id)
req_data = {
"l": 'english',
"count": 5000,
} # TODO
if start_asset_id is not None:
req_data['start_assetid'] = start_asset_id # TODO
if has_secured_cookie:
resp_data = requests.get(url, params=req_data, cookies=cookie) # TODO
else:
resp_data = requests.get(url, params=req_data) # TODO
status_code = resp_data.status_code
if status_code == 200:
steam_inventory = resp_data.json()
if has_secured_cookie:
jar = dict(resp_data.cookies)
cookie = update_and_save_cookie_to_disk_if_values_changed(cookie, jar)
if save_to_disk:
with open(get_steam_inventory_file_name(profile_id), 'w') as f:
json.dump(steam_inventory, f)
else:
print(
'Inventory for profile {} could not be loaded. Status code {} was returned.'.format(
profile_id,
status_code,
),
)
steam_inventory = None
return steam_inventory
def get_session_id(cookie=None):
if cookie is None:
cookie = get_cookie_dict()
session_id = cookie['sessionid']
return session_id
def get_request_headers():
# Reference: https://dev.doctormckay.com/topic/287-automatic-market-seller/
request_headers = {
'Origin': 'https://steamcommunity.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'https://steamcommunity.com/my/inventory/',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
}
return request_headers
def main():
steam_inventory = load_steam_inventory()
return True
if __name__ == '__main__':
main()