Skip to content

Instantly share code, notes, and snippets.

@dopuskh3
Created October 5, 2009 10:39
Show Gist options
  • Save dopuskh3/202044 to your computer and use it in GitHub Desktop.
Save dopuskh3/202044 to your computer and use it in GitHub Desktop.
def indexFilesFromPath(dir, idxPath):
initVM(CLASSPATH)
writer = IndexWriter(idxPath, StandardAnalyzer(), True)
count = 1
for root, dirs, files in os.walk(dir):
for file in files:
#print "- "+str(root)+" "+file
sfx = file[-3:]
if sfx.lower() not in [ "mp3","mp4", "ogg", "flac" ]:
continue
filepath = os.path.join(root, file)
try:
doc = Document()
f = tagpy.FileRef(filepath)
title = unicode(f.tag().title)
artist = unicode(f.tag().artist)
album = unicode(f.tag().album)
genre = unicode(f.tag().genre)
path = unicode(filepath)
doc.add(Field("title", title, Field.Store.YES, Field.Index.TOKENIZED))
doc.add(Field("artist", artist, Field.Store.YES, Field.Index.TOKENIZED))
doc.add(Field("album",album, Field.Store.YES, Field.Index.TOKENIZED))
doc.add(Field("genre",genre, Field.Store.YES, Field.Index.TOKENIZED))
doc.add(Field("all", album + u" " + artist + u" " + title, Field.Store.YES, Field.Index.TOKENIZED))
doc.add(Field("path", path, Field.Store.YES, Field.Index.UN_TOKENIZED))
print "\r %5d files indexed"%count, # (%s/%s/%s)..."%(count, f.tag().artist, f.tag().album. f.tag().title)
count+=1
writer.addDocument(doc)
except Exception, e:
#print str(e)
# print "+"
continue
print "Done %d"%count
writer.optimize()
writer.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment