-
Notifications
You must be signed in to change notification settings - Fork 4
/
nsb_database.py
71 lines (55 loc) · 1.73 KB
/
nsb_database.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
import pickle
import xml.etree.ElementTree as ET
import nsb_steam
def entryIndex(xml):
for index, value in enumerate(xml):
if value.tag == 'entries':
return index
raise Exception('no index tag in xml')
def convertIfPossible(data):
try:
return int(data)
except (ValueError, TypeError):
pass
try:
return float(data)
except (ValueError, TypeError):
pass
return data
def xmlToList(response, responseType):
text = nsb_steam.decodeResponse(response)
data = ET.fromstring(text)
return xmlToList_internal(data, responseType)
def xmlToList_file(path, responseType):
tree = ET.parse(path)
root = tree.getroot()
return xmlToList_internal(root, responseType)
def xmlToList_internal(data, responseType):
result = []
if responseType == 'leaderboard':
index = entryIndex(data)
entries = data[index]
elif responseType == 'index':
entries = data[3:]
else:
raise Exception('Unknown responseType')
for entry in entries:
dictEntry = {}
for data in entry:
tag = data.tag
if tag == 'score':
tag = 'points'
elif tag == 'steamid':
tag = 'steam_id'
dictEntry[tag] = convertIfPossible(data.text)
result.append(dictEntry)
return result
def pickle_file(data, path):
with open(path, 'wb') as f:
# Pickle the 'data' dictionary using the highest protocol available.
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
def unpickle(path):
with open(path, 'rb') as f:
# The protocol version used is detected automatically, so we do not
# have to specify it.
return pickle.load(f)