import os import re import tagpy #import cPickle as pickle from songInfo import SongInfo #import thread class Library: #def __init__(self,prnt): def __init__(self): self.goodTypes = ["ogg","mp3","flac"]#,"wma"] self.reset() #self.scanning = False self.libview = None self.data = None #self.notAdded = open("/home/bjschuma/Desktop/notAdded.txt",'w') # Begin a scan on dir def scan(self,thread,dir): #self.scanning = True self.reset() if self.libview: self.libview.updates() self.path = os.path.expanduser(dir) if os.path.exists(self.path) == False: #print "Directory not found: %s" % dir return self.traverse("") if self.libview: self.libview.stopUpdates() if self.data.options.verbose == True: print "Found",self.count,"songs." self.data.dump(False) def reset(self): self.files = [] self.count = 0 self.artAlb = dict() self.albTrk = dict() self.path = "" # Traverse directorys def traverse(self,dir): # List and sort contents contents = os.listdir(os.path.join(self.path,dir)) contents.sort() for entry in contents: joined = os.path.join(dir,entry) full = os.path.join(self.path,joined) # Call traverse on directorys if os.path.isdir(full): self.traverse(joined) # Add music to library else: tSplit = entry.rsplit('.') type = tSplit[len(tSplit)-1].lower() if (type in self.goodTypes) == True: song = SongInfo() song.filename = os.path.join(self.path,joined) self.files+=[song] #thread.start_new_thread(self.add,(self.hash(joined),self.count)) #self.add(self.hash(joined),self.count) #thread.start_new_thread(self.add,(self.count)) self.add(self.count) self.count += 1 # Hash a file and return list of words #def hash(self,file): # return # file = file.lower() # # Only keep letters and numbers # words = re.sub('[^a-z0-9]',' ',file).split() # return words # Add song to library def add(self,index): info = self.files[index] info.count = 0 info.id = index split = info.filename.rsplit(os.sep) max = 3 if len(split) < 3: max = len(split) for i in range(max): if i==0: info.title = split[len(split)-1] elif i==1: info.album = split[len(split)-2] else: info.artist = split[len(split)-3] try: f = tagpy.FileRef(info.filename) t = f.tag() if t.title != "": info.title = t.title if t.album != "": info.album = t.album if t.artist != "": info.artist = t.artist #info.tnum = str(t.track) a = f.audioProperties() info.setTime(a.length) except: print info.filename #print "here" artist = info.artist.lower() info.artistl = artist album = info.album.lower() info.albuml = album title = info.title.lower() info.titlel = title if (artist in self.artAlb.keys()) == False: self.artAlb[artist] = [album] elif (album in self.artAlb[artist]) == False: self.artAlb[artist] += [album] if ((artist,album) in self.albTrk.keys()) == False: self.albTrk[(artist,album)] = [index] else: self.albTrk[(artist,album)] += [index] #for letter in title: # info.single[letter] = True #for letter in artist: # info.single[letter] = True #for letter in album: # info.single[letter] = True #for i in range(len(title)-1): # str = title[i]+title[i+1] # info.double[str] = True #for i in range(len(artist)-1): # str = artist[i]+artist[i+1] # info.double[str] = True #for i in range(len(album)-1): # str = album[i]+album[i+1] # info.double[str] = True if self.libview: self.libview.insert(info) if self.data.options.verbose == True: print info.filename # Return true if file is in the library #def has(self,file): # words = self.hash(file) # if len(words) == 0: # return -1 # if (words[0] in self.data.map.keys()) == False: # return -1 # indices = set(self.data.map[words[0]]) # for word in words[1:]: # indices = indices & set(self.data.map[word]) # # Return first instance # if len(indices) > 0: # return indices.pop() # return -1