def bulk_upload_songs(songfile): """ Given a file of songs, will insert them into the database. Will not insert songs that are not (title, author) unique Returns a list of failed songs that trigger IntegrityError Exception """ errors = [] # skip headers songfile.readline() # Extract information from each row and create song objects, validating info. for line in songfile: fields = line.strip().split(",") song = Song() song, status = set_song_values(song, fields) if status is not None: errors.append([fields, status]) continue else: try: song.save() except IntegrityError: errors.append([fields, "Failed to write to DB. Possible duplicate"]) if len(errors) == 0: errors.append("No errors. All songs successfully inserted") return errors
def vote(request,song_id): # we should get the song in the DB from the index page, so we # won't be adding dupe records try: song = Song.objects.get(pk=song_id) # this exception should occur when we vote from the search results # page, in which case we'll make a new DB record and give it one vote except Song.DoesNotExist: song = Song(id_code=song_id, title=request.POST['title'], artist=request.POST['artist'], album=request.POST['album'], votes=0) # are we voting up or down? assume up in case of forgotten POST data if ('vote_up' in request.POST): vote_up = request.POST['vote_up'] else: vote_up = "False" if (vote_up == "True"): song.votes += 1; # elif (song.votes > 0): # if voting down, ensure vote # will remain nonnegative else: song.votes -= 1; # else, do nothing (don't vote down) song.save() # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. We redirect to the song-level results/info page return HttpResponseRedirect(reverse('songs:results', args=(song.id_code,)))
def setUp(self): # load all the songs into the DB for i in songs: foo = Song(name=i[0]) foo.save() # must also add a venue foo = Venue(name='test', city='test', state=4, country=0) foo.save()
def process_one_line(line, count, debug=False): line = line.rstrip("\n\r") line = line.rstrip('/') folders = line.split('/') if len(folders) == 3: if (debug): print('folders=', folders) service_folder = folders[0] # if service_folder.find("Kabbalat Shabbat") >= 0: service_name = valid_dir_name.match(service_folder).group(2) if (debug): print('ServiceName=', service_name) service_key = service_name.replace(" ", "") srv = ServiceName.objects.get(pk=service_key) if (debug): print('srv.name=', srv.name) song_folder = folders[1] if (debug): print('SongFolder=', song_folder) song_name = valid_dir_name.match(song_folder).group(2) if (debug): print('SongName=', song_name) page_number = int(valid_dir_name.match(song_folder).group(1)) if (debug): print('PageNumber=', page_number) file_folder = folders[2] file_name = valid_dir_name.match(file_folder).group(2) if (debug): print('FileName=',file_name) # Figure out the extension ext='' if file_name.lower().endswith(".mp3") : ext = "mp3" elif file_name.lower().endswith(".pdf") : ext = "pdf" elif file_name.lower().endswith(".jpg") : ext = "jpg" if (debug): print('Extension=',ext) sg = Song( service_name=srv, name=song_name.replace(" ", ""), display=song_name, s3_obj_key=line, extension=ext, page_number=page_number, seq_number=page_number, file_name=file_name) sg.save() if (debug): print('sequence number = ', count)
def handle(self, *args, **options): # add all songs that do not exist newsongs = 0 for i in songs: print('Adding {0}'.format(i[0])) try: song = Song.objects.get(name=i[0]) # update the song short name song.shortname = i[1] except: # song does not exist song = Song(name=i[0], shortname=i[1]) newsongs += 1 song.save() print('Added {0} new songs.'.format(newsongs))
def addSong(request): if request.method == 'POST': form = SongForm(request.POST) if form.is_valid(): # Process form data from form.cleaned_data newTrackNum = len( Song.objects.filter( playlistID__exact=form.cleaned_data['playlistID'])) currentSong = Song() currentSong.songName = form.cleaned_data['songName'] currentSong.songUrl = form.cleaned_data['songUrl'] currentSong.playlistID = form.cleaned_data['playlistID'] currentSong.playlistPosition = newTrackNum currentSong.save() pl_id = currentSong.playlistID return HttpResponseRedirect("/songs/%d&tracknum=%d" % (pl_id, newTrackNum)) else: form = SongForm() return render(request, 'songs/addSong.html', {'form': form})
def handle(self, *args, **kwargs): print 'Beginning import....' attrs = ['title', 'show', 'gender', 'song_style', 'additional_info', 'character_type', 'low_note', 'high_note', 'tessitura', 'tempo', 'character_name', 'original_artist', 'year', 'pre_post', 'composer', 'media_link', 'buy_sheet', 'buy_audio'] book = xlrd.open_workbook('apps/songs/management/data/beltBookExcel.xlsx') sheet = book.sheets()[0] for i in xrange(1, sheet.nrows): song = Song() for c in xrange(len(sheet.row(i))): attribute_str = attrs[c] try: setattr(song, attribute_str, sheet.row(i)[c].value) except: pass song.save() print 'Done'
def upload_songs(): path = r"/Users/daniel/emelody/mysite/songs.txt" f = open(path, 'r') songs = [] for line in f: # Lines are in the following format: # Artist - Title.mp3 line = line.replace('.mp3', '') line = line.split('-') artist = line[0].strip() song = line[1].strip() songs.append((artist, song)) f.close() # Populate the Artist table for song in songs: artist = song[0] try: db_artist = Artist.objects.get(name=artist) except Artist.DoesNotExist: db_artist = Artist(name=artist) db_artist.save() # Populate the Song table for song in songs: artist_name = song[0] title = song[1] try: artist = Artist.objects.get(name=artist_name) try: db_song = Song.objects.get(title=title, artist=artist) except Song.DoesNotExist: db_song = Song(title=title, artist=artist) db_song.save() except Artist.DoesNotExist: continue
def song_add(request, song_name, duration): song = Song(name=song_name, duration=duration) song.save() return HttpResponse("Song was added wiht id {}".format(song.pk))
for line in f: # Lines are in the following format: # Artist - Title.mp3 line = line.replace('.mp3', '') line = line.split(' - ') artist = line[0].strip() song = line[1].strip() songs.append((artist, song)) f.close() # Populate the Artist table for song in songs: artist = song[0] try: db_artist = Artist.objects.get(name=artist) except Artist.DoesNotExist: db_artist = Artist(name=artist) db_artist.save() # Populate the Song table for song in songs: artist = song[0] title = song[1] try: db_song = Song.objects.get(title=title, artist=artist) except Song.DoesNotExist: db_song = Song(title=title, artist=artist) db_song.save()
def save(self, request): song = Song() song.user = request.user domain_name = '' if self.cleaned_data['audio_url']: domain_name = self.cleaned_data['audio_url'].split('/') song.audio_url = self.cleaned_data['audio_url'] if self.cleaned_data['vedio_url']: domain_name = self.cleaned_data['vedio_url'].split('/') song.vedio_url = self.cleaned_data['vedio_url'] if self.cleaned_data['flash_url']: domain_name = self.cleaned_data['flash_url'].split('/') song.flash_url = self.cleaned_data['flash_url'] if len(domain_name) >= 2: song.source = domain_name[2] if song.flash_url: s_size = _get_swf_size(song.source) song.flash_width = s_size[0] song.flash_height = s_size[1] song.title = self.cleaned_data['title'] song.singer = self.cleaned_data['singer'] song.album = self.cleaned_data['album'] song.genre = SongGenre.objects.get(id=self.cleaned_data['genre']) if self.cleaned_data['song_cover_url'].find('images/artist.png') == -1: song.song_cover_url = self.cleaned_data['song_cover_url'] song.intro = self.cleaned_data['intro'] song.show_media = True #当链接是百度那个下载链接(但不能在线引用播放)时,转成flash if song.audio_url and song.audio_url.find('zhangmenshiting') != -1 and song.audio_url.find('baidu') != -1: song.audio_url = None m = {'artist' : song.singer, 'name' : song.title, } song.flash_url = 'http://box.baidu.com/widget/flash/mbsong.swf?%s' % (urllib.urlencode(m)) song.flash_width = 400 song.flash_height = 95 song.save() #加入电台播放 if song.audio_url and request.POST.get('add_to_playlist'): _add_to_playlist(song, request.user) #发布到新浪微博 if (request.POST.get('share_swf_to_weibo') and song.flash_url) or (request.POST.get('share_to_weibo') and song.audio_url): current_site = get_current_site(request) domain = current_site.domain public_radio_url = reverse('songs.views.public_radio',args=(song.id,)) from social.views import _post_weibo s_url = 'http://%s%s' % (domain, public_radio_url) msg = '[%s-%s]%s' %(song.title, song.singer, song.intro) _post_weibo(request, msg, s_url) request.user.get_profile() request.user.get_profile().songs = request.user.get_profile().songs + 1 request.user.get_profile().save()
def handle(self, *args, **options): # import the scan_tool we will use spec = importlib.util.spec_from_file_location( 'scan_tool', Setting.objects.get(key='scan_tool').value) scan_tool = importlib.util.module_from_spec(spec) sys.modules[spec.name] = scan_tool spec.loader.exec_module(scan_tool) # new artists, songs and metadata are added by "admin" admin_id = User.objects.get(username='******').pk for path in options['paths']: # scan the uploaded file try: info = scan_tool.scan(path) except Exception as e: raise ValueError("Failed to parse uploaded file: %s" % str(e)) # also calculate the hash with open(path, 'rb') as f: filehash = hashlib.sha1(f.read()).digest() # construct song meta dict for adding new entries file_info = { 'file_type': info['file_type'], 'sample_rate': info['sample_rate'], 'channels': info['channels'], 'bit_rate': info['bit_rate'], 'duration': timedelta(seconds=info['duration']), 'hash': filehash } song_info = {} changed_fields = 'name filepath' if 'tags' in info: if 'title' in info['tags']: song_info['name'] = info['tags']['title'] else: song_info['name'] = path if 'album' in info['tags']: song_info['info'] = info['tags']['album'] changed_fields += ' info' if 'date' in info['tags']: song_info['date'] = datetime.strptime( info['tags']['date'], '%Y-%m-%d') changed_fields += ' release_date' if 'artist' in info['tags']: artist_name = info['tags']['artist'] changed_fields += ' artist' else: song_info['name'] = path artist_name = None # get the artist from the DB - if it doesn't exist, we need to create it if artist_name: try: artist = Artist.objects.get(name=artist_name) except Artist.DoesNotExist: artist = Artist(name=artist_name) artist.save() artist_meta = ArtistMeta(artist=artist, name=artist_name) artist_meta.save() else: artist = None # Everything is parsed. We're committed to import now! # create the song and meta, and attach the song to it. with open(path, 'rb') as f: imported_filename = default_storage.save( upload_to(None, path), f) # file has been imported to our local filesystem # now we can create an SongFile object around it song_file = SongFile(filepath=imported_filename, **file_info) song_file.save() # add the Song song = Song(**song_info) song.song_file = song_file song.save() song.artist.set([artist]) song_meta = SongMeta(song=song, reviewed=True, accepted=True, changed_fields=changed_fields, submitter_id=admin_id, song_file=song_file, **song_info) song_meta.save() song_meta.artist.set([artist]) self.stdout.write( self.style.SUCCESS('Successfully imported file "%s"' % path))
def setUp(self): s = Song(url = "fake", title = "fake", artist = "fake", artwork = "a") s.save()