-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteamlibsize_apiver.py
149 lines (127 loc) · 4.77 KB
/
steamlibsize_apiver.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
import requests
import argparse
import xmltodict
import shutil
import json
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
def autodelete_useless(nosizeapp):
os.remove(f"appcache/{nosizeapp}.json")
def delete_cache():
really = input("delete cache? (y/N) ")
if not really == "y":
exit()
else:
print(f"deleting cache...")
shutil.rmtree("appcache/")
def dump_appinfo(appid):
try:
response = requests.get(f"https://api.steamcmd.net/v1/info/{appid}")
app_json = response.json()
except:
print(f"Error: Received status code {response.status_code}")
os.makedirs("appcache", exist_ok=True)
with open(f"appcache/{appid}.json", "w") as appfile:
json.dump(app_json, appfile, indent=4)
print(f"app dump written: {appid}")
#broken_files = []
nosize = 0
def get_app_maxsize(appid):
global nosize
#global broken_files
#response = requests.get(f"https://api.steamcmd.net/v1/info/{appid}")
#app_json = response.json()
with open(f"appcache/{appid}.json", "r") as appfile:
app_json = json.load(appfile)
try:
depots = app_json["data"][f"{appid}"]["depots"]
except:
print("Couldn't get size data of", appid)
nosize += 1
#broken_files.append(appid)
return 0
sizes = []
for depot_id, depot_data in depots.items():
if isinstance(depot_data, dict): # ensure depot_data is a dictionary
manifests = depot_data.get("manifests")
if manifests and "public" in manifests:
size = manifests["public"].get("size")
if size is not None: # check if size exists
sizes.append((depot_id, size))
else:
pass
#print(f"depot {depot_id} skipped: 'manifests' or 'public' key missing.")
else:
pass
#print(f"depot {depot_id} skipped: data is not a dictionary.")
sizenums = []
for depot_id, size in sizes:
sizenums.append(int(size))
# print(size)
#print("Biggest: ",max(sizenums))
return max(sizenums, default=0)
### cli argument parser
cliparser = argparse.ArgumentParser(description="Alternative options.")
cliparser.add_argument("-u", "--url", type=str, help="Provide full URL of a Profile")
cliparser.add_argument("-d", "--delete-cache", action="store_true", help="Delete Cache")
# parse arguments
args = cliparser.parse_args()
if args.delete_cache:
delete_cache()
print("done.")
exit()
if args.url:
steamurl = args.url.rstrip('/')
url = f"{steamurl}/games?tab=all&xml=1"
print("calling Steam about provided profile's games...")
else:
print("\nNo 64 IDs here! use -u [link] to provide a full profile url instead; --help for a list of additional arguments.\n")
steamurl = input("Please Provide a Steam Custom URL: ")
if not steamurl == "":
print(f"calling Steam about {steamurl}'s games...")
url = f"https://steamcommunity.com/id/{steamurl}/games?tab=all&xml=1"
else:
exit()
response = requests.get(url)
lib_data = xmltodict.parse(response.content)
###local testing
#with open("flumandashort_lib.json","r") as inputfile:
# lib_data = json.load(inputfile)
#if "gamesList" in lib_data and "games" in lib_data["gamesList"]: # check if the response makes sense, if yes: store the important stuff
if ( # improved response check. specifically for public profiles with private games.
"gamesList" in lib_data
and isinstance(lib_data["gamesList"], dict)
and "games" in lib_data["gamesList"]
and lib_data["gamesList"]["games"] is not None
and "game" in lib_data["gamesList"]["games"]
):
gamedetails = lib_data["gamesList"]["games"]["game"]
app_ids = [game["appID"] for game in gamedetails]
#print(app_ids)
#print(type(app_ids))
else:
print(f"couldn't fetch {steamurl}'s apps. perhaps they're private or Steam is unresponsive.")
exit()
nosize_files = []
app_sizes = []
for appid in app_ids:
if not os.path.isfile(f"appcache/{appid}.json"):
dump_appinfo(appid)
get_app_bytesize = int(get_app_maxsize(appid))
if get_app_bytesize == 0:
nosize_files.append(appid)
else:
#print(get_app_bytesize)
app_sizes.append(get_app_bytesize)
print(f":::::::::::::::::::::\nResults for: {steamurl}")
max_size_gib = int(sum(app_sizes)) / (1024 ** 3)
max_size_gb = int(sum(app_sizes)) / (1000 ** 3)
print(f"::: {max_size_gb:.2f} GB")
print(f"or: {max_size_gib:.2f} GiB")
print(f"for {len(app_sizes)} apps steam provided sizes of.")
if nosize > 0:
print(nosize, " returned no size.")
#print(f"Specifically: {nosize_files}")
for nosizeapp in nosize_files:
autodelete_useless(nosizeapp)
print("removed them from cache.")