-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve_game_info.py
65 lines (49 loc) · 1.19 KB
/
retrieve_game_info.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
#!/usr/bin/env python
import urllib
from bs4 import BeautifulSoup
import sys
import json
ID = sys.argv[1]
f = urllib.urlopen("https://www.boardgamegeek.com/xmlapi2/thing?id=%s&ratingcomments=1&stats=1" %(ID))
s = f.read()
f.close()
soup = BeautifulSoup(s)
tag_w_value = (
"yearpublished",
"minplayers",
"maxplayers",
"playingtime",
"minplaytime",
"maxplaytime",
"minage",
"usersrated",
"average",
"bayesaverage",
"stddev",
"median",
"owned",
"trading",
"wanting",
"wishing",
"numcomments",
"numweights",
"averageweight",
)
games = {}
for item in soup.find_all("item"):
d = {}
# Extract collection table
d["description"] = item.find("description").get_text()
for t in tag_w_value:
d[t] = item.find(t)["value"].replace("\t", " ")
for l in item.find_all("link"):
d.setdefault(l["type"], []).append( l["id"] + "_" + l["value"].replace("\t"," "))
d["n_comments"] = sum(int(c["totalitems"]) for c in item.find_all("comments"))
comments = []
for i,comment in enumerate(item.find_all("comment")):
comments.append({})
for attr in ("username", "rating", "value"):
comments[i][attr] = comment[attr]
d["comments"] = comments
games[item["id"]] = d
print json.dumps(games)