Skip to content

Instantly share code, notes, and snippets.

@bethropolis
Created November 3, 2023 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bethropolis/39dc023ab18516137880e2295f834526 to your computer and use it in GitHub Desktop.
Save bethropolis/39dc023ab18516137880e2295f834526 to your computer and use it in GitHub Desktop.
import json
import random;
import os
folder_name = "Playlist"
tracks=os.listdir(folder_name)
while True:
songName = input("\n\nEnter the song or artist name(Enter q to quit): ").lower()
def readfile(file):
file_path = folder_name+"/"+file+".json"
if(not os.path.isfile(file_path)):
return False
with open(file_path) as f:
data = json.load(f)
total_songs = len(data)
for song in data:
found = False
if song['song'].lower() == songName:
found = True
print("Below are great alternatives to ", song['song'], ":\n")
if song['artist'].lower() == songName:
found = True
print("Recommended alternatives for", song['artist'], "songs:\n")
if found:
print(file ,"alternatives:")
for i in range(3):
song_number = random.randint(0, total_songs)
song = data[song_number]['song'] # Access the 'song' key to get the song name
artist = data[song_number]['artist'] # Access the 'artist' key to get the artist name
print(str(i+1) + ". " + song + " by " + artist)
return True
else:
return False
if songName=="q":
print("Thank you for using our recommendation system.... ")
break
checker = False
for track in tracks:
track_name = str.split(track, ".")
if(readfile(track_name[0]) and track_name[1] == "json"):
checker = True
break
if checker == False:
print("Song not found. Can you please check the spelling.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment