-
Notifications
You must be signed in to change notification settings - Fork 47
/
inventoryApi.py
493 lines (415 loc) · 15.8 KB
/
inventoryApi.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
from fastapi import FastAPI, status, HTTPException, Request
from fastapi.responses import ORJSONResponse
from pydantic import BaseModel
import csv
import sqlite3
import sys
from fastapi.middleware.cors import CORSMiddleware
import requests
from datetime import datetime
import config
import uvicorn
import logging
import json
import subprocess
import io
import time
from fastapi.responses import StreamingResponse
from AccessingWFMarket import *
from typing import Any, Dict, List, Union
logging.basicConfig(format='{levelname:7} {message}', style='{', level=logging.DEBUG)
f = open("config.json")
configData = json.load(f)
f.close()
jwt_token = configData["wfm_jwt_token"]
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
itemNameList = []
class Item(BaseModel):
name:str
purchasePrice:float | None = None
listedPrice:int | None = None
number:int | None = None
class Transact(BaseModel):
name:str
transaction_type:str
price:float | None = None
number:int | None = 1
def receive_signal(signalNumber, frame):
print('Received:', signalNumber)
sys.exit()
def aggregate_and_delete_rows_by_name(name):
# Connect to the SQLite database
conn = sqlite3.connect('inventory.db')
cursor = conn.cursor()
# Retrieve the matching rows and calculate aggregated data
cursor.execute("SELECT SUM(number), SUM(number * purchasePrice) FROM inventory WHERE name = ?", (name,))
result = cursor.fetchone()
total_number = result[0]
weighted_average = result[1] / total_number if total_number else 0
# Delete rows with the same name, except for the first one
cursor.execute("DELETE FROM inventory WHERE name = ? AND rowid NOT IN (SELECT MIN(rowid) FROM inventory WHERE name = ?)", (name, name))
# Update the first row with the aggregated data
cursor.execute("UPDATE inventory SET number = ?, purchasePrice = ? WHERE name = ?", (total_number, weighted_average, name))
# Commit the changes to the database
conn.commit()
# Close the cursor and connection
cursor.close()
conn.close()
return total_number, weighted_average
liveScraperProcess = None
statisticsScraperProcess = None
screenReaderProcess = None
@app.on_event("startup")
async def startup_event():
import signal
signal.signal(signal.SIGINT, receive_signal)
#logger = logging.getLogger("uvicorn.access")
#handler = logging.StreamHandler()
#handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
#logger.addHandler(handler)
config.setConfigStatus("runningWarframeScreenDetect", False)
config.setConfigStatus("runningLiveScraper", False)
config.setConfigStatus("runningStatisticsScraper", False)
# startup tasks
@app.get("/testlog")
async def testLog():
#p = subprocess.Popen(["python", "sandbox.py"])
logging.debug("Testing debug.")
logging.info("Testing info.")
logging.warning("Testing warning! Check console!")
logging.error("Testing error.")
#p.wait()
#p.kill()
return {}
@app.get("/")
async def root():
return {"Nothing Here!": True}
@app.get("/all_items")
async def get_a_list_of_names_of_all_tradable_items():
global itemNameList
if len(itemNameList) == 0:
allItemsLink = "https://api.warframe.market/v1/items"
r = requests.get(allItemsLink)
itemList = r.json()["payload"]["items"]
itemNameList = sorted([x["url_name"] for x in itemList])
return {"item_names" : itemNameList}
@app.get("/items")
async def getItems():
jsonArray = []
con = sqlite3.connect("inventory.db")
con.row_factory = sqlite3.Row # add this row
cur = con.cursor()
res = cur.execute(f"SELECT id, name, purchasePrice, listedPrice, number FROM inventory ")
for row in res:
jsonArray.append(dict(row))
con.close()
return jsonArray
@app.get("/items/sum")
async def sumItems():
con = sqlite3.connect("inventory.db")
cur = con.cursor()
cur.execute("SELECT SUM(number * purchasePrice) AS total_purchase_price, SUM(number * listedPrice) AS total_listed_price FROM inventory")
result = cur.fetchone()
con.close()
return {"total_purchase_price": result[0], "total_listed_price": result[1]}
@app.post("/item")
async def addItem(item : Item):
con = sqlite3.connect("inventory.db")
cur = con.cursor()
alreadyExists = cur.execute(f"SELECT COUNT(name) FROM inventory WHERE name='{item.name}'").fetchone()
if alreadyExists[0] != 0:
cur.execute("INSERT INTO inventory (name, purchasePrice, number) VALUES(?,?,?)", [item.name, item.purchasePrice, item.number])
con.commit()
con.close()
aggregate_and_delete_rows_by_name(item.name)
return {"Executed" : True}
if item.name and item.number:
cur.execute("INSERT INTO inventory (name, purchasePrice, number) VALUES(?,?,?)", [item.name, item.purchasePrice, item.number])
con.commit()
con.close()
return {"Executed" : True}
else:
con.close()
return {"Executed" : False, "Reason": "Need a purchase price and number."}
@app.delete("/item")
async def removeItem(item : Item):
con = sqlite3.connect("inventory.db")
cur = con.cursor()
alreadyExists = cur.execute(f"SELECT COUNT(name) FROM inventory WHERE name='{item.name}'").fetchone()
if alreadyExists[0] != 0:
cur.execute(f"DELETE FROM inventory WHERE name='{item.name}'")
con.commit()
con.close()
return {"Executed" : True}
else:
con.close()
return {"Executed" : False, "Reason": "Item not in database."}
@app.put("/item")
async def updateItem(item : Item):
if (item.number == 0):
await removeItem(item)
return {"Executed" : True}
con = sqlite3.connect("inventory.db")
cur = con.cursor()
alreadyExists = cur.execute(f"SELECT COUNT(name) FROM inventory WHERE name='{item.name}'").fetchone()
if alreadyExists[0] != 0:
if item.name:
cur.execute(f"UPDATE inventory SET purchasePrice=?, number=?, listedPrice=? WHERE name=?", [item.purchasePrice, item.number, item.listedPrice, item.name])
con.commit()
con.close()
return {"Executed" : True}
else:
con.close()
return {"Executed" : False, "Reason": "Need a purchaseprice and number."}
else:
con.close()
return {"Executed" : False, "Reason": "Item not in database."}
@app.post("/item/sell")
async def sellItem(item : Item):
con = sqlite3.connect("inventory.db")
cur = con.cursor()
alreadyExists = cur.execute(f"SELECT COUNT(name) FROM inventory WHERE name='{item.name}'").fetchone()
if alreadyExists[0] != 0:
cur.execute("UPDATE inventory SET number=number-1 WHERE name=?", [item.name])
con.commit()
numLeft = cur.execute(f"SELECT SUM(number) FROM inventory WHERE name='{item.name}'").fetchone()[0]
con.close()
if (numLeft == 0):
await removeItem(item)
return {"Executed" : True}
return {"Executed" : numLeft}
else:
con.close()
return {"Executed" : False, "Reason": "Item not in database."}
def get_order_data(t : Transact):
url = f"https://api.warframe.market/v1/profile/{config.inGameName}/orders"
headers = {
"Content-Type": "application/json; utf-8",
"Accept": "application/json",
"auth_type": "header",
"platform": config.platform,
"language": "en",
"Authorization" : jwt_token
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
orders = data["payload"][f"{t.transaction_type}_orders"]
for order in orders:
if order["item"]["url_name"] == t.name:
return order["id"], order['platinum'], order["quantity"]
# If no matching order found
return None, None, None
# If API call failed
return None, None, None
@app.put("/market/delete")
def delete_order(t : Transact):
con = sqlite3.connect("inventory.db")
cur = con.cursor()
numLeft = cur.execute(f"SELECT SUM(number) FROM inventory WHERE name='{t.name}'").fetchone()[0]
con.close()
if numLeft != 1:
return {"message": "Not deleting order since you have may of these left"}
# Make the DELETE API call
order_id, order_plat, order_quant = get_order_data(t)
time.sleep(0.33)
if order_id is None:
raise HTTPException(
status_code=400,
detail=f'Something went getting the id of this order.',
)
delete_url = f"https://api.warframe.market/v1/profile/orders/{order_id}"
headers = {
"Content-Type": "application/json; utf-8",
"Accept": "application/json",
"auth_type": "header",
"platform": config.platform,
"language": "en",
"Authorization": jwt_token,
}
response = requests.delete(delete_url, headers=headers)
if response.status_code == 200:
return {"message": "Order deleted successfully"}
else:
raise HTTPException(
status_code=400,
detail=f'Something went wrong accessing wf.market api.',
)
@app.put("/market/close")
def close_order(t : Transact):
logging.error(t.name)
# Make the DELETE API call
order_id, order_plat, order_quant = get_order_data(t)
time.sleep(0.33)
if order_id is None:
return {"message": "Order not found"}
if order_plat != t.price:
updateListing(order_id, t.price, order_quant, True, t.name, t.transaction_type)
time.sleep(0.33)
close_url = f"https://api.warframe.market/v1/profile/orders/close/{order_id}"
headers = {
"Content-Type": "application/json; utf-8",
"Accept": "application/json",
"auth_type": "header",
"platform": config.platform,
"language": "en",
"Authorization": config.jwt_token,
'User-Agent': 'Warframe Algo Trader/1.2.8',
}
response = requests.put(close_url, headers=headers, json={})
if response.status_code == 200:
return {"message": "Order closed successfully"}
else:
raise HTTPException(
status_code=400,
detail=f'Something went wrong accessing wf.market api.',
)
@app.get("/transactions")
async def get_transactions():
jsonArray = []
con = sqlite3.connect("inventory.db")
con.row_factory = sqlite3.Row # add this row
cur = con.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
datetime TEXT,
transactionType TEXT,
price INTEGER
) STRICT
""")
res = cur.execute(f"SELECT id, name, datetime, transactionType, price from transactions ")
for row in res:
jsonArray.append(dict(row))
con.close()
return jsonArray
@app.post("/transaction")
def create_transaction(t : Transact):
conn = sqlite3.connect("inventory.db")
cursor = conn.cursor()
# Create the transactions table if it doesn't exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
datetime TEXT,
transactionType TEXT,
price INTEGER
) STRICT
""")
# Insert the transaction into the table
for x in range(t.number):
cursor.execute("INSERT INTO transactions (name, datetime, transactionType, price) VALUES (?, ?, ?, ?)",
(t.name, datetime.now(), t.transaction_type, t.price))
time.sleep(0.0001)
conn.commit()
conn.close()
return {"message": "Transaction created successfully"}
@app.get("/live_scraper")
def get_live_scraper_status():
global liveScraperProcess
if liveScraperProcess == None:
return {"Running" : False}
else:
try:
liveScraperProcess.wait(timeout=0)
return {"Running" : False}
except subprocess.TimeoutExpired:
return {"Running" : True}
return {"Running" : True}
@app.post("/live_scraper/start")
def start_live_scraper():
global liveScraperProcess
if config.getConfigStatus("runningLiveScraper") or liveScraperProcess != None:
return {"Executed" : False, "Reason" : "Scraper already running"}
else:
liveScraperProcess = subprocess.Popen(["python", "LiveScraper.py"])
config.setConfigStatus("runningLiveScraper", True)
f = open("tradeLog.txt", "w")
f.write(f"Starting log file at {datetime.now()}\n")
f.close()
return {"Executed": True}
@app.post("/live_scraper/stop")
def stop_live_scraper():
global liveScraperProcess
if liveScraperProcess == None:
return {"Executed" : False, "Reason" : "Scraper was not running."}
config.setConfigStatus("runningLiveScraper", False)
liveScraperProcess.kill()
liveScraperProcess.wait()
liveScraperProcess = None
return {"Executed": True}
@app.get("/stats_scraper")
def get_stats_scraper_status():
return {"Running" : config.getConfigStatus("runningStatisticsScraper")}
@app.post("/stats_scraper/start")
def start_stats_scraper():
global statisticsScraperProcess
if config.getConfigStatus("runningStatisticsScraper"):
return {"Executed" : False, "Reason" : "Scraper already running"}
else:
statisticsScraperProcess = subprocess.Popen(["python", "StatsScraper.py"])
config.setConfigStatus("runningStatisticsScraper", True)
return {"Executed": True}
@app.post("/stats_scraper/stop")
def stop_stats_scraper():
global statisticsScraperProcess
if statisticsScraperProcess == None:
return {"Executed" : False, "Reason" : "Scraper was not running."}
config.setConfigStatus("runningStatisticsScraper", False)
statisticsScraperProcess.wait()
return {"Executed": True}
@app.get("/screen_reader")
def get_screen_reader_status():
return {"Running" : config.getConfigStatus("runningWarframeScreenDetect")}
@app.post("/screen_reader/start")
def start_screen_reader():
global screenReaderProcess
if config.getConfigStatus("runningWarframeScreenDetect"):
return {"Executed" : False, "Reason" : "Scraper already running"}
else:
config.setConfigStatus("runningWarframeScreenDetect", True)
screenReaderProcess = subprocess.Popen(["python", "EEParser.py"])
return {"Executed": True}
@app.post("/screen_reader/stop")
def stop_screen_reader():
global screenReaderProcess
if screenReaderProcess == None:
return {"Executed" : False, "Reason" : "Screen reader was not running."}
config.setConfigStatus("runningWarframeScreenDetect", False)
screenReaderProcess.wait()
return {"Executed": True}
@app.get("/graph")
def write_graph_to_file(startDate : str | None = None, endDate : str | None = None):
if startDate == None or startDate == "":
startDate = "1990"
if endDate == None or endDate == "":
endDate = "3000"
imgGen = subprocess.run(["python", "GenerateProfitFigure.py", startDate, endDate])
with open("accValue.png", "rb") as f:
# Read the contents of the file
image_data = f.read()
# Create a BytesIO object with the image data
buffer = io.BytesIO(image_data)
# Return the BytesIO object as a StreamingResponse with the appropriate media type
return StreamingResponse(buffer, media_type="image/png")
@app.get("/settings")
def get_settings():
with open('settings.json') as settings:
data = json.load(settings)
return data
@app.put("/settings")
async def put_settings(request: Union[List,Dict,Any]=None):
newSettings = json.dumps(request, indent=4)
with open("settings.json", "w") as settings:
settings.write(newSettings)
return {"Executed" : True}