forked from akmayer/Warframe-Algo-Trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessingWFMarket.py
212 lines (186 loc) · 7.08 KB
/
AccessingWFMarket.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import json
import requests
from bs4 import BeautifulSoup
import time
import config
import pandas as pd
import customLogger
class WarframeApi:
def __init__(self):
self.t0 = time.time()
self.jwt_token = config.jwt_token
self.session = requests.Session() # Use a Session object for connection pooling
self.session.headers = {
"Content-Type": "application/json; utf-8",
"Accept": "application/json",
"auth_type": "header",
"platform": config.platform,
"language": "en",
"Authorization": self.jwt_token,
'User-Agent': 'Warframe Algo Trader/1.3.1',
}
self.lastRequestTime = 0
self.timeBetweenRequests = 0.34
def waitUntilDelayEnds(self):
if (time.time() - self.lastRequestTime) < self.timeBetweenRequests:
time.sleep(self.lastRequestTime - time.time() + self.timeBetweenRequests)
def get(self, link):
self.waitUntilDelayEnds()
self.lastRequestTime = time.time()
response = self.session.get(link)
return response
def post(self, link, json):
self.waitUntilDelayEnds()
self.lastRequestTime = time.time()
response = self.session.post(link, json=json)
return response
def delete(self, link):
self.waitUntilDelayEnds()
self.lastRequestTime = time.time()
response = self.session.delete(link)
return response
def put(self, link, json):
self.waitUntilDelayEnds()
self.lastRequestTime = time.time()
response = self.session.put(link, json=json)
return response
# class WarframeApi:
# def __init__(self):
# self.t0 = time.time()
# self.jwt_token = config.jwt_token
# self.headers = {
# "Content-Type": "application/json; utf-8",
# "Accept": "application/json",
# "auth_type": "header",
# "platform": config.platform,
# "language": "en",
# "Authorization": self.jwt_token,
# 'User-Agent': 'Warframe Algo Trader/1.2.9',
# }
# self.lastRequestTime = 0
# self.timeBetweenRequests = 0.1
# def waitUntilDelayEnds(self):
# if (time.time() - self.lastRequestTime) < self.timeBetweenRequests:
# time.sleep(self.lastRequestTime - time.time() + self.timeBetweenRequests)
# def get(self, link, headers=None):
# t0 = time.time()
# self.waitUntilDelayEnds()
# self.lastRequestTime = time.time()
# r = requests.get(link, headers=self.headers)
# #print(time.time()-t0)
# return r
# def post(self, link, json, headers=None):
# t0 = time.time()
# self.waitUntilDelayEnds()
# self.lastRequestTime = time.time()
# r = requests.post(link, headers=self.headers, json=json)
# #print(time.time()-t0)
# return r
# def delete(self, link, headers=None):
# t0 = time.time()
# self.waitUntilDelayEnds()
# self.lastRequestTime = time.time()
# r = requests.delete(link, headers=self.headers)
# #print(time.time()-t0)
# return r
# def put(self, link, json, headers=None):
# t0 = time.time()
# self.waitUntilDelayEnds()
# self.lastRequestTime = time.time()
# r = requests.put(link, headers=self.headers, json=json)
# #print(time.time()-t0)
# return r
WFM_API = "https://api.warframe.market/v1"
warframeApi = WarframeApi()
def login(
user_email: str, user_password: str, platform: str = "pc", language: str = "en"
):
"""
Used for logging into warframe.market via the API.
Returns (User_Name, JWT_Token) on success,
or returns (None, None) if unsuccessful.
"""
content = {"email": user_email, "password": user_password, "auth_type": "header"}
response = warframeApi.post(f"{WFM_API}/auth/signin", data=json.dumps(content))
customLogger.writeTo(
"wfmAPICalls.log",
f"POST:{WFM_API}/auth/signin\tResponse:{response.status_code}"
)
if response.status_code != 200:
return None, None
return (response.json()["payload"]["user"]["ingame_name"], response.headers["Authorization"])
def postOrder(item, order_type, platinum, quantity, visible, modRank, itemName):
json_data = {
"item": str(item),
"order_type": str(order_type),
"platinum": int(platinum),
"quantity": int(quantity),
"visible": visible
}
if modRank:
json_data["rank"] = modRank
response = warframeApi.post(f'{WFM_API}/profile/orders', json=json_data)
customLogger.writeTo(
"wfmAPICalls.log",
f"POST:{WFM_API}/profile/orders\tResponse:{response.status_code}\tItem:{itemName}\tOrder Type:{order_type}\tPlatinum:{platinum}\tQuantity:{quantity}\tVisible:{visible}"
)
if response.status_code == 200:
customLogger.writeTo(
"orderTracker.log",
f"POSTED\tItem:{itemName}\tOrder Type:{order_type}\tPlatinum:{platinum}\tQuantity:{quantity}\tVisible:{visible}"
)
return response
def deleteOrder(orderID):
r = warframeApi.delete(f'{WFM_API}/profile/orders/{orderID}')
customLogger.writeTo(
"wfmAPICalls.log",
f"DELETE:{WFM_API}/profile/orders/{orderID}\tResponse:{r.status_code}"
)
if r.status_code == 200:
customLogger.writeTo(
"orderTracker.log",
f"DELETED\tOrder ID: {orderID}"
)
def getOrders():
r = warframeApi.get(f"{WFM_API}/profile/{config.inGameName}/orders")
customLogger.writeTo(
"wfmAPICalls.log",
f"GET:{WFM_API}/profile/{config.inGameName}/orders\tResponse:{r.status_code}"
)
return r.json()["payload"]
def updateListing(listing_id, platinum, quantity, visibility, itemName, order_type):
try:
url = WFM_API + "/profile/orders/" + listing_id
contents = {
"platinum": int(platinum),
"quantity": int(quantity),
"visible": visibility
}
response = warframeApi.put(url, json=contents)
customLogger.writeTo(
"wfmAPICalls.log",
f"PUT:{WFM_API}/profile/orders/{listing_id}\tResponse:{response.status_code}\tItem:{itemName}\tOrder Type:{order_type}\tPlatinum:{platinum}\tVisible:{visibility}"
)
response.raise_for_status() # Raises an exception for non-2xx status codes
if response.status_code == 200:
customLogger.writeTo(
"orderTracker.log",
f"UPDATED\tItem:{itemName}\tOrder Type:{order_type}\tPlatinum:{platinum}\tVisible:{visibility}"
)
return True
except requests.exceptions.RequestException as e:
print(f"update_listing: {e}")
return False
if __name__ == "__main__":
r = warframeApi.post(
f'{WFM_API}/profile/orders',
{
"item": "5bc1ab93b919f200c18c10ef",
"platinum": 1,
"order_type": "buy",
"quantity": 1,
"rank": 1,
"visible": False
}
)
print(r.status_code)