Example #1
0
def update_viewed(media_id, elapsed=None, delete=False):
    try:
        database = Database()

        try:
            info = database.select_media(int(media_id))

        except Exception:
            info = {}

        if info.get("category") in config.getlist("viewed", "no_history"):
            return "0"

        if elapsed:
            database.update_viewed(media_id, elapsed)

        elif delete:
            database.delete_viewed(media_id)

        else:
            database.insert_viewed(media_id)

        return "0"

    except Exception as excp:
        log.error("Update viewed failed: %s", excp)

        return "1"
Example #2
0
    def _connect(self):
        self.connected = False

        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)

        try:
            self.connect((self.host, self.port))

        except socket.gaierror as excp:
            log.error("%s failed to connect: %s", self.name, excp)

            raise

        # Send our name to the server to register the connection with.
        #
        try:
            while True:
                if self.qthread:
                    sent = self.send(CommunicationQThread.packer.pack(self.name))
                
                else:
                    sent = self.send(CommunicationThread.packer.pack(self.name))

                if sent != 0:
                    break

            self.connected = True

            log.warn("%s sent name" % self.name)

        except socket.error as excp:
            log.error("%s failed to send name: %s", self.name, excp)
Example #3
0
def update_viewed(media_id, elapsed=None, delete=False):
    try:
        database = Database()

        try:
            info = database.select_media(int(media_id))

        except Exception:
            info = {}

        if info.get("category") in config.getlist("viewed", "no_history"):
            return "0"

        if elapsed:
            database.update_viewed(media_id, elapsed)

        elif delete:
            database.delete_viewed(media_id)

        else:
            database.insert_viewed(media_id)

        return "0"

    except Exception as excp:
        log.error("Update viewed failed: %s", excp)

        return "1"
Example #4
0
    def handle_read(self):
        """
        Stream incoming messages into MessagePack and action them when ready.
        """

        self.unpacker.feed(self.recv(1))

        for message in self.unpacker:
            if not self.name:
                # The first message received from a new connection will be the
                # name of the client, which we can store and use in future to
                # access this connection.
                #
                try:
                    Communicate.connections[message] = self
                    self.name = message

                    log.info("%s connected", self.name)

                except TypeError as excp:
                    log.error("Failed to set name: %s", excp)

            else:
                log.info("Received from %s: %s", self.name, message)

                # Attempt to call the requested method on the proxy.
                #
                method = message.keys()[0]

                try:
                    getattr(CommunicationServer.proxy, method)(message[method])

                except AttributeError as excp:
                    log.error("Failed to call method %s: %s", method, excp)
Example #5
0
    def handle_close(self):
        self.close()

        try:
            Communicate.connections.pop(self.name, None)

        except TypeError as excp:
            log.error("Failed to remove connection: %s", excp)

        log.warn("%s disconnected", self.name)
Example #6
0
def run_index():
    try:
        Index().index()

        return "0"

    except Exception as excp:
        log.error("Index failed: %s", excp)

        return "1"
Example #7
0
def run_index():
    try:
        Index().index()

        return "0"

    except Exception as excp:
        log.error("Index failed: %s", excp)

        return "1"
Example #8
0
    def action(self, message):
        """
        Try to perform the received playback action on the Control object.
        """

        try:
            function, arguments = message

            getattr(self.control, function)(*arguments)

            log.warn("Performed action: %s", function)

        except Exception as excp:
            log.error("Action %s failed: %s", function, excp)
            log.error(traceback.format_exc())
Example #9
0
def get_snakes(queue):
    data = {}

    try:
        snakes = Communicate.connections

    except Exception as excp:
        log.error("Failed to get Snakes: %s", excp)

    if queue:
        for snake in snakes.keys():
            if not Proxy._snakes[snake].get("media_id"):
                snakes.pop(snake, None)

    data["snakes"] = snakes.keys()

    return data
Example #10
0
    def _call_api(self, bits):
        """
        Make a request to the Head's API.
        """

        url = "http://%s:%s/medusa/%s/%s" % (config.get("head", "host"),
                                      config.get("ports", "head"),
                                      config.get("head", "api_base"),
                                      "/".join(str(b) for b in bits))

        try:
            return requests.get(url).json()

        except Exception as excp:
            log.error("API call to %s failed: %s", url, excp)

            return {}
