Exemplo n.º 1
0
class MediaRequestLoop(Thread):
    MAX_REQUESTS = 32

    def __init__(self, absolutePath:str):
        Thread.__init__(self, name = "MediaRequestLoop")
        self.queue = queue.Queue(self.MAX_REQUESTS)
        self._absolutePath = absolutePath
        self._mediaDatabase = None
        self.searchColumns = ['title', 'artist', 'albumArtist', 'album']
        self.groupByColumns = ['artist', 'album']
        self.orderByColumns = [('lastPlayed', 'DESC'),
                                       ('lastModified', 'DESC'),
                                       ('artist', 'ASC'),
                                       ('title', 'ASC')]
        self._isRunning = False

    def run(self):
        # The database needs to be created here because sqlite requires that
        # all requests to a database be made by the same thread.
        self._mediaDatabase = MediaDatabase(self._absolutePath)
        self._rescanLibrary()
        self._isRunning = True
        while self._isRunning:
            request = self.queue.get(block = True)
            self._processRequest(request)

    def addRequest(self, request:MediaRequest):
        try:
            self.queue.put(request, block = False)
        except queue.Full:
            logger.warn("Failed adding request, queue is full")

    def quit(self):
        self.addRequest(MediaRequest(type=MediaRequest.Operations.QUIT))

    def _processRequest(self, request:MediaRequest):
        if request.type == MediaRequest.Operations.SEARCH:
            results = self._search(request.query)
            if request.delegate is not None:
                request.delegate.onRequestComplete(request, results)
        elif request.type == MediaRequest.Operations.RESCAN:
            self._rescanLibrary()
            if request.delegate is not None:
                request.delegate.onRequestComplete(request, "Rescan finished")
        elif request.type == MediaRequest.Operations.QUIT:
            self._isRunning = False
        else:
            logger.info("Unknown media request type '" + request.type + "'")

    def _search(self, searchQuery, matchFromStart = True):
        return self._mediaDatabase.search(searchQuery,
                                          searchColumns = self.searchColumns,
                                          orderByColumns = self.orderByColumns,
                                          matchFromStart = matchFromStart)

    def _rescanLibrary(self):
        self._mediaDatabase.rescan()
Exemplo n.º 2
0
 def run(self):
     # The database needs to be created here because sqlite requires that
     # all requests to a database be made by the same thread.
     self._mediaDatabase = MediaDatabase(self._absolutePath)
     self._rescanLibrary()
     self._isRunning = True
     while self._isRunning:
         request = self.queue.get(block = True)
         self._processRequest(request)
Exemplo n.º 3
0
import sys
from lucidity.media.media import MediaDatabase
from time import time

if __name__ == "__main__":
    searchArgument = "./tests/resources"
    if len(sys.argv) == 2:
        searchArgument = sys.argv[1]

    startTime = time()
    mediaDb = MediaDatabase("media.db")
    mediaDb.addLocation(searchArgument)
    # mediaDb.rescan()
    stopTime = time()
    print("Elapsed time: ", stopTime - startTime, "seconds")