def save_deezer_data_to_db(input): # "https://api.deezer.com/search" + "?", params = {'q': name} for key, value in input.items(): if key == 'data': number_of_items = len(value) # iterate all objects and save to data for i in range(number_of_items): each_object = input['data'][i] artist = Artist() artist.Name = each_object['artist']['name'] artist.PictureURL = each_object['artist']['picture_medium'] artist.ArtistDeezerID = each_object['artist']['id'] try: artist = Artist.objects.get(Name=artist.Name) except Artist.DoesNotExist: artist.save() except Artist.MultipleObjectsReturned: pass album = Album() album.Artist = artist album.Title = each_object['album']['title'] album.PictureURL = each_object['album']['cover_medium'] album.AlbumDeezerID = each_object['album']['id'] album.ArtistDeezerID = each_object['artist']['id'] try: album = Album.objects.get(Title=album.Title) except Album.DoesNotExist: album.save() except Album.MultipleObjectsReturned: pass song = Song() song.Album = album song.Artist = artist song.Title = each_object['title'] song.PictureURL = each_object['album']['cover_medium'] song.PreviewURL = each_object['preview'] song.AlbumDeezerID = each_object['album']['id'] song.ArtistDeezerID = each_object['artist']['id'] song.SongDeezerID = each_object['id'] try: song = Song.objects.get(Title=song.Title) except Song.DoesNotExist: song.save() except Song.MultipleObjectsReturned: pass
def song_form(request): from musicapp.models import Song songs = Song.objects.all() if request.method == 'POST': print("in if") form = SongForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] singer = form.cleaned_data['singer'] movie = form.cleaned_data['movie'] genre = form.cleaned_data['genre'] playlist = form.cleaned_data['playlist'] c =Song(name = name, singer = singer, movie = movie, genre = genre, playlist = playlist) c.save() return HttpResponseRedirect('songs/') else: print("in else") form = SongForm() return render(request, 'musicapp/songform.html', {'form': form})
def save_deezer_album_songs_to_db(result, album_id, artist_id): for data in result['data']: album = Album.objects.get(AlbumDeezerID__exact=album_id) artist = Artist.objects.get(ArtistDeezerID__exact=artist_id) song = Song() song.Album = album song.Artist = artist song.Title = data['title'] song.PictureURL = album.PictureURL song.PreviewURL = data['preview'] song.AlbumDeezerID = album.AlbumDeezerID song.ArtistDeezerID = artist.ArtistDeezerID song.SongDeezerID = data['id'] try: song = Song.objects.get(Title=song.Title) except Song.DoesNotExist: song.save() except Song.MultipleObjectsReturned: pass
def get_songs(request): #todo: interact with models to get list of songs from musicapp.models import Song if request.method == 'GET': songs = [] for asong in Song.objects.all(): songs.append(_extract_song_object_to_dict(asong)) songs_json = json.dumps(songs) return HttpResponse(songs_json) else: #print('Name : "%s"' %request.body) #print('Name: "%s"' % request.body["name"]) thatsong = json.loads(request.body) songs = Song.objects.all() print(thatsong["singer"]) c = Song(name = thatsong["name"], singer = "viraj", movie = thatsong["movie"], genre = thatsong["genre"], playlist = thatsong["playlist"]) c.save() return HttpResponse("OK")
print("Populated " + str(lineNum - 1) + " genres") with open(playlistsFile) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') lineNum = 0 for row in csv_reader: if lineNum != 0: playlist = Playlist(name=row[0]) playlist.save() lineNum += 1 print("Populated " + str(lineNum - 1) + " playlists") with open(songsFile) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') lineNum = 0 for row in csv_reader: if lineNum != 0: genre = Genre.objects.filter(name=row[2]).first() album = Album.objects.filter(name=row[3]).first() song = Song(title=row[0], file_location=row[1], genre=genre, album=album) song.save() lineNum += 1 print("Populated " + str(lineNum - 1) + " songs")
def searchRequest(name="", conn="", artist="", album=""): if conn == "": searchRequest(name, conn="artist") searchRequest(name, conn="album") searchRequest(name, conn="track") # Send request to search information result = requests.get("https://api.deezer.com/search/" + conn + "?", params={'q': name}) # Check if the HTTP response is OK if result.status_code == 200: result = result.json() # Don't need to populate to much if (result['total'] > 25): nbr_results = 25 else: nbr_results = result['total'] # Browse the different element in the JSON answer for i in range(len(result['data'])): data = result['data'][i] # If an artist field is found, search his album if data['type'] == "artist": print(data['name'], ",", data['type']) # Create a new artist artistDB = Artist() artistDB.Name = data['name'] artistDB.PictureURL = data['picture_medium'] artistDB.ArtistDeezerID = data['id'] # If the artist is not already in the DB, save it try: artistDB = Artist.objects.get(Name=artistDB.Name) except Artist.DoesNotExist: artistDB.save() # Search the albums of the artist searchRequest(name=data['name'], artist=data['name'], conn="album") # Populate the database only with the first artist found break # If an album field is found, search his tracks if data['type'] == "album" and\ (data['artist']['name'] == artist or artist == ""): print(artist, ",", data['title'], ",", data['type']) artistDB = Artist.objects.get(Name=artist) # Create a new album albumDB = Album() albumDB.Artist = artistDB albumDB.Title = data['title'] albumDB.PictureURL = data['cover_medium'] albumDB.AlbumDeezerID = data['id'] albumDB.ArtistDeezerID = data['artist']['id'] # If the album is not already in the DB, save it try: albumDB = Album.objects.get(Title=albumDB.Title, Artist=artistDB) except Album.DoesNotExist: albumDB.save() # Search the songs of the artist searchRequest(name=data['title'], conn="track", artist=artist, album=data['title']) # Populate the database only with the four first albums found if i > 3: break if data['type'] == "track" and\ (data['artist']['name'] == artist or artist == "") and\ (data['album']['title'] == album or album == ""): print(artist, ",", album, ",", data['title'], ",", data['type']) artistDB = Artist.objects.get(Name=artist) albumDB = Album.objects.get(Title=album, Artist=artistDB) # Create a new song songDB = Song() songDB.Album = albumDB songDB.Artist = artistDB songDB.Title = data['title'] songDB.PictureURL = data['album']['cover_medium'] songDB.PreviewURL = data['preview'] songDB.AlbumDeezerID = data['album']['id'] songDB.ArtistDeezerID = data['artist']['id'] songDB.SongDeezerID = data['id'] # If the song is not already in the DB, save it try: songDB = Song.objects.get(Title=songDB.Title, Album=albumDB, Artist=artistDB) except Song.DoesNotExist: songDB.save()
def query_post(request, song_name = "ameya",song_singer = "ameya",song_movie = "ameya",song_genre = "ameya",song_playlist = "ameya"): from musicapp.models import Song songs = Song.objects.all() c =Song(name = song_name, singer = song_singer, movie = song_movie, genre = song_genre, playlist = song_playlist) c.save() return HttpResponse("saved")