Example #11
0
def get_snakes(queue):
    data = {}

    try:
        snakes = Communicate.connections

    except Exception as excp:
        log.error("Failed to get Snakes: %s", excp)

    if queue:
        for snake in snakes.keys():
            if not Proxy._snakes[snake].get("media_id"):
                snakes.pop(snake, None)

    data["snakes"] = snakes.keys()

    return data
Example #12
0
    def _send_server(self, names, message):
        """
        Send a message to the named connection(s).
        """

        try:
            for name in names:
                connection = Communicate.connections[name]
                connection.send(self.packer.pack(message))

                log.info("Sent to %s: %s", name, message)

        except (KeyError, RuntimeError) as excp:
            log.error("Send failed: %s", excp)

            return False

        return True
Example #13
0
    def __init__(self, proxy, host, port):
        asyncore.dispatcher.__init__(self)

        log.info("CommunicationServer initialised")

        CommunicationServer.proxy = proxy()
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()

        try:
            self.bind((host, port))

        except socket.error as excp:
            log.error("Failed to bind socket: %s", excp)

            raise

        log.warn("Listening on port: %s", port)

        self.listen(5)
Example #14
0
    def handle_read(self):
        """
        Stream incoming messages into MessagePack, and action them when ready.
        """

        try:
            self.unpacker.feed(self.recv(1))

            for message in self.unpacker:
                log.info("%s received: %s", self.name, message)

                # Attempt to call the requested method on the proxy.
                #
                try:
                    method = message.keys()[0]

                    try:
                        getattr(self.proxy, method)(message[method])

                    except AttributeError as excp:
                        log.error("Failed to call method %s: %s", method, excp)

                except AttributeError as excp:
                    log.error("Failed to call a method: %s", excp)

        except socket.error as excp:
            log.error("Failed to receive: %s", excp)
Example #15
0
def send_to_snake(snake, action, value=None):
    if action in ["play", "stop"]:
        send_to_snake(snake, "empty_queue")

    arguments = [action]

    if value:
        arguments.append([value])

    else:
        arguments.append([])

    try:
        Communicate().send([snake], {"action": arguments})

        if action == "play":
            categories.queue_tracks(snake, value)

        return "0"

    except Exception as excp:
        log.error("%s on %s failed: %s", action.title(), snake, excp)

        return "1"
Example #16
0
def send_to_snake(snake, action, value=None):
    if action in ["play", "stop"]:
        send_to_snake(snake, "empty_queue")

    arguments = [action]

    if value:
        arguments.append([value])

    else:
        arguments.append([])

    try:
        Communicate().send([snake], {"action": arguments})

        if action == "play":
            categories.queue_tracks(snake, value)

        return "0"

    except Exception as excp:
        log.error("%s on %s failed: %s", action.title(), snake, excp)

        return "1"
Example #17
0
    def _send_client(self, message):
        """
        Check for an active connection and then send a message to the server.
        """

        try:
            # Check that we are connected.
            if self.connection.connected:
                # Write the message to the buffer so that it will be sent.
                self.connection.buffer_ = self.packer.pack(message)

                log.info("%s buffered: %s", self.name, message)

            else:
                log.error("Send failed: No active connection found")

                return False

        except AttributeError as excp:
            log.error("Send failed: %s", excp)

            return False

        return True
Example #18
0
    def select_next_tracks(self, media_id):
        log.info("Perfoming select next tracks for media: %s", media_id)

        try:
            media_id = int(media_id)

            with self.database:
                data = self.database.select_media_by_id(media_id)[media_id]

        except Exception as excp:
            log.error("Select next tracks failed: %s", excp)

            return []

        if data["category"] != "Music":
            return []

        artist = data["name_one"]
        album = data["name_two"]

        with self.database:
            data = self.database.select_tracks(artist, album)

        return data
