Spaces:
Running
Running
File size: 2,136 Bytes
73a30d9 |
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 |
import json
def convert_song_format(original_song: dict, song_id: int) -> dict:
"""
Convert song data from original format to target format.
Args:
original_song (dict): Dictionary containing original song metadata.
song_id (int): ID to assign to the converted song entry.
Returns:
dict: Converted song dictionary.
"""
return {
"id": str(song_id),
"title": original_song.get("Song Name ", "").strip(),
"artist": original_song.get("Singer(s) ", "").strip(),
"albumArtUrl": original_song.get("Thumbnail", "").strip(),
"audioUrl": original_song.get("Play Online", "").strip()
}
def read_json_file(file_path: str) -> list:
"""
Read a JSON file and return the content as a dictionary.
Args:
file_path (str): Path to the JSON file.
Returns:
dict or list: Parsed JSON data.
"""
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
def write_json_file(data, file_path: str, indent: int = 2):
"""
Write data to a JSON file.
Args:
data (dict or list): Data to write to the file.
file_path (str): Path to the output JSON file.
indent (int): Indentation level for formatting (default is 2).
"""
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=indent)
def main():
bollywood_song_metadata_json = "bollywood_song_metadata.json"
list_of_original_song = read_json_file(bollywood_song_metadata_json)
# print(list_of_original_song)
songs_json = "songs.json"
list_of_song= []
# list_of_song = read_json_file(songs_json)
i = len(list_of_song)
print(f"initial len of song list {len(list_of_song)}")
for original_song in list_of_original_song:
i = i + 1
converted = convert_song_format(original_song, song_id=i)
list_of_song.append(converted)
print(f"final len of song list {len(list_of_song)}")
print(list_of_song)
write_json_file(list_of_song, songs_json)
if __name__ == "__main__":
main()
|