song_json_api_hf / convert_song_format.py
niharika17032001's picture
Create Dockerfile
73a30d9
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()