/
new-to-me.py
83 lines (67 loc) · 2.33 KB
/
new-to-me.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
# pip install boardgamegeek2
from boardgamegeek import BGGClient
from boardgamegeek import CacheBackendSqlite
from boardgamegeek import exceptions
import time
bgg = BGGClient()
bgg_sqlite_cache = BGGClient(cache=CacheBackendSqlite(path="./cache.db", ttl=86400)) # ttl = 1 day (60 s * 60 m * 24 h) Does this even work?
year = 2020
also_old = False
user_name = 'mortenjohs'
other_player_names = ["Gab", "Nina", "Jean", "Pierrot", "Florent", "Clem", "Laure"]
user = bgg_sqlite_cache.user(user_name)
# temp_collection = bgg.collection(user_name)
# collection
plays = bgg_sqlite_cache.plays(user_name)
games = {}
mechanics = {}
categories = {}
def get_game(id, bgg_client):
try:
return bgg_client.game(game_id=id)
except boardgamegeek.exceptions.BGGApiError as e:
print(e)
s = 120
print("\nBGG limit reached? -- Sleeping for "+ str(s) + " seconds")
time.sleep(s) # sleep for two minutes -- is that enough?
for p in plays:
is_new = False
if p.date.year == year:
for pl in p.players:
if pl.username == user_name:
if pl.new == "1" or also_old:
print(".", end ="")
games[p.game_id] = get_game(p.game_id,bgg_sqlite_cache)
games_others = {}
for name in other_player_names:
games_others[name] = {}
for p in plays:
is_new = False
if p.date.year == year:
for pl in p.players:
if pl.name == name:
if pl.new == "1" or also_old:
print(".", end ="")
games_others[name][p.game_id] = get_game(p.game_id,bgg_sqlite_cache)
games = {k: v for k, v in sorted(games.items(), key=lambda item: item[1].name)}
for g in games.values():
for m in g.mechanics:
mechanics.setdefault(m, 0)
mechanics[m] += 1
for c in g.categories:
categories.setdefault(c, 0)
categories[c] += 1
mechanics = { k: v for k, v in sorted(mechanics.items(), key=lambda item: -item[1]) }
categories = { k: v for k, v in sorted(categories.items(), key=lambda item: -item[1]) }
print("\nName,id,Year,Rank,Rating (avg),Weight")
for k, v in games.items():
print(
v.name + "," +
str(k) + "," +
str(v.year) + "," +
str(v.bgg_rank) + "," +
str(v.rating_average) + "," +
str(v.rating_average_weight)
)
for name in other_player_names:
print("\n" + name + "("+ str(len(games_others[name].items())) + "): " + str([i.name for k,i in games_others[name].items()]))