Skip to content

Commit 773ed3a

Browse files
Refactor dict literals and add test_limit arg
Replace many dict() calls with literal {} notation for consistency and readability (auth headers, youtube headers, request_dict, full_dict, buckets, etc.). Add test_count tracking and a --test_limit CLI argument to limit items collected when --test_mode is enabled; the loop now stops when test_count >= test_limit. Minor help text and messaging tweaks for test mode and clearer dict initializations; small change to how empty appended items are created ({} instead of dict()).
1 parent 59e61a8 commit 773ed3a

1 file changed

Lines changed: 61 additions & 52 deletions

File tree

src/update_db.py

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ def igdb_authorization(client_id: str, client_secret: str) -> dict:
3636
dict
3737
Dictionary containing access token and expiration.
3838
"""
39-
auth_headers = dict(
40-
Accept='application/json',
41-
client_id=client_id,
42-
client_secret=client_secret,
43-
grant_type='client_credentials'
44-
)
39+
auth_headers = {
40+
'Accept': 'application/json',
41+
'client_id': client_id,
42+
'client_secret': client_secret,
43+
'grant_type': 'client_credentials',
44+
}
4545

4646
token_url = 'https://id.twitch.tv/oauth2/token'
4747

@@ -88,7 +88,7 @@ def get_youtube(video_ids: list) -> dict:
8888
videos = ','.join(video_ids)
8989
fields = 'items(id,snippet(title,description,thumbnails,localized))'
9090
url = f'{uri}?id={videos}&key={args.youtube_api_key}&part=snippet&fields={fields}'
91-
headers = dict(Accept='application/json')
91+
headers = {'Accept': 'application/json'}
9292

9393
session = requests_cache.CachedSession(
9494
cache_name='cache/youtube_cache',
@@ -107,37 +107,37 @@ def get_data():
107107
is appended to the games list. Games are appended to platforms. Videos metadata is also added to the games list.
108108
Individual files will be written to disk for each item.
109109
"""
110-
request_dict = dict(
111-
characters=dict(
112-
fields=[
110+
request_dict = {
111+
'characters': {
112+
'fields': [
113113
'character_gender.name',
114114
'character_species.name',
115115
'games',
116116
'mug_shot.url',
117117
'name',
118118
],
119-
write_all=True,
120-
),
121-
collections=dict(
122-
fields=[
119+
'write_all': True,
120+
},
121+
'collections': {
122+
'fields': [
123123
'games',
124124
'name',
125125
'slug',
126126
'url',
127127
],
128-
write_all=True,
129-
),
130-
franchises=dict(
131-
fields=[
128+
'write_all': True,
129+
},
130+
'franchises': {
131+
'fields': [
132132
'games',
133133
'name',
134134
'slug',
135135
'url',
136136
],
137-
write_all=True,
138-
),
139-
games=dict(
140-
fields=[
137+
'write_all': True,
138+
},
139+
'games': {
140+
'fields': [
141141
'age_ratings.organization.name',
142142
'age_ratings.rating_category.rating',
143143
'aggregated_rating',
@@ -174,21 +174,21 @@ def get_data():
174174
'videos.name',
175175
'videos.video_id',
176176
],
177-
append=dict(
178-
characters=dict(
179-
fields=[
177+
'append': {
178+
'characters': {
179+
'fields': [
180180
'id',
181181
'gender',
182182
'mug_shot',
183183
'name',
184184
'species',
185-
]
186-
)
187-
),
188-
write_all=False,
189-
),
190-
platforms=dict(
191-
fields=[
185+
],
186+
},
187+
},
188+
'write_all': False,
189+
},
190+
'platforms': {
191+
'fields': [
192192
'abbreviation',
193193
'alternative_name',
194194
'generation',
@@ -218,27 +218,28 @@ def get_data():
218218
'versions.summary',
219219
'versions.url',
220220
],
221-
append=dict(
222-
games=dict(
223-
fields=[
221+
'append': {
222+
'games': {
223+
'fields': [
224224
'id',
225225
'cover',
226226
'name',
227227
'release_dates',
228-
]
229-
)
230-
),
231-
write_all=True,
232-
),
233-
)
228+
],
229+
},
230+
},
231+
'write_all': True,
232+
},
233+
}
234234
limit = 500
235-
full_dict = dict()
235+
full_dict = {}
236236

237237
for end_point, end_point_dict in request_dict.items():
238238
print(f'now processing endpoint: {end_point}')
239239
offset = 0
240240
result = True
241-
full_dict[end_point] = dict()
241+
full_dict[end_point] = {}
242+
test_count = 0
242243

243244
while result:
244245
try:
@@ -257,7 +258,10 @@ def get_data():
257258
full_dict[end_point][item['id']] = item
258259

259260
if args.test_mode:
260-
break
261+
test_count += 1
262+
if test_count >= args.test_limit:
263+
result = False
264+
break
261265

262266
offset += limit
263267

@@ -297,7 +301,7 @@ def get_data():
297301
except KeyError:
298302
full_dict[end_point][item_id_dest][item_type] = []
299303
finally:
300-
full_dict[end_point][item_id_dest][item_type].append(dict())
304+
full_dict[end_point][item_id_dest][item_type].append({})
301305

302306
for field in item_type_dict['fields']:
303307
try:
@@ -310,7 +314,7 @@ def get_data():
310314

311315
# create buckets and get list of all videos
312316
print('creating buckets / collecting video ids')
313-
buckets = dict()
317+
buckets = {}
314318
all_videos = []
315319
for game_id, game_data in full_dict['games'].items():
316320
# games
@@ -321,11 +325,9 @@ def get_data():
321325
try:
322326
buckets[bucket]
323327
except KeyError:
324-
buckets[bucket] = dict()
328+
buckets[bucket] = {}
325329
finally:
326-
buckets[bucket][game_id] = dict(
327-
name=game_data['name']
328-
)
330+
buckets[bucket][game_id] = {'name': game_data['name']}
329331

330332
# videos
331333
try:
@@ -349,7 +351,7 @@ def get_data():
349351
print('collecting video metadata')
350352

351353
end_point = 'videos'
352-
full_dict[end_point] = dict()
354+
full_dict[end_point] = {}
353355

354356
all_videos.sort()
355357

@@ -473,7 +475,14 @@ def get_platform_cross_reference():
473475
'-t',
474476
'--test_mode',
475477
action='store_true',
476-
help='Only write one item file per end point, per request.',
478+
help='Only write limited items per endpoint.',
479+
)
480+
parser.add_argument(
481+
'--test_limit',
482+
type=int,
483+
required=False,
484+
default=500,
485+
help='Number of items to collect per endpoint when test_mode is enabled (default: 10).',
477486
)
478487
parser.add_argument(
479488
'-i',

0 commit comments

Comments
 (0)