Example #19
0
    def select_next_tracks(self, media_id):
        log.info("Perfoming select next tracks for media: %s", media_id)

        try:
            media_id = int(media_id)

            with self.database:
                data = self.database.select_media_by_id(media_id)[media_id]

        except Exception as excp:
            log.error("Select next tracks failed: %s", excp)

            return []

        if data["category"] != "Music":
            return []

        artist = data["name_one"]
        album = data["name_two"]

        with self.database:
            data = self.database.select_tracks(artist, album)

        return data
Example #20
0
    def find_media(self, category, path, expressions):
        media = []
        deep = False

        if category in config.getlist("index", "deep"):
            deep = True

        if category in config.getlist("index", "audio_categories"):
            allowed_formats = config.getlist("index", "audio_formats")

        else:
            allowed_formats = config.getlist("index", "video_formats")

        for root, directories, files in os.walk(unicode(path)):
            data = {}
            paths = []
            data["category"] = category

            for f in files:
                if f.startswith("."):
                    continue

                sub_data = {}
                sub_paths = []
                sub_data["category"] = category

                f = os.path.join(root, f)
                short_path = f.replace("%s/" % os.path.dirname(path), "", 1)
                paths.append(short_path)
                sub_paths.append(short_path)

                extension = os.path.splitext(os.path.basename(f))[-1].replace(
                    ".", "", 1)

                if extension not in allowed_formats:
                    continue

                if not deep and data.get("extension"):
                    continue

                data["extension"] = extension
                sub_data["extension"] = extension

                match_path = os.path.splitext(short_path)[0]

                for expression in expressions:
                    matches = expression.match(match_path)

                    if matches:
                        break

                if not matches:
                    log.error("Failed to expression match %s", match_path)

                    continue

                result = getattr(categories,
                                 "parse_%s" % category.lower())(matches)

                data.update(result)
                sub_data.update(result)

                try:
                    modified = int(os.stat(f).st_mtime)

                except Exception:
                    modified = 0

                data["modified"] = modified
                sub_data["modified"] = modified

                if not sub_paths or not sub_data.get("name_one"):
                    continue

                sub_data["paths"] = sub_paths

                if deep:
                    media.append(sub_data)

            if not paths or not data.get("name_one"):
                continue

            data["paths"] = paths

            if not deep:
                media.append(data)

        return media
Example #21
0
    def find_media(self, category, path, expressions):
        media = []
        deep = False

        if category in config.getlist("index", "deep"):
            deep = True

        if category in config.getlist("index", "audio_categories"):
            allowed_formats = config.getlist("index", "audio_formats")

        else:
            allowed_formats = config.getlist("index", "video_formats")

        for root, directories, files in os.walk(unicode(path)):
            data = {}
            paths = []
            data["category"] = category

            for f in files:
                if f.startswith("."):
                    continue

                sub_data = {}
                sub_paths = []
                sub_data["category"] = category

                f = os.path.join(root, f)
                short_path = f.replace("%s/" % os.path.dirname(path), "", 1)
                paths.append(short_path)
                sub_paths.append(short_path)

                extension = os.path.splitext(os.path.basename(f))[-1].replace(".", "", 1)

                if extension not in allowed_formats:
                    continue

                if not deep and data.get("extension"):
                    continue

                data["extension"] = extension
                sub_data["extension"] = extension

                match_path = os.path.splitext(short_path)[0]

                for expression in expressions:
                    matches = expression.match(match_path)

                    if matches:
                        break

                if not matches:
                    log.error("Failed to expression match %s", match_path)
                    
                    continue

                result = getattr(categories,
                                 "parse_%s" % category.lower())(matches)

                data.update(result)
                sub_data.update(result)

                try:
                    modified = int(os.stat(f).st_mtime)

                except Exception:
                    modified = 0

                data["modified"] = modified
                sub_data["modified"] = modified

                if not sub_paths or not sub_data.get("name_one"):
                    continue

                sub_data["paths"] = sub_paths

                if deep:
                    media.append(sub_data)

            if not paths or not data.get("name_one"):
                continue

            data["paths"] = paths

            if not deep:
                media.append(data)

        return media