def search_disk_for_mp3_files(search_dir, root_dir): from shutil import copyfile existing_files = os.listdir(root_dir) for (dirpath, dirnames, filenames) in walk(search_dir): for file_name in filenames: abs_dirpath = abspath(dirpath) if abs_dirpath != root_dir and isMp3File(file_name) and file_name not in existing_files: try: copyfile(abs_dirpath + '/' + file_name, root_dir + '/' + file_name) print('Copying file = {file} in dir = {dir} to {root_dir}'\ .format(file = file_name, dir = dirpath, root_dir = root_dir)) except: pass
def update_database(conn, files): errors = [] for file in files: if not isMp3File(file['path']): errors.append('{fi} is not an mp3 file.'.format(fi = file['name'])) continue mp3_file = eyed3.load(file['path']) tag = mp3_file.tag if tag: artist = mp3_file.tag.artist album = mp3_file.tag.album title = mp3_file.tag.title genre = mp3_file.tag.genre year = mp3_file.tag.release_date or mp3_file.tag.recording_date if genre: genre = genre.name insert_into_db(conn = conn, file = file, artist = artist, album = album, title = title, genre = genre, year = year) else: title, album, artist, genre, year = search_apple_api(file['name']) if title and title != UNKNOWN: errors.append('No tag found for mp3 file {fi}. '\ 'Successfully found tag info using Apple API.'.\ format(fi = file['name'])) insert_into_db(conn = conn, file = file, artist = artist, album = album, title = title, genre = genre, year = year) else: errors.append('No tag found for mp3 file {fi}.'\ .format(fi = file['name'])) # All values default to UNKNOWN insert_into_db(conn = conn, file = file) return True, errors
def add_file(root, myfile, path, newCollection, formKind, formTag): """ passed the root, file, path from an os walk newCollection was created to hold the new database object formKind, formTag are from the create collections form returns musician (null unless it was found for a song) to allow redirect if appropriate """ musician = None theFile = unicode(os.path.join(root, myfile)) try: statinfo = os.stat(theFile) except Exception as ex: # in this case I want to see what the exception is, but the file is ok and # will not be ignored utility.log("SKIP (os.stat) %s unhandled exception %s" % (theFile, type(ex).__name__)) return musician if not statinfo.st_size: utility.log("SKIP file with 0 size %s" % theFile) return musician base = unicode(os.path.basename(theFile)) mTitle, extension = os.path.splitext(base) mTitle = unicode(mTitle) extension = unicode(extension) adate = time.gmtime(os.path.getmtime(theFile)) fdate = time.strftime('%Y-%m-%d', adate) if mp3.isMp3File(theFile): nc = newCollection sSlug = slugify(unicode('%s%s-sg' % (nc.slug, mTitle))) song = add_song(mTitle, base, sSlug, nc) song.date_added = fdate if len(formTag): xSlug = slugify(unicode('%s' % (formTag))) xTag = add_tag(formTag, xSlug) song.tags.add(xTag) # fill in extra details from the id3 tag on file if possible musician = fix_song(song, theFile, nc) song.save() else: # This section is for the info on mkv files # currently not being used, but may be in future # in this section new movies will be added to the passed collection ## try: ## with open(theFile,'rb') as f: ## mkv = enzyme.MKV(f) ## print (mkv) ## print (type(mkv)) ## f.close() ##except enzyme.MalformedMKVError: ## print("BOGUS MKV skip") nc = newCollection if as_movie(extension): mSlug = slugify(unicode('%s%s-mv' % (nc.slug, mTitle))) movie = add_movie(mTitle, base, mSlug, nc, formKind) movie.date_added = fdate if len(formTag): xSlug = slugify(unicode('%s' % (formTag))) xTag = add_tag(formTag, xSlug) movie.tags.add(xTag) movie.save() elif as_picture(extension): mSlug = slugify(unicode('%s%s-pict' % (nc.slug, mTitle))) picture = add_picture(mTitle, base, mSlug, nc) picture.date_added = fdate picture.save() # supposed to be faster with details=False #itags = exifread.process_file(theFile, details=False) fname = unicode(theFile) try: #print("before open file open file %s" % fname) with open(fname, 'r') as f: #print("file open ok now get info") itags = exifread.process_file(f, details=False) #print("got info %s " % fname) #print (itags) #print tag['Image DateTime'] for tag in itags: if tag in 'Image DateTime': value = itags[tag] picture.data1 = value try: picture.year = int(value[:4]) except ValueError: # if its not a number just # don't set the year pass if tag in 'EXIF DateTimeOriginal': value = itags[tag] picture.data2 = value #print("2 %s" % value) if tag in 'EXIF DateTimeDigitized': value = itags[tag] #print("3 %s" % value) picture.data3 = value picture.save() except NameError: #print("ERROR (opening for info) NameError file %s" % fname) # this is ok, file is OK just ignore the info pass except Exception as ex: # in this case I want to see what the exception is, but the file is ok and # will not be ignored utility.log( "ERROR (opening for info) %s unhandled exception %s" % (fname, type(ex).__name__)) if len(formTag): xSlug = slugify(unicode('%s' % (formTag))) xTag = add_tag(formTag, xSlug) picture.tags.add(xTag) picture.save() else: utility.log("SKIPPING - unhandled extension %s/%s" % (path, base)) return musician