Exemplo n.º 1
0
def __create_artwork_cache(book, pixbuf, size):
    """
    Creates a resized cache version of the given pixbuf and saves it 
    in the cozy cache folder under a unique identifier. 
    :param book: Book which the artwork is from
    :param pixbuf: Pixbuf to be cached
    :param size: Size for the cached version
    :return: Resized pixbuf
    """
    query = ArtworkCache.select().where(ArtworkCache.book == book.id)
    gen_uuid = ""

    if query.exists():
        gen_uuid = str(query.first().uuid)
    else:
        gen_uuid = str(uuid.uuid4())
        ArtworkCache.create(book = book, uuid=gen_uuid)

    cache_dir = os.path.join(os.path.join(get_cache_dir(), "artwork"), gen_uuid)
    if not os.path.exists(cache_dir):
        os.makedirs(cache_dir)

    resized_pixbuf = __resize_pixbuf(pixbuf, size)
    file_path = os.path.join(cache_dir, str(size) + ".jpg")
    if not os.path.exists(file_path):
        try:
            resized_pixbuf.savev(file_path, "jpeg", ["quality", None], ["95"])
        except Exception as e:
            log.warning("Failed to save resized cache albumart for following uuid: " + gen_uuid)
            log.warning(e)

    return resized_pixbuf
Exemplo n.º 2
0
def __load_pixbuf_from_cache(book, size):
    """
    """
    pixbuf = None

    query = ArtworkCache.select().where(ArtworkCache.book == book.id)
    if query.exists():
        uuid = query.first().uuid
    else:
        return None

    cache_dir = os.path.join(get_cache_dir(), "artwork")
    cache_dir = os.path.join(cache_dir, uuid)

    try:
        if os.path.exists(cache_dir):
            file_path = os.path.join(cache_dir, str(size) + ".jpg")
            if os.path.exists(file_path):
                pixbuf = GdkPixbuf.Pixbuf.new_from_file(os.path.join(cache_dir, str(size) + ".jpg"))
            else:
                return None
    except Exception as e:
        log.warning(e)
        return None

    return pixbuf
Exemplo n.º 3
0
    def __update_metadata(self, track):
        if track is None:
            track = get_current_track()
        if track is None:
            self.__metadata = {
                "mpris:trackid":
                GLib.Variant("o", "/org/mpris/MediaPlayer2/TrackList/NoTrack")
            }
            self.refresh = True
        else:
            self.__metadata["mpris:trackid"] = self.__track_id
            track_number = track.number
            if track_number is None:
                track_number = 1
            self.__metadata["xesam:trackNumber"] = GLib.Variant(
                "i", track_number)
            self.__metadata["xesam:title"] = GLib.Variant("s", track.name)
            self.__metadata["xesam:album"] = GLib.Variant("s", track.book.name)
            self.__metadata["xesam:artist"] = GLib.Variant(
                "as", [track.book.author])
            self.__metadata["mpris:length"] = GLib.Variant(
                "x", track.length * 1000 * 1000)
            self.__metadata["xesam:url"] = GLib.Variant(
                "s", "file:///" + track.file)

            query = ArtworkCache.select().where(
                ArtworkCache.book == track.book.id)
            if query.exists():
                uuid = query.first().uuid
                cache_dir = get_cache_dir()
                cache_dir = os.path.join(cache_dir, uuid)
                file_path = os.path.join(cache_dir, "180.jpg")
                if file_path:
                    self.__metadata["mpris:artUrl"] = GLib.Variant(
                        "s", "file://" + file_path)
Exemplo n.º 4
0
def clean_db():
    """
    Delete everything from the database except settings.
    """
    q = Track.delete()
    q.execute()
    q = Book.delete()
    q.execute()
    q = ArtworkCache.delete()
    q.execute()
Exemplo n.º 5
0
def delete_artwork_cache():
    """
    Deletes the artwork cache completely.
    """
    cache_dir = os.path.join(get_cache_dir(), "artwork")

    import shutil
    if os.path.exists(cache_dir):
        shutil.rmtree(cache_dir)

    q = ArtworkCache.delete()
    q.execute()