def queue(mediaObj): log.msg("Queueing %s" % mediaObj) session = Session() inQueue = session.query(Queue).filter(Queue.media_object==mediaObj).filter(Queue.play_date==None).first() if not inQueue: qob = Queue(mediaObj) session.add(qob) session.commit() if not __activeSource: play() else: for key, mediactr in __controllers.items(): log.msg("\tSending Queue Change to %s" % key) mediactr.queueUpdated(QueueActions.ADD, qob) return True else: return False
def readFiles(self): print "Starting to index files" session = Session() fileList = [] hashList = [] for d in ["/home/jlew/Music","/home/jlew/BACKUP_TO_SORT/Music"]: for root, dirs, files in os.walk(d): for f in files: fileList.append( root + "/" + f ) fileLen = len(fileList) print "Found %d Files" % fileLen for i,f in enumerate(fileList): filePath = f.decode('utf-8') filePathSha = sha1(filePath).hexdigest() hashList.append(filePathSha) fileTime = str(os.path.getmtime(f)).decode('UTF-8') item = session.query(MediaObject)\ .filter(MediaObject.source_id==filePathSha)\ .filter(MediaObject.source==self.__module__).first() if item: if item.valid == False: item.setValid(True) for attribute in item.attributes: if attribute.key == u"filetime" and attribute.value != fileTime: title,artist,album = self.getTagInfo(f) item.title = title item.artist = artist item.album = album attribute.value = fileTime print "Updated", item else: title,artist,album = self.getTagInfo(f) print "Indexed %s %s %s" % (artist,album, title) m = MediaObject(self.__module__,title,artist,album, source_id = filePathSha) session.add(m) session.flush() # Flush the session so we get the ID session.add(MediaObjectAttribute(m.id,u"filename", filePath)) session.add(MediaObjectAttribute(m.id,u"filetime", fileTime)) if (i % 500) == 0: print "Commiting Batch" session.commit() session.commit() # Invalidate Files that are no longer found print "Files indexed, checking for missing files" query = session.query(MediaObject)\ .filter(MediaObject.source==self.__module__)\ .filter(MediaObject.valid==True) for obj in query: if obj.source_id not in hashList: print "DEACTIVATED", obj obj.setValidState(False) session.commit() session.close() print "File Indexing Completed"