def insert_songs(self, playlist_id): #not tested for song in range(0,len(self.songs['items'])): song_to_db=Song(order = song, spotify_id = self.songs['items'][song]['track']['id'], name = self.songs['items'][song]['track']['name'], album = self.songs['items'][song]['track']['album']['name'], artist = self.songs['items'][song]['track']['album']['artists'][0]['name'], popularity = self.songs['items'][song]['track']['popularity'], playlist_id = playlist_id, danceability = self.songs['items'][song]['features']['danceability'],#alternative-danceability = self.features[song]['features']['danceability'], energy = self.songs['items'][song]['features']['energy'], key = self.songs['items'][song]['features']['key'], mode = self.songs['items'][song]['features']['mode'], speechiness = self.songs['items'][song]['features']['speechiness'], acousticness =self.songs['items'][song]['features']['acousticness'], instrumentalness =self.songs['items'][song]['features']['instrumentalness'], liveness = self.songs['items'][song]['features']['liveness'], valence = self.songs['items'][song]['features']['valence'], tempo = self.songs['items'][song]['features']['tempo'], uri = self.songs['items'][song]['features']['uri'], time_signature = self.songs['items'][song]['features']['time_signature']) song_from_db=Song.query.filter_by(spotify_id=song_to_db.spotify_id, playlist_id=playlist_id).first() if song_from_db!=None: song_from_db=Song.query.filter_by(playlist_id=playlist_id, order=song).first() if song_from_db!=None: if song_from_db.spotify_id!=song_to_db.spotify_id: #if there is difference at song orders or different songs added etc. here i delete rest of rows bellow (> asc order)!!! try: Song.query.filter(Song.id >= song_from_db.id).delete() db.session.commit() db.session.add(song_to_db) db.session.commit() except Exception as e: print(e) db.session.rollback() else: try: db.session.add(song_to_db) db.session.commit() except Exception as e: print(e) db.session.rollback()
def add(request): title = "Main Window" artist = Artist() album = Album() songG = Song() if request.method == 'POST': form = AddLyrics(request.POST) if form.is_valid(): artistName = form.cleaned_data['artist'] albumName = form.cleaned_data['album'] albumPicture = form.cleaned_data['albumPicture'] position = albumPicture.find('albums') - 1 albumPicture = albumPicture[position:] print "albumPicture = %s" % (albumPicture) songName = form.cleaned_data['song'] songG.lyrics = form.cleaned_data['lyrics'] print "adding new song..." try: artist = Artist.objects.get(artist=artistName) print "artist %s already exist" % (artist) except: artist.artist = artistName artist.save() print "artist %s was added" % (artist) try: album = Album.objects.get(album=albumName) #renew album photo if albumPicture: album.photo = albumPicture album.save() print "album picture renew " + albumPicture print "album %s is already exist" % (album) except: album.album = albumName album.artist = artist if albumPicture: album.photo = albumPicture print "album picture was added " + albumPicture album.save() print "album %s was added" % (album) # TODO: here you should implement logic for adding different translations for song # try: # song = Song.objects.get(song=songName) # print "sadasdasds" # render_to_response('song.html', { # "title": title, # "artist": song.artist, # "song" : song.song # }) # except: songG.song = songName songG.artist = artist songG.album = album songG.save() print "song %s was added" % (songG) print "artist.id = %s, album.id = %s, song.id = %s" % (artist.id, album.id, songG.id) return song(request, artist.id, album.id, songG.id) else: form = AddLyrics() return render_to_response('add.html', { 'form': form, })
def upload_song(user, file_mp3_handle=None, file_source_handle=None, max_song_len=None, band=None, song_title=None, song_album="", song_comments="", filename_appendix=""): """ inputs: user: the person uploading stuff file_mp3_handle: the handle from the form file_source_handle: the handle from the form. need to also pass user if you want this to work. max_song_len: how many seconds long a song can be. None means unlimited. band: band model - who to attribute the song to. song_title: id3 title to enforce upon the mp3 song_album: id3 album to enforce upon the mp3 song_comments: the author's own comments for the song. filename_appendix: use this if you want to add something to the filename, before the extension. * uploads the mp3 and source file if applicable * creates the proper files and id3 tags * generates the waveform image outputs a dict: success: boolean - whether it was successful or not reason: string - what went wrong if success is False. song: new song model. it will have been saved, but then more things added to it so it needs to be saved again. it will have the following fields properly populated: owner source_file waveform_img mp3_file band title album length studio samples effects generators """ data = { 'success': False, 'song': None, } assert band != None, "Band parameter isn't optional." assert song_title != None, "Song title parameter isn't optional." song = Song() song.band = band song.owner = user song.title = song_title song.length = 0 song.album = song_album # save so we can use relational fields song.save() # create the root node for song comments node = SongCommentNode() node.song = song node.owner = user node.content = song_comments node.save() song.comment_node = node if file_mp3_handle != None: result = handle_mp3_upload(file_mp3_handle, song, max_song_len, filename_appendix=filename_appendix) if not result['success']: song.delete() return result # upload the source file if file_source_handle != None: handle_project_upload(file_source_handle, user, song, filename_appendix=filename_appendix) # we incremented bytes_used in band, so save it now band.save() data['song'] = song data['success'] = True return data