def save_uploaded_file (form, form_field, upload_dir, mysql_cursor): """This saves a file uploaded by an HTML form. The form_field is the name of the file input field from the form. For example, the following form_field would be "file_1": <input name="file_1" type="file"> The upload_dir is the directory where the file will be written. If no file was uploaded or if the field does not exist then this does nothing. """ if not form.has_key(form_field): return "Please upload a file." fileitem = form[form_field] if not form.has_key('safe'): return "Naughty!" safe = form['safe'].value if not fileitem.file: return "Please select a file before clicking upload." if not fileitem.filename.endswith('mp3') : return "Please upload MP3s Only" filename = os.path.join(upload_dir, fileitem.filename) if os.path.exists(filename): return "That file already exists!" fout = file (filename, 'wb') while 1: chunk = fileitem.file.read(100000) if not chunk: break fout.write (chunk) fout.close() try: s = pydub.AudioSegment.from_mp3(filename) id3 = mutagen.id3.ID3(filename) if id3.get('TIT2', 'Unknown') == 'Unknown' or id3.get('TPE1', 'Unknown') == 'Unknown': os.remove(filename) return "Could not read ID3 tags, please edit the file to include them: (Artist:%s - Title:%s)" % (id3.get('TPE1', 'Unknown'), id3.get('TIT2', 'Unknown')) mysql_cursor.execute("""INSERT INTO media (location, title, artist, album, duration, safe) VALUES (%s, %s, %s, %s, %s, %s)""", (filename, id3.get('TIT2', 'Unknown'), id3.get('TPE1', 'Unknown'), id3.get('TALB', 'Unknown'), int(s.duration_seconds), safe)) except Exception as e: #remove file if something went wrong! os.remove(filename) return 'Something went wrong! %s - %s' % (file, e) else: return "Added...<br> Title: %s<br>Artist:%s<br>Album:%s<br>Duration:%s<br>Classified as:%s" % ( id3.get('TIT2', 'Unknown'), id3.get('TPE1', 'Unknown'), id3.get('TALB', 'Unknown'), int(s.duration_seconds), safe)
def main(): mysql_connect_object = MySQLdb.connect(host=mysql_host, user=mysql_username, passwd=mysql_password, db=mysql_db) mysql_cursor = mysql_connect_object.cursor() found_media = os.listdir(media_directory) for file in found_media: filename = "%s%s" % (media_directory, file) mysql_cursor.execute("""SELECT id FROM media WHERE location = %s""", (filename,)) if len(mysql_cursor.fetchall()) == 0: try: s = pydub.AudioSegment.from_mp3(filename) id3 = mutagen.id3.ID3(filename) mysql_cursor.execute( """INSERT INTO media (location, title, artist, album, duration, safe) VALUES (%s, %s, %s, %s, %s, %s)""", ( filename, id3.get("TIT2", "Unknown"), id3.get("TPE1", "Unknown"), id3.get("TALB", "Unknown"), int(s.duration_seconds), "goodish", ), ) mysql_connect_object.commit() logging.info( "Added: %s, %s, %s, %s, %s" % ( filename, id3.get("TIT2", "Unknown"), id3.get("TPE1", "Unknown"), id3.get("TALB", "Unknown"), int(s.duration_seconds), ) ) except Exception as e: logging.info("Something couldnt load %s - %s" % (file, e)) else: pass
def main(): mysql_connect_object = MySQLdb.connect(host=mysql_host, user=mysql_username, passwd=mysql_password, db=mysql_db) mysql_cursor = mysql_connect_object.cursor() found_media = os.listdir(media_directory) for file in found_media: filename = '%s%s' % (media_directory, file) mysql_cursor.execute("""SELECT id FROM media WHERE location = %s""", (filename, )) if len(mysql_cursor.fetchall()) == 0: try: s = pydub.AudioSegment.from_mp3(filename) id3 = mutagen.id3.ID3(filename) mysql_cursor.execute( """INSERT INTO media (location, title, artist, album, duration, safe) VALUES (%s, %s, %s, %s, %s, %s)""", (filename, id3.get('TIT2', 'Unknown'), id3.get('TPE1', 'Unknown'), id3.get('TALB', 'Unknown'), int(s.duration_seconds), 'goodish')) mysql_connect_object.commit() logging.info( "Added: %s, %s, %s, %s, %s" % (filename, id3.get('TIT2', 'Unknown'), id3.get('TPE1', 'Unknown'), id3.get( 'TALB', 'Unknown'), int(s.duration_seconds))) except Exception as e: logging.info('Something couldnt load %s - %s' % (file, e)) else: pass
import sys for file in glob.glob('*.mp3'): id3 = mutagen.id3.ID3(file, translate=True) valid_file = True for required_field in ['TPE1', 'TRCK', 'TIT2']: if not id3.has_key(required_field): if required_field == "TPE1": if not id3.has_key("TPE2"): print "ERROR: %s missing field: %s and %s" % (file, "TPE1", "TPE2") else: print "ERROR: %s missing field: %s" % (file, required_field) valid_file = False if valid_file == True: trck = id3.get('TRCK').text[0] if "/" in trck: track_num = int(trck.split("/")[0]) else: track_num = int(trck) artist = id3.get('TPE1').text[0].strip() if artist == "": artist = id3.get('TPE2').text[0].strip() title = id3.get('TIT2').text[0].strip() dest_file = "%02d. %s - %s.mp3" % (track_num, artist, title) dest_file = dest_file.replace("/", "-") print "New file: %s" % (dest_file) os.rename(file, dest_file) else: sys.exit(1)