-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend_igdb.py
224 lines (172 loc) · 6.65 KB
/
extend_igdb.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import json
import time
from pathlib import Path
from anonymize_data import get_data_folder
from igdb_databases import (
get_igdb_file_name_suffix,
load_igdb_local_database,
load_igdb_match_database,
save_igdb_local_database,
)
from igdb_look_up import look_up_game_id, wait_for_cooldown
def get_file_name_for_fixes_to_igdb_database(
release_year: str | None = None,
database_type: str | None = None,
) -> str:
if database_type is None:
database_type = "local" # either 'local' or 'match'
prefix = "fixes_to_"
suffix = get_igdb_file_name_suffix(release_year)
return (
get_data_folder()
+ prefix
+ "igdb_"
+ database_type
+ "_database"
+ suffix
+ ".json"
)
def load_fixes_to_igdb_database(
release_year: str | None = None,
database_type: str | None = None,
) -> dict:
if database_type is None:
database_type = "local" # either 'local' or 'match'
file_name = get_file_name_for_fixes_to_igdb_database(
release_year=release_year,
database_type=database_type,
)
try:
with Path(file_name).open(encoding="utf-8") as f:
fixes_to_igdb_database = json.load(f)
except FileNotFoundError:
print(f"File {file_name} not found.")
fixes_to_igdb_database = {}
return fixes_to_igdb_database
def load_fixes_to_igdb_local_database(release_year: str | None = None) -> dict:
return load_fixes_to_igdb_database(
release_year=release_year,
database_type="local",
)
def load_fixes_to_igdb_match_database(release_year: str | None = None) -> dict:
return load_fixes_to_igdb_database(
release_year=release_year,
database_type="match",
)
def extend_igdb_local_database(
release_year: str | None = None,
igdb_local_database: dict | None = None,
) -> dict:
if igdb_local_database is None:
igdb_local_database = load_igdb_local_database(release_year=release_year)
fixes_to_igdb_local_database = load_fixes_to_igdb_local_database(
release_year=release_year,
)
extended_igdb_local_database = igdb_local_database
for igdb_id in fixes_to_igdb_local_database:
if igdb_id in igdb_local_database:
print(
f"IGDB ID {igdb_id} already exists in IGDB local database. Data will be overwritten.",
)
extended_igdb_local_database[igdb_id] = fixes_to_igdb_local_database[igdb_id]
return extended_igdb_local_database
def extend_igdb_match_database(
release_year: str | None = None,
igdb_match_database: dict | None = None,
*,
verbose: bool = True,
) -> dict:
if igdb_match_database is None:
igdb_match_database = load_igdb_match_database(release_year=release_year)
fixes_to_igdb_match_database = load_fixes_to_igdb_match_database(
release_year=release_year,
)
extended_igdb_match_database = igdb_match_database
for app_name in fixes_to_igdb_match_database:
if app_name in igdb_match_database and verbose:
print(
f"Query name {app_name} already exists in IGDB match database. Match will be overwritten.",
)
extended_igdb_match_database[app_name] = fixes_to_igdb_match_database[app_name]
return extended_igdb_match_database
def fill_in_blanks_in_the_local_database(
release_year: str | None = None,
igdb_local_database: dict | None = None,
igdb_match_database: dict | None = None,
*,
save_to_disk: bool = True,
) -> dict:
if igdb_local_database is None:
igdb_local_database = load_igdb_local_database(release_year=release_year)
if igdb_match_database is None:
igdb_match_database = load_igdb_match_database(release_year=release_year)
fixes_to_igdb_match_database = load_fixes_to_igdb_match_database(
release_year=release_year,
)
required_igdb_ids = []
for igdb_ids in fixes_to_igdb_match_database.values():
required_igdb_ids += igdb_ids
for igdb_ids in igdb_match_database.values():
required_igdb_ids += igdb_ids
augmented_igdb_local_database = igdb_local_database
num_additional_entries = 0
start_time = time.time()
for igdb_id in required_igdb_ids:
igdb_id_as_str = str(igdb_id)
is_a_real_igdb_id = bool(igdb_id > 0)
if is_a_real_igdb_id and igdb_id_as_str not in augmented_igdb_local_database:
# Give as much freedom as possible: we **know** the IGDB ID (and it is a real IGDB ID since it is positive),
# but we ignore the reason why the matching previously failed. It is likely due a combination of missing
# information about the PC release on IGDB, and our parameters constraining the search to PC games.
encapsulated_data = look_up_game_id(
igdb_id,
must_be_available_on_pc=False,
must_be_a_game=False,
)
data = encapsulated_data[0]
augmented_igdb_local_database[igdb_id_as_str] = data
num_additional_entries += 1
start_time = wait_for_cooldown(
num_requests=num_additional_entries,
start_time=start_time,
)
if save_to_disk and num_additional_entries > 0:
save_igdb_local_database(
augmented_igdb_local_database,
release_year=release_year,
)
return augmented_igdb_local_database
def extend_both_igdb_databases(
release_year: str | None = None,
igdb_match_database: dict | None = None,
igdb_local_database: dict | None = None,
*,
verbose: bool = True,
) -> tuple[dict, dict]:
# Manual extension of the match database
extended_igdb_match_database = extend_igdb_match_database(
release_year=release_year,
igdb_match_database=igdb_match_database,
verbose=verbose,
)
# Automatic extension of the local database after the manual extension of the match database
augmented_igdb_local_database = fill_in_blanks_in_the_local_database(
release_year=release_year,
igdb_local_database=igdb_local_database,
igdb_match_database=extended_igdb_match_database,
)
# Manual extension of the local database
extended_igdb_local_database = extend_igdb_local_database(
release_year=release_year,
igdb_local_database=augmented_igdb_local_database,
)
return extended_igdb_match_database, extended_igdb_local_database
def main() -> bool:
release_year = "2018"
(
_extended_igdb_match_database,
_extended_igdb_local_database,
) = extend_both_igdb_databases(release_year=release_year)
return True
if __name__ == "__main__":
main()