190 lines
4.5 KiB
Python
190 lines
4.5 KiB
Python
import os
|
|
import re
|
|
import tagpy
|
|
import cPickle as pickle
|
|
from libdata import LibData
|
|
from songInfo import SongInfo
|
|
|
|
|
|
class Library:
|
|
#def __init__(self,prnt):
|
|
def __init__(self):
|
|
#self.prnt = prnt
|
|
#self.data = LibData()
|
|
self.goodTypes = ["ogg","mp3"]#,"wma"]
|
|
self.files = []
|
|
self.count = 0
|
|
self.path = ""
|
|
self.scanning = False
|
|
self.artAlb = dict()
|
|
self.albTrk = dict()
|
|
# Build up directory if library save
|
|
#self.save = os.path.expanduser("~")
|
|
#self.save = os.path.join(self.save,".ocarina")
|
|
#self.save = os.path.join(self.save,"library.pickle")
|
|
# Load existing library
|
|
#if os.path.exists(self.save):
|
|
#self.prnt(["Library found, loading..."])
|
|
# print "Library found, loading..."
|
|
# p = pickle.Unpickler(open(self.save))
|
|
# self.data = p.load()
|
|
|
|
|
|
# Begin a scan on dir
|
|
def scan(self,thread,dir):
|
|
#self.data = LibData()
|
|
#self.data.path = os.path.expanduser(dir)
|
|
self.scanning = True
|
|
self.files = []
|
|
self.count = 0
|
|
self.artAlb = dict()
|
|
self.albTrk = dict()
|
|
|
|
self.path = os.path.expanduser(dir)
|
|
if os.path.exists(self.path) == False:
|
|
#self.prnt(["Directory not found: "+dir])
|
|
print "Directory not found: %s" % dir
|
|
return
|
|
print "Scanning: "+self.path
|
|
#self.scanning = True
|
|
self.traverse("")
|
|
self.scanning = False
|
|
#num = len(self.data.files)
|
|
#self.prnt(["Found "+str(num)+" files!"])
|
|
print "Found %s files!" % str(self.count)
|
|
#self.dump()
|
|
|
|
|
|
# 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:
|
|
self.add(self.hash(joined),joined)
|
|
|
|
|
|
# Hash a file and return list of words
|
|
def hash(self,file):
|
|
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,words,file):
|
|
index = len(self.files)
|
|
info = SongInfo()
|
|
info.filename = os.path.join(self.path,file)
|
|
self.files += [info.filename]
|
|
info.count = 0
|
|
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]
|
|
self.files+=[info]
|
|
|
|
if (info.artist in self.artAlb.keys()) == False:
|
|
self.artAlb[info.artist] = [info.album]
|
|
elif (info.album in self.artAlb[info.artist]) == False:
|
|
self.artAlb[info.artist] += [info.album]
|
|
|
|
if (info.album in self.albTrk.keys()) == False:
|
|
self.albTrk[info.album] = [info.title]
|
|
#elif (info.title in self.albTrk[self.info.album]) == False:
|
|
else:
|
|
self.albTrk[info.album] += [info.title]
|
|
|
|
self.count += 1
|
|
#print self.count
|
|
|
|
return
|
|
|
|
#use later for length
|
|
#a = f.audioProperties()
|
|
#info.length = a.length
|
|
|
|
#print file
|
|
|
|
#print info.artist,",", info.title,",", info.album
|
|
#print s1,s2,s3
|
|
|
|
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
|
|
|
|
a = f.audioProperties()
|
|
info.length = a.length
|
|
info.id = len(self.data.files)
|
|
|
|
self.data.files+=[info]
|
|
print info.id
|
|
for word in words:
|
|
if (word in self.data.map.keys()) == True:
|
|
self.data.map[word]+=[index]
|
|
else:
|
|
self.data.map[word] = [index]
|
|
|
|
|
|
# Dump to file
|
|
def dump(self):
|
|
out = open(self.save,'w')
|
|
p = pickle.Pickler(out,2)
|
|
p.dump(self.data)
|
|
out.close()
|
|
|
|
|
|
# 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
|
|
|
|
|
|
def nonBanned(self):
|
|
list = []
|
|
for i in range(len(self.data.files)):
|
|
if self.data.files[i].banned == False:
|
|
list += [i]
|
|
return list
|
|
|
|
|
|
def translate(self,sid):
|
|
#file = self.data.files[sid]
|
|
return self.data.files[sid]
|
|
#print file.title, file.artist, file.album
|
|
return (sid,file.title,file.artist,file.album,file.playCount)
|
|
#return (file.artist,file.album)
|