-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_parser.py
159 lines (107 loc) · 4.37 KB
/
game_parser.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
import os
import json
import tomllib
import requests
with open("config.toml", "rb") as f:
config = tomllib.load(f)
def search_game_with_API(title):
payload = {'api_key': config['APIKEY'],
'format': 'json',
'resources': 'game',
'limit': 10,
'field_list': 'name,guid',
'query': title}
response = requests.get('https://www.giantbomb.com/api/search/',
params=payload,
headers = {"User-Agent": "guicli-call"})
response.raise_for_status()
raw_data = response.json()
return raw_data['results']
def fetch_game_guid(title):
# first perform a search with the game's title to find it's guid
payload = {'api_key': config['APIKEY'],
'format': 'json',
'resources': 'game',
'limit': 1,
'field_list': 'guid',
'query': title}
response = requests.get('https://www.giantbomb.com/api/search/',
params=payload,
headers = {"User-Agent": "guicli-call"})
response.raise_for_status()
raw_data = response.json()
#print(raw_data)
'''
{
'error': 'OK',
'limit': 1,
'offset': 0,
'number_of_page_results': 1,
'number_of_total_results': 362,
'status_code': 1,
'results': [
{'guid': '3030-2512',
'resource_type': 'game'}
],
'version': '1.0'}
'''
game_guid = raw_data['results'][0]['guid']
return game_guid
def fetch_game_data(guid):
# check if game is cached
if os.path.isdir(os.path.join(config['CACHE_DIR'], guid)):
game_dir = os.path.join(config['CACHE_DIR'], guid)
print(f"Found cached information in {game_dir}")
return
# use the guid to fetch the necessary game info
payload = {'api_key': config['APIKEY'],
'format': 'json',
'field_list': 'image,genres,platforms,developers,publishers,name,original_release_date'}
response = requests.get(f'https://www.giantbomb.com/api/game/{guid}/',
params=payload,
headers = {"User-Agent": "guicli-call"})
response.raise_for_status()
raw_data = response.json()
game_data = {'title': raw_data['results']['name'],
'original_release_date':raw_data['results']['original_release_date'],
'platforms': raw_data['results']['platforms'],
'developers': raw_data['results']['developers'],
'publishers': raw_data['results']['publishers'],
'genres': raw_data['results']['genres'],
'guid': guid}
entries_to_remove = ('api_detail_url', 'id', 'site_detail_url', 'abbreviation')
for element in game_data['platforms']:
for e in entries_to_remove:
element.pop(e, None)
for element in game_data['developers']:
for e in entries_to_remove:
element.pop(e, None)
for element in game_data['publishers']:
for e in entries_to_remove:
element.pop(e, None)
for element in game_data['genres']:
for e in entries_to_remove:
element.pop(e, None)
#print(game_data)
os.mkdir(os.path.join(config['CACHE_DIR'], guid))
with open(os.path.join(config['CACHE_DIR'], guid, 'game_data.json'), 'w') as f:
json.dump(game_data, f, indent=4)
image_url = str(raw_data['results']['image']['original_url'])
image_url = image_url.replace('\\','')
print(image_url)
#wget.download(image_url, os.path.join(config['CACHE_DIR'],guid,'image.jpg'), bar=False)
# wget throughs 404
# https://stackoverflow.com/questions/30229231/python-save-image-from-url
img_data = requests.get(image_url).content
with open(os.path.join(config['CACHE_DIR'],guid,'image.jpg'), 'wb') as handler:
handler.write(img_data)
def search_cache_data(guid):
if os.path.isdir(os.path.join(config['CACHE_DIR'], guid)):
game_dir = os.path.join(config['CACHE_DIR'], guid)
print(f"Found cached information in {game_dir}")
with open(os.path.join(game_dir, 'game_data.json')) as json_file:
game_data = json.load(json_file)
return game_data, os.path.join(game_dir, 'image.jpg')
else:
print('Game not in cache!')
return None