def add_song(file_name, path): # Convert if not mp3 if not file_name.endswith(".mp3"): if not converter.convert( path, os.path.join(storage.MUSIC_TEMP_STORAGE, os.path.splitext(file_name)[0] + '.mp3')): return # TODO: check if need to remove old file path = os.path.join(storage.MUSIC_TEMP_STORAGE, os.path.splitext(file_name)[0] + '.mp3') # Read tags if necessary # if correct_tags: # checker.correct_tags(file_path) try: file = MP3(path) except HeaderNotFoundError: return False # Add Song to database divided_name = checker.divide_name(file_name) database.add_song(title=divided_name[0], artist=divided_name[1], path=path, duration=int(file.info.length * 1000), status=u'added') return True
def add_single(conn, pathfile): """ add a single song to database """ if pathfile.endswith(".mp3"): # read metadata tup = c.meta(pathfile) d.add_song(tup, conn) log.info('metadata recorded in the database') # convert mp3 to wav c.convert(pathfile) log.info('audio converted to wav') # read the wav from local directory filename = os.path.basename(pathfile) pathwav = "./music/wav/" + filename[:-3] + "wav" # compute spectrogram and fingerprints framerate, f, t, spect = a.spectrogram(pathwav) fingerprints1 = a.fingerprint(f, spect) fingerprints2 = a.fingerprint2(f, spect, framerate) song_id = d.select_songid(filename, conn) log.info('audio file no. %s recorded in the database', song_id) # add fingerprints to database d.add_fingerprint(filename, t, fingerprints1, fingerprints2, conn) # update fingerprinted status d.update_fingerprinted(song_id, conn) print('Done!', filename, 'added to your database ❤')
def train(folder="audio"): filepaths = song_loader.convert_files_to_songpaths(folder) # load songs for i in filepaths: name, artist, features = train_single(i) print("Adding:", name, "by", artist) database.add_song(features, name, artist) print("Added:", name, "by", artist) database.write_database()
def test_add_song(conn): """test if add_song works as expected""" filename = os.listdir("./music/mp3")[0] pathfile = "./music/mp3/" + filename tup = c.meta(pathfile) d.add_song(tup, conn) # check if the song is added cur = conn.cursor() query = 'SELECT count(*) FROM music where title = %s' val = (filename[:-4], ) cur.execute(query, val) rowcount = cur.fetchall() assert rowcount[0][0] != 0, "add_song works"
def firststep(conn): """ ingest a directory of music for database construction USAGE + this is the prerequisite for all analyses/identification + run this function to get a nicely-built music database + which will be used as the reference for audio matching WHAT IT DOES + sql database contruction + read all mp3 files from a local dir + read metadata + convert mp3 to wav + compute spectrograms and fingerprints + record all info in the database GOOD FOR + lazy guys like me who don't want to contruct db manually """ # create tables if non-exist d.create_table(conn) log.info("database created") # construct database for file in os.listdir("./music/mp3"): if file.endswith(".mp3"): pathfile = "./music/mp3/" + file # read metadata tup = c.meta(pathfile) d.add_song(tup, conn) # convert mp3 to wav c.convert(pathfile) log.info('all metadata recorded in the database') log.info('all audio converted to wav') for file in os.listdir("./music/wav"): if file.endswith(".wav"): pathfile = "./music/wav/" + file # compute spectrogram and fingerprints framerate, f, t, spect = a.spectrogram(pathfile) fingerprints1 = a.fingerprint(f, spect) fingerprints2 = a.fingerprint2(f, spect, framerate) song_id = d.select_songid(file, conn) log.info('audio file no. %s recorded in the database', song_id) # add fingerprints to database d.add_fingerprint(file, t, fingerprints1, fingerprints2, conn) # update fingerprinted status d.update_fingerprinted(song_id, conn) print('Done! Please check out your database ❤')
def upload_file(): if 'username' in loging_session: if 'member_name' in loging_session: username = loging_session['username'] membername = loging_session['member_name'] if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) add_song(membername, filename, url_for('uploaded_file', filename = filename), username) return redirect(url_for('audio')) else: return redirect(url_for('choose')) else: return redirect(url_for('login'))
def add_entry(): """ Adds an entry to the CSV database, and refreshes the home page to update """ username = util.remove_commas_from_string(request.form["name"]) link = util.remove_commas_from_string(request.form["ytLink"]) song = util.remove_commas_from_string(request.form["songName"]) festive = CHRISTMAS_MODE and "christmasSong" in request.form with database.connect_to_database() as db: user_id = database.get_userid(db, username) database.add_song(db, link, song, user_id, month=12 if festive else None) return redirect(url_for('main'))
def _add_song(self, song_no=0, user_name=None, month=None, day=None): if user_name is None: user_name = self.test_user_name song_id = database.add_song(url=self.test_url.format(id=song_no), title=self.test_title.format(id=song_no), user_name=user_name, month=month, day=day) return song_id
def add_entry(): """ Adds an entry to the CSV database, and refreshes the home page to update """ username = request.form['username'] email = request.form['email'] link = utils.remove_commas_from_string(request.form["ytLink"]) song = utils.remove_commas_from_string(request.form["songName"]) festive = CHRISTMAS_MODE and "christmasSong" in request.form try: database.add_song(link, song, username, month=12 if festive else None, email=email) except database.KeyExistsError: pass return redirect(url_for('main'))
def cloudmusic_top_list(): nu = 0 while nu < 4: source = '网易云音乐' if nu == 0: url = 'http://music.163.com/discover/toplist?id=19723756' music_list_name = '云音乐飙升榜' elif nu == 1: url = 'http://music.163.com/discover/toplist?id=3779629' music_list_name = '云音乐新歌榜' elif nu == 2: url = 'http://music.163.com/discover/toplist?id=3778678' music_list_name = '网易原创歌曲榜' else: url = 'http://music.163.com/discover/toplist?id=3778678' music_list_name = '云音乐热歌榜' nu += 1 page = requests.get(url, headers=header, timeout=30) tree = html.fromstring(page.text) #print(page.text) rank_list = tree.xpath('//textarea[@id="song-list-pre-data"]/text()') rank_list = str(rank_list[0]) #正则表达式处理 result = rank_list[1:len(rank_list) - 1] name_result = re.findall('"album":{"id":.*?,"name":"(.*?)"', rank_list) link_result = re.findall('"picUrl":"(.*?)"', rank_list) singer_result = re.findall('"artists":\[{"id":.*?,"name":"(.*?)"', rank_list) song_result = re.findall('"id":(\d*?),"pl"', rank_list) rank_result = [] m = 0 while m < len(song_result): rank_result.append(m + 1) m += 1 song_link = [] for i in song_result: s = 'http://music.163.com/#/song?id=' + i song_link.append(s) database.add_song(source, music_list_name, rank_result, name_result, singer_result, song_link, link_result)
def main(playlist=PLAYLIST_CSV): songs = read_playlist(playlist) with connect_to_database() as db: db_size_before = get_songs_size(db) for url, played, title, user in songs: played = ast.literal_eval(played) assert type(played) is bool user_id = get_userid(db, user) title = title.replace("'", '') added = add_song(db, url, title, user_id) logging.info("%s %s by %s", "Added " if added else "Did not add ", title, user) if played: set_song_played(db, url) logging.info("Set song played: %s", title) db_size_after = get_songs_size(db) no_songs_added = db_size_after - db_size_before logging.info( "Populated database with playlist. Added %d songs. Database size: %d -> %d", no_songs_added, db_size_before, db_size_after)