-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_and_reformat.py
141 lines (119 loc) · 4.09 KB
/
fetch_and_reformat.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
import requests
import json
import os
from datetime import datetime, timezone
API_URL = 'https://fortnitecontent-website-prod07.ol.epicgames.com/content/api/pages/fortnite-game/spark-tracks'
OUTPUT_DIR = 'api/track_api'
MAIN_FILE = 'api/fnfest_content_api.json'
def fetch_available_jam_tracks():
try:
response = requests.get(API_URL)
response.raise_for_status()
# Check the encoding of the response
print(f"Response encoding: {response.encoding}")
# Ensure the response is parsed as JSON
data = response.json()
if not isinstance(data, dict):
print("Error: The fetched data is not a dictionary.")
return None
available_tracks = {k: v for k, v in data.items() if isinstance(v, dict) and v.get('track')}
return available_tracks
except requests.RequestException as e:
print(f'Error fetching available jam tracks: {e}')
return None
except json.JSONDecodeError as e:
print(f'Error decoding JSON: {e}')
return None
def beautify_and_translate(data):
key_mapping = {
"tt": "trackTitle",
"ry": "releaseYear",
"dn": "duration",
"sib": "iconBass",
"sid": "iconDrum",
"sig": "iconGuitar",
"siv": "iconVocals",
"ge": "genres",
"mk": "key",
"mm": "majorMinor",
"ab": "albumName",
"in": "intensities",
"mt": "BPM",
"an": "artistName",
"ar": "ageRating",
"au": "albumArtFilename",
"isrc": "recordingCode",
"su": "songUUID",
"ti": "trackIndex",
"tb": "thumbFilename"
}
intensity_mapping = {
"pb": "proBass",
"pd": "proDrums",
"vl": "vocals",
"pg": "proGuitar",
"gr": "guitar",
"ds": "drums",
"ba": "bass"
}
keys_to_exclude = [
"_title",
"sn",
"mu",
"nu",
"gt",
"ld",
"jc",
"_noIndex",
"_locale",
"_templateName",
"qi",
"_type"
]
for track_id, track_data in data.items():
track_data['lastModified'] = track_data.get('lastModified', datetime.now(timezone.utc).isoformat())
track = track_data['track']
for old_key, new_key in key_mapping.items():
if old_key in track:
track[new_key] = track.pop(old_key)
if 'intensities' in track:
intensities = track['intensities']
for old_key, new_key in intensity_mapping.items():
if old_key in intensities:
intensities[new_key] = intensities.pop(old_key)
# Format the 'ti' value
if 'trackIndex' in track:
track['trackIndex'] = track['trackIndex'].replace('SparksSong:sid_placeholder_', '')
# Format the 'au' value
if 'albumArtFilename' in track:
track['albumArtFilename'] = os.path.basename(track['albumArtFilename'])
# Format the 'tb' value
if 'thumbFilename' in track:
track['thumbFilename'] = os.path.basename(track['thumbFilename'])
# Remove excluded keys
for key in keys_to_exclude:
if key in track_data:
del track_data[key]
if key in track:
del track[key]
if 'intensities' in track and key in track['intensities']:
del track['intensities'][key]
return data
def save_json_files(data):
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
with open(os.path.join(MAIN_FILE), 'w') as f:
json.dump(data, f, indent=4)
for track_id, track_data in data.items():
with open(os.path.join(OUTPUT_DIR, f'{track_id}.json'), 'w') as f:
json.dump(track_data, f, indent=4)
def main():
data = fetch_available_jam_tracks()
if data:
beautified_data = beautify_and_translate(data)
save_json_files(beautified_data)
print(f"Data saved successfully in '{OUTPUT_DIR}' directory.")
else:
print("No data to process.")
if __name__ == '__main__':
main()