forked from TjtnSmejky/Steam-market-automated-spreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsitems.py
99 lines (76 loc) · 2.69 KB
/
csitems.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
import sys
import time
import json
import steammarket
from datetime import datetime
def get_items(filename):
items = []
with open(filename, "r", encoding="utf8") as txt_items:
for line in txt_items:
line = line.strip("\n")
if line:
items.append(line)
return items
def get_history(name):
try:
with open(f"{name}.history.json", "r", encoding="utf8") as file:
history = json.loads(file.read())
except FileNotFoundError as e:
history = {}
return history
def clean_up_history(history, items):
"""This function removes all items from history that are not longer in the items file"""
new_history = {}
for date, date_data in history.items():
new_data = {}
for item, item_data in date_data.items():
if item in items:
new_data[item] = item_data
if new_data:
new_history[date] = new_data
return new_history
def write_history(name, history):
print("Writing History")
print(json.dumps(history))
with open(f"{name}.history.json", "w", encoding="utf8") as file:
file.write(json.dumps(history))
def write_output_csv(name, history):
print("Writing CSV")
with open(f"{name}.output.csv", "w", encoding="utf8") as file:
file.write("Date,Item,Price")
for date, date_data in history.items():
for item, item_data in date_data.items():
if item_data.get("success"):
output = f"{date},\"{item}\",{item_data.get('median_price')}\n"
file.write(output)
def main(filename, currency):
name = filename.split(".")[0]
items = get_items(filename)
history = get_history(name)
history = clean_up_history(history, items)
date = datetime.now().date()
isodate = date.isoformat()
print("Request Date: ", isodate)
for item in items:
# Sleep it to avoid rate limiting and returning nothing
time.sleep(2)
result = steammarket.get_csgo_item(item, currency=currency)
data = history.get(isodate, {})
if result and result.get("success") is True:
print(result)
data[item] = result
else:
print("Cannot retrieve item: ", item, result)
data[item] = {"success": False, "median_price": "N/A"}
history[isodate] = data
write_history(name, history)
write_output_csv(name, history)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Please provide a filename")
exit()
currency = "EUR"
if len(sys.argv) > 2:
currency = sys.argv[2]
main(filename=sys.argv[1], currency=currency)
print("###### Bingo ######")