コード例 #1
0
ファイル: wkserver.py プロジェクト: rstemmer/WandKalender
class WKServerConfig(Config):
    """
    This class provides the access to the MusicDB configuration file.
    """
    def __init__(self, filename):
        Config.__init__(self, filename)
        self.fs = Filesystem("/")

        logging.info("Reading and checking WKServer Configuration")

        # [meta]
        self.meta = META()
        self.meta.version           = self.Get(int, "meta",     "version",      1)


        # [websocket]
        self.websocket = WEBSOCKET()
        self.websocket.address      = self.Get(str, "websocket","address",      "127.0.0.1")
        self.websocket.port         = self.Get(int, "websocket","port",         9000)
        self.websocket.url          = self.Get(str, "websocket","url",          "wss://localhost:9000")
        self.websocket.apikey       = self.Get(str, "websocket","apikey",       None)
        if not self.websocket.apikey:
            logging.warning("Value of [websocket]->apikey is not set!")

        self.caldav = CALDAV()
        self.caldav.username     = self.Get(str, "caldav","username",          "user")
        self.caldav.password     = self.Get(str, "caldav","password",          "password")
        self.caldav.url          = self.Get(str, "caldav","url",               "https://localhost:443")

        # [TLS]
        self.tls = TLS()
        self.tls.cert               = self.GetFile( "tls",      "cert",         "/dev/null")
        self.tls.key                = self.GetFile( "tls",      "key",          "/dev/null")
        if self.tls.cert == "/dev/null" or self.tls.key == "/dev/null":
            logging.warning("You have to set a valid TLS certificate and key!")


        # [log]
        self.log        = LOG()
        self.log.logfile            = self.Get(str, "log",      "logfile",      "stderr")
        self.log.loglevel           = self.Get(str, "log",      "loglevel",     "WARNING")
        if not self.log.loglevel in ["DEBUG", "INFO", "WARNING", "ERROR"]:
            logging.error("Invalid loglevel for [log]->loglevel. Loglevel must be one of the following: DEBUG, INFO, WARNING, ERROR")
        self.log.debugfile          = self.Get(str, "log",      "debugfile",    None)
        if self.log.debugfile == "/dev/null":
            self.log.debugfile = None
        self.log.ignore             = self.Get(str, "log",      "ignore",       None, islist=True)


        # [debug]
        self.debug      = DEBUG()     


        logging.info("\033[1;32mdone")



    def GetDirectory(self, section, option, default):
        """
        This method gets a string from the config file and checks if it is an existing directory.
        If not it prints a warning and creates the directory if possible.
        If it fails with an permission-error an additional error gets printed.
        Except printing the error nothing is done.
        The \"invalid\" path will be returned anyway, because it may be OK that the directory does not exist yet.

        The permissions of the new created directory will be ``rwxrwxr-x``
        
        Args:
            section (str): Section of an ini-file
            option (str): Option inside the section of an ini-file
            default (str): Default directory path if option is not set in the file

        Returns:
            The value of the option set in the config-file or the default value.
        """
        path = self.Get(str, section, option, default)
        if self.fs.IsDirectory(path):
            return path

        # Create Directory
        logging.warning("Value of [%s]->%s does not address an existing directory. \033[1;30m(Directory \"%s\" will be created)", section, option, path)
        try:
            self.fs.CreateSubdirectory(path)
        except Exception as e:
            logging.error("Creating directory %s failed with error: %s.", path, str(e))

        # Set mode
        mode = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
        try:
            self.fs.SetAttributes(path, None, None, mode);
        except Exception as e:
            logging.error("Creating directory %s failed with error: %s.", path, str(e))

        return path # return path anyway, it does not matter if correct or not. Maybe it will be created later on.


    def GetFile(self, section, option, default, logger=logging.error):
        """
        This method gets a string from the config file and checks if it is an existing file.
        If not it prints an error.
        Except printing the error nothing is done.
        The \"invalid\" will be returned anyway, because it may be OK that the file does not exist yet.
        
        Args:
            section (str): Section of an ini-file
            option (str): Option inside the section of an ini-file
            default (str): Default file path if option is not set in the file
            logger: Logging-handler. Default is logging.error. logging.warning can be more appropriate in some situations.

        Returns:
            The value of the option set in the config-file or the default value.
        """
        path = self.Get(str, section, option, default)
        if not self.fs.IsFile(path):
            logger("Value of [%s]->%s does not address an existing file.", section, option)
        return path # return path anyway, it does not matter if correct or not. Maybe it will be created later on.
コード例 #2
0
ファイル: musicdb.py プロジェクト: rstemmer/musicdb
class MusicDBConfig(Config):
    """
    This class provides the access to the MusicDB configuration file.
    """
    def __init__(self, filename):
        Config.__init__(self, filename)
        self.fs = Filesystem("/")

        logging.info("Reading and checking MusicDB Configuration")

        # [meta]
        self.meta = META()
        self.meta.version           = self.Get(int, "meta",     "version",      1)
        if self.meta.version < 2:
            logging.warning("Version of musicdb.ini is too old. Please update the MusicDB Configuration!")


        # [server]
        self.server = SERVER()
        self.server.pidfile         = self.Get(str, "server",   "pidfile",      "/opt/musicdb/data/musicdb.pid")
        self.server.statedir        = self.GetDirectory("server","statedir",    "/opt/musicdb/data/mdbstate")
        self.server.fifofile        = self.Get(str, "server",   "fifofile",     "/opt/musicdb/data/musicdb.fifo")
        self.server.webuiconfig     = self.Get(str, "server",   "webuiconfig",  "/opt/musicdb/data/webui.ini")


        # [websocket]
        self.websocket = WEBSOCKET()
        self.websocket.address      = self.Get(str, "websocket","address",      "127.0.0.1")
        self.websocket.port         = self.Get(int, "websocket","port",         9000)
        self.websocket.url          = self.Get(str, "websocket","url",          "wss://localhost:9000")
        self.websocket.opentimeout  = self.Get(int, "websocket","opentimeout",  10)
        self.websocket.closetimeout = self.Get(int, "websocket","closetimeout",  5)
        self.websocket.apikey       = self.Get(str, "websocket","apikey",       None)
        if not self.websocket.apikey:
            logging.warning("Value of [websocket]->apikey is not set!")


        # [TLS]
        self.tls = TLS()
        self.tls.cert               = self.GetFile( "tls",      "cert",         "/dev/null")
        self.tls.key                = self.GetFile( "tls",      "key",          "/dev/null")
        if self.tls.cert == "/dev/null" or self.tls.key == "/dev/null":
            logging.warning("You have to set a valid TLS certificate and key!")


        # [database]
        self.database = DATABASE()
        self.database.path          = self.GetFile( "database", "path",         "/opt/musicdb/data/music.db")


        # [music]
        self.music = MUSIC()
        self.music.path             = self.GetDirectory("music",    "path",     "/var/music")
        self.music.owner            = self.Get(str, "music",    "owner",        "user")
        self.music.group            = self.Get(str, "music",    "group",        "musicdb")
        try:
            pwd.getpwnam(self.music.owner)
        except KeyError:
            logging.warning("The group name for [music]->owner is not an existing UNIX user!")
        try:
            grp.getgrnam(self.music.group)
        except KeyError:
            logging.warning("The group name for [music]->group is not an existing UNIX group!")

        ignorelist = self.Get(str, "music",    "ignoreartists","lost+found")
        ignorelist = ignorelist.split("/")
        self.music.ignoreartists = [item.strip() for item in ignorelist]

        ignorelist = self.Get(str, "music",    "ignorealbums", "")
        ignorelist = ignorelist.split("/")
        self.music.ignorealbums = [item.strip() for item in ignorelist]

        ignorelist = self.Get(str, "music",    "ignoresongs",  ".directory / desktop.ini / Desktop.ini / .DS_Store / Thumbs.db")
        ignorelist = ignorelist.split("/")
        self.music.ignoresongs = [item.strip() for item in ignorelist]


        # [artwork]
        self.artwork = ARTWORK()
        self.artwork.path           = self.GetDirectory("artwork",  "path",     "/opt/musicdb/data/artwork")
        self.artwork.scales         = self.Get(int, "artwork",  "scales",       "50, 150, 500", islist=True)
        for s in [50, 150, 500]:
            if not s in self.artwork.scales:
                logging.error("Missing scale in [artwork]->scales: The web UI expects a scale of %d (res: %dx%d)", s, s, s)
        self.artwork.manifesttemplate=self.GetFile( "artwork",  "manifesttemplate", "/opt/musicdb/server/manifest.txt", logging.warning) # a missing manifest does not affect the main functionality
        self.artwork.manifest       = self.Get(str, "artwork",  "manifest",     "/opt/musicdb/server/webui/manifest.appcache")


        # [videoframes]
        self.videoframes = VIDEOFRAMES()
        self.videoframes.path           = self.GetDirectory("videoframes",  "path",     "/opt/musicdb/data/videoframes")
        self.videoframes.frames         = self.Get(int, "videoframes",  "frames",       "5")
        self.videoframes.previewlength  = self.Get(int, "videoframes",  "previewlength","3")
        self.videoframes.scales         = self.Get(str, "videoframes",  "scales",       "50x27, 150x83", islist=True)
        for s in ["150x83"]:
            if not s in self.videoframes.scales:
                logging.error("Missing scale in [videoframes]->scales: The web UI expects a scale of %s", s)
        for scale in self.videoframes.scales:
            try:
                width, height   = map(int, scale.split("x"))
            except Exception as e:
                logging.error("Invalid video scale format in [videoframes]->scales: Expected format WxH, with W and H as integers. Actual format: %s.", scale)

        # [uploads]
        self.uploads = UPLOAD()
        self.uploads.allow           = self.Get(bool,    "uploads", "allow",      False)
        self.uploads.path            = self.GetDirectory("uploads", "path",       "/tmp")


        # [extern]
        self.extern = EXTERN()
        self.extern.configtemplate  = self.GetFile( "extern",   "configtemplate","/opt/musicdb/server/share/extconfig.ini")
        self.extern.statedir        = self.Get(str, "extern",   "statedir",     ".mdbstate")
        self.extern.configfile      = self.Get(str, "extern",   "configfile",   "config.ini")
        self.extern.songmap         = self.Get(str, "extern",   "songmap",      "songmap.csv")
        

        # [tracker]
        self.tracker = TRACKER()
        self.tracker.dbpath         = self.GetFile( "tracker",  "dbpath",       "/opt/musicdb/data/tracker.db")
        self.tracker.cuttime        = self.Get(int, "tracker",  "cuttime",      "30")


        # [lycra]
        self.lycra = LYCRA()
        self.lycra.dbpath           = self.GetFile( "lycra",    "dbpath",       "/opt/musicdb/data/lycra.db")


        # [Icecast]
        self.icecast    = ICECAST()
        self.icecast.port           = self.Get(int, "Icecast",  "port",     "6666")
        self.icecast.user           = self.Get(str, "Icecast",  "user",     "source")
        self.icecast.password       = self.Get(str, "Icecast",  "password", "hackme")
        self.icecast.mountname      = self.Get(str, "Icecast",  "mountname","/stream")


        # [Randy]
        self.randy      = RANDY()
        self.randy.nodisabled       = self.Get(bool, "Randy",   "nodisabled",   True)
        self.randy.nohated          = self.Get(bool, "Randy",   "nohated",      True)
        self.randy.minsonglen       = self.Get(int,  "Randy",   "minsonglen",   120)
        self.randy.maxsonglen       = self.Get(int,  "Randy",   "maxsonglen",   600)
        self.randy.songbllen        = self.Get(int,  "Randy",   "songbllen",    50)
        self.randy.albumbllen       = self.Get(int,  "Randy",   "albumbllen",   20)
        self.randy.artistbllen      = self.Get(int,  "Randy",   "artistbllen",  10)
        self.randy.videobllen       = self.Get(int,  "Randy",   "videobllen",   10)
        self.randy.maxblage         = self.Get(int,  "Randy",   "maxblage",     24)
        self.randy.maxtries         = self.Get(int,  "Randy",   "maxtries",     10)


        # [log]
        self.log        = LOG()
        self.log.logfile            = self.Get(str, "log",      "logfile",      "stderr")
        self.log.loglevel           = self.Get(str, "log",      "loglevel",     "WARNING")
        if not self.log.loglevel in ["DEBUG", "INFO", "WARNING", "ERROR"]:
            logging.error("Invalid loglevel for [log]->loglevel. Loglevel must be one of the following: DEBUG, INFO, WARNING, ERROR")
        self.log.debugfile          = self.Get(str, "log",      "debugfile",    None)
        if self.log.debugfile == "/dev/null":
            self.log.debugfile = None
        self.log.ignore             = self.Get(str, "log",      "ignore",       None, islist=True)


        # [debug]
        self.debug      = DEBUG()     
        self.debug.disablestats     = self.Get(int, "debug",    "disablestats",     0)
        self.debug.disabletracker   = self.Get(int, "debug",    "disabletracker",   0)
        self.debug.disableai        = self.Get(int, "debug",    "disableai",        1)
        self.debug.disabletagging   = self.Get(int, "debug",    "disabletagging",   0)
        self.debug.disableicecast   = self.Get(int, "debug",    "disableicecast",   0)
        self.debug.disablevideos    = self.Get(int, "debug",    "disablevideos",    0)

        logging.info("\033[1;32mdone")



    def GetDirectory(self, section, option, default):
        """
        This method gets a string from the config file and checks if it is an existing directory.
        If not it prints a warning and creates the directory if possible.
        If it fails with an permission-error an additional error gets printed.
        Except printing the error nothing is done.
        The \"invalid\" path will be returned anyway, because it may be OK that the directory does not exist yet.

        The permissions of the new created directory will be ``rwxrwxr-x``
        
        Args:
            section (str): Section of an ini-file
            option (str): Option inside the section of an ini-file
            default (str): Default directory path if option is not set in the file

        Returns:
            The value of the option set in the config-file or the default value.
        """
        path = self.Get(str, section, option, default)
        if self.fs.IsDirectory(path):
            return path

        # Create Directory
        logging.warning("Value of [%s]->%s does not address an existing directory. \033[1;30m(Directory \"%s\" will be created)", section, option, path)
        try:
            self.fs.CreateSubdirectory(path)
        except Exception as e:
            logging.error("Creating directory %s failed with error: %s.", path, str(e))

        # Set mode
        mode = stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH
        try:
            self.fs.SetAttributes(path, None, None, mode);
        except Exception as e:
            logging.error("Creating directory %s failed with error: %s.", path, str(e))

        return path # return path anyway, it does not matter if correct or not. Maybe it will be created later on.


    def GetFile(self, section, option, default, logger=logging.error):
        """
        This method gets a string from the config file and checks if it is an existing file.
        If not it prints an error.
        Except printing the error nothing is done.
        The \"invalid\" will be returned anyway, because it may be OK that the file does not exist yet.
        
        Args:
            section (str): Section of an ini-file
            option (str): Option inside the section of an ini-file
            default (str): Default file path if option is not set in the file
            logger: Logging-handler. Default is logging.error. logging.warning can be more appropriate in some situations.

        Returns:
            The value of the option set in the config-file or the default value.
        """
        path = self.Get(str, section, option, default)
        if not self.fs.IsFile(path):
            logger("Value of [%s]->%s does not address an existing file.", section, option)
        return path # return path anyway, it does not matter if correct or not. Maybe it will be created later on.
コード例 #3
0
ファイル: musiccache.py プロジェクト: whiteblue3/musicdb
class MusicCache(object):
    """
    Args:
        config: MusicDB configuration object
        database: MusicDB database

    Raises:
        TypeError: when *config* or *database* not of type :class:`~lib.cfg.musicdb.MusicDBConfig` or :class:`~lib.db.musicdb.MusicDatabase`
    """
    def __init__(self, config, database):
        if type(config) != MusicDBConfig:
            print(
                "\033[1;31mFATAL ERROR: Config-class of unknown type!\033[0m")
            raise TypeError("config argument not of type MusicDBConfig")
        if type(database) != MusicDatabase:
            print(
                "\033[1;31mFATAL ERROR: Database-class of unknown type!\033[0m"
            )
            raise TypeError("database argument not of type MusicDatabase")

        self.db = database
        self.cfg = config
        self.fs = Filesystem(self.cfg.music.cache)
        self.fileprocessor = Fileprocessing(self.cfg.music.cache)
        self.artworkcache = ArtworkCache(self.cfg.artwork.path)

    def GetAllCachedFiles(self):
        """
        This method returns three lists of paths of all files inside the cache.
        The tree lists are the following:

            #. All artist directories
            #. All album paths
            #. All song paths

        Returns:
            A tuple of three lists: (Artist-Paths, Album-Paths, Song-Paths)

        Example:

            .. code-block:: python

                (artists, albums, songs) = cache.GetAllCachedFiles()

                print("Albums in cache:")
                for path in albums:
                    name = musicdb.GetAlbumByPath(path)["name"]
                    print(" * " + name)

                print("Files in cache:")
                for path in songs:
                    print(" * " + path)
        """
        # Get all files from the cache
        artistpaths = self.fs.ListDirectory()
        albumpaths = self.fs.GetSubdirectories(artistpaths)
        songpaths = self.fs.GetFiles(albumpaths)

        return artistpaths, albumpaths, songpaths

    def RemoveOldArtists(self, cartistpaths, mdbartists):
        """
        This method removes all cached artists when they are not included in the artist list from the database.

        ``cartistpaths`` must be a list of artist directories with the artist ID as directory name.
        From these paths, a list of available artist ids is made and compared to the artist ids from the list of artists returned by the database (stored in ``mdbartists``)

        Is there a path/ID in ``cartistpaths`` that is not in the ``mdbartists`` list, the directory gets removed.
        The pseudo code can look like this:

            .. code-block:: python

                for path in cartistpaths:
                    if int(path) not in [mdbartists["id"]]:
                        self.fs.RemoveDirectory(path)

        Args:
            cartistpaths (list): a list of artist directories in the cache
            mdbartists (list): A list of artist rows from the Music Database

        Returns:
            *Nothing*
        """
        artistids = [artist["id"] for artist in mdbartists]
        cachedids = [int(path) for path in cartistpaths]

        for cachedid in cachedids:
            if cachedid not in artistids:
                self.fs.RemoveDirectory(str(cachedid))

    def RemoveOldAlbums(self, calbumpaths, mdbalbums):
        """
        This method compares the available album paths from the cache with the entries from the Music Database.
        If there are albums that do not match the database entries, then the cached album will be removed.

        Args:
            calbumpaths (list): a list of album directories in the cache (scheme: "ArtistID/AlbumID")
            mdbalbums (list): A list of album rows from the Music Database

        Returns:
            *Nothing*
        """
        # create "artistid/albumid" paths
        validpaths = [
            os.path.join(str(album["artistid"]), str(album["id"]))
            for album in mdbalbums
        ]

        for cachedpath in calbumpaths:
            if cachedpath not in validpaths:
                self.fs.RemoveDirectory(cachedpath)

    def RemoveOldSongs(self, csongpaths, mdbsongs):
        """
        This method compares the available song paths from the cache with the entries from the Music Database.
        If there are songs that do not match the database entries, then the cached song will be removed.

        Args:
            csongpaths (list): a list of song files in the cache (scheme: "ArtistID/AlbumID/SongID:Checksum.mp3")
            mdbsongs (list): A list of song rows from the Music Database

        Returns:
            *Nothing*
        """
        # create song paths
        validpaths = []
        for song in mdbsongs:
            path = self.GetSongPath(song)
            if path:
                validpaths.append(path)

        for cachedpath in csongpaths:
            if cachedpath not in validpaths:
                self.fs.RemoveFile(cachedpath)

    def GetSongPath(self, mdbsong, absolute=False):
        """
        This method returns a path following the naming scheme for cached songs (``ArtistID/AlbumID/SongID:Checksum.mp3``).
        It is not guaranteed that the file actually exists.

        Args:
            mdbsong: Dictionary representing a song entry form the Music Database
            absolute: Optional argument that can be set to ``True`` to get an absolute path, not relative to the cache directory.

        Returns:
            A (possible) path to the cached song (relative to the cache directory, ``absolute`` got not set to ``True``).
            ``None`` when there is no checksum available. The checksum is part of the file name.
        """
        # It can happen, that there is no checksum for a song.
        # For example when an old installation of MusicDB got not updated properly.
        # Better check if the checksum is valid to avoid any further problems.
        if len(mdbsong["checksum"]) != 64:
            logging.error(
                "Invalid checksum of song \"%s\": %s \033[1;30m(64 hexadecimal digit SHA265 checksum expected. Try \033[1;34mmusicdb repair --checksums \033[1;30mto fix the problem.)",
                mdbsong["path"], mdbsong["checksum"])
            return None

        path = os.path.join(str(mdbsong["artistid"]),
                            str(mdbsong["albumid"]))  # ArtistID/AlbumID
        path = os.path.join(path,
                            str(mdbsong["id"]))  # ArtistID/AlbumID/SongID
        path += ":" + mdbsong[
            "checksum"] + ".mp3"  # ArtistID/AlbumID/SongID:Checksum.mp3

        if absolute:
            path = self.fs.AbsolutePath(path)

        return path

    def Add(self, mdbsong):
        """
        This method checks if the song exists in the cache.
        When it doesn't, the file will be created (this may take some time!!).

        This process is done in the following steps:

            #. Check if song already cached. If it does, the method returns with ``True``
            #. Create directory tree if it does not exist. (``ArtistID/AlbumID/``)
            #. Convert song to mp3 (320kbp/s) and write it into the cache.
            #. Update ID3 tags. (ID3v2.3.0, 500x500 pixel artworks)

        Args:
            mdbsong: Dictionary representing a song entry form the Music Database

        Returns:
            ``True`` on success, otherwise ``False``
        """
        path = self.GetSongPath(mdbsong)
        if not path:
            return False

        # check if file exists, and create it when not.
        if self.fs.IsFile(path):
            return True

        # Create directory if not exists
        directory = os.path.join(str(mdbsong["artistid"]),
                                 str(mdbsong["albumid"]))  # ArtistID/AlbumID
        if not self.fs.IsDirectory(directory):
            self.fs.CreateSubdirectory(directory)

        # Create new mp3
        srcpath = os.path.join(self.cfg.music.path, mdbsong["path"])
        dstpath = self.fs.AbsolutePath(path)
        retval = self.fileprocessor.ConvertToMP3(srcpath, dstpath)
        if retval == False:
            logging.error("Converting %s to %s failed!", srcpath, dstpath)
            return False
        os.sync()

        # Optimize new mp3
        mdbalbum = self.db.GetAlbumById(mdbsong["albumid"])
        mdbartist = self.db.GetArtistById(mdbsong["artistid"])

        try:
            relartworkpath = self.artworkcache.GetArtwork(
                mdbalbum["artworkpath"], "500x500")
        except Exception as e:
            logging.error(
                "Getting artwork from cache failed with exception: %s!",
                str(e))
            logging.error("   Artwork: %s, Scale: %s", mdbalbum["artworkpath"],
                          "500x500")
            return False

        absartworkpath = os.path.join(self.cfg.artwork.path, relartworkpath)

        retval = self.fileprocessor.OptimizeMP3Tags(
            mdbsong,
            mdbalbum,
            mdbartist,
            srcpath=path,
            dstpath=path,
            absartworkpath=absartworkpath,
            forceID3v230=True)
        if retval == False:
            logging.error("Optimizing %s failed!", path)
            return False

        return True
コード例 #4
0
ファイル: cache.py プロジェクト: whiteblue3/musicdb
class ArtworkCache(object):
    """
    This class handles the artwork cache.
    Its main job is to scale an image if a special resolution is requested.

    Args:
        artworkdir: Absolute path to the artwork root directory
    """
    def __init__(self, artworkdir):
        self.artworkroot = Filesystem(artworkdir)


    def GetArtwork(self, artworkname, resolution):
        """
        This method returns a valid path to an artwork with the specified resolution.

        The final path will consist of the *artworkdir* given as class parameter, the *resolution* as subdirectory and the *artworkname* as filename. (``{artworkdir}/{resolution}/{artworkname}``)

        If the artwork does not exist for this resolution it will be generated.
        If the directory for that scale does not exist, it will be created.
        In case an error occcurs, an exception gets raised.

        The resolution is given as string in the format ``{X}x{Y}`` (For example: ``100x100``).
        *X* and *Y* must have the same value.
        This method expects an aspect ratio of 1:1.

        Beside scaling the JPEG, it will be made progressive.

        Args:
            artworkname (str): filename of the source artwork (Usually ``$Artist - $Album.jpg``)
            resolution (str): resolution of the requested artwork

        Returns:
            Relative path to the artwork in the specified resolution.

        Raises:
            ValueError: When the source file does not exist

        Example:

            .. code-block:: python

                cache = ArtworkCache("/data/artwork")
                path  = cache.GetArtwork("example.jpg", "150x150")
                # returned path: "150x150/example.jpg"
                # absolute path: "/data/artwork/150x150/example.jpg"
        """
        logging.debug("GetArtwork(%s, %s)", artworkname, resolution)

        # Check if source exists
        if not self.artworkroot.Exists(artworkname):
            logging.error("Source file %s does not exist in the artwork root directory!", artworkname)
            raise ValueError("Source file %s does not exist in the artwork root directory!", 
                    artworkname)

        # Check if already scaled. If yes, our job is done
        scaledfile = os.path.join(resolution, artworkname)
        if self.artworkroot.Exists(scaledfile):
            return scaledfile

        # Check if the scale-directory already exist. If not, create one
        if not self.artworkroot.IsDirectory(resolution):
            logging.debug("Creating subdirectory: %s", resolution)
            self.artworkroot.CreateSubdirectory(resolution)

        # Scale image
        logging.debug("Converting image to %s", resolution)
        abssrcpath = self.artworkroot.AbsolutePath(artworkname)
        absdstpath = self.artworkroot.AbsolutePath(scaledfile)

        # "10x10" -> (10, 10)
        length = int(resolution.split("x")[0])
        size   = (length, length)

        im = Image.open(abssrcpath)
        im.thumbnail(size, Image.BICUBIC)
        im.save(absdstpath, "JPEG", optimize=True, progressive=True)

        return scaledfile
コード例 #5
0
ファイル: uploadmanager.py プロジェクト: rstemmer/musicdb
class UploadManager(object):
    """
    This class manages uploading content to the server MusicDB runs on.
    All data is stored in the uploads-directory configured in the MusicDB configuration.
    
    Args:
        config: :class:`~lib.cfg.musicdb.MusicDBConfig` object holding the MusicDB Configuration
        database: (optional) A :class:`~lib.db.musicdb.MusicDatabase` instance

    Raises:
        TypeError: When the arguments are not of the correct type.
    """
    def __init__(self, config, database):
        if type(config) != MusicDBConfig:
            raise TypeError("config argument not of type MusicDBConfig")
        if database != None and type(database) != MusicDatabase:
            raise TypeError(
                "database argument not of type MusicDatabase or None")

        self.db = database
        self.cfg = config
        self.uploadfs = Filesystem(self.cfg.uploads.path)
        self.musicfs = Filesystem(self.cfg.music.path)
        self.artworkfs = Filesystem(self.cfg.artwork.path)
        # TODO: check write permission of all directories
        self.fileprocessing = Fileprocessing(self.cfg.uploads.path)
        self.dbmanager = MusicDBDatabase(config, database)

        global Tasks
        if Tasks == None:
            self.LoadTasks()

    #####################################################################
    # Callback Function Management                                      #
    #####################################################################

    def RegisterCallback(self, function):
        """
        Register a callback function that reacts on Upload related events.
        For more details see the module description at the top of this document.

        Args:
            function: A function that shall be called on an event.

        Returns:
            *Nothing*
        """
        global Callbacks
        Callbacks.append(function)

    def RemoveCallback(self, function):
        """
        Removes a function from the list of callback functions.

        Args:
            function: A function that shall be called removed.

        Returns:
            *Nothing*
        """
        global Callbacks

        # Not registered? Then do nothing.
        if not function in Callbacks:
            logging.warning(
                "A Streaming Thread callback function should be removed, but did not exist in the list of callback functions!"
            )
            return

        Callbacks.remove(function)

    def NotifyClient(self, notification, task, message=None):
        """
        This method triggers a client-notification.

        There are three kind of notifications:

            * ``"ChunkRequest"``: A new chunk of data is requested
            * ``"StateUpdate"``: The state or annotations of an upload-task has been changed. See ``"state"`` value.
            * ``"InternalError"``: There is an internal error occurred during. See ``"message"`` value.

        The notification comes with the current status of the upload process.
        This includes the following keys - independent of the state of the upload:

            * uploadid: ID of the upload the notification is associated with
            * offset: Offset of the requested data in the source file
            * chunksize: The maximum chunk size
            * state: The current state of the upload task
            * message: ``null``/``None`` or a message from the server
            * uploadtask: The task dictionary itself
            * uploadslist: Except for ``ChunkRequest`` events, the WebSocket server append the result of :meth:`lib.ws.mdbwsi.MusicDBWebSocketInterface.GetUploads` to the notification

        *task* can be ``None`` in case the notification is meant to be an information that a given upload ID is invalid.

        Args:
            notification (str): Name of the notification
            task (dict): Task structure
            message (str): (optional) text message (like an error message) to the client

        Returns:
            *Nothing*

        Raises:
            ValueError: When notification has an unknown notification name
        """
        if not notification in [
                "ChunkRequest", "StateUpdate", "InternalError"
        ]:
            raise ValueError("Unknown notification \"%s\"" % (notification))

        status = {}
        if task != None:
            status["uploadid"] = task["id"]
            status["offset"] = task["offset"]  # offset of the data to request
            status[
                "chunksize"] = 4096 * 100  # Upload 400KiB (TODO: Make configurable)
            status["state"] = task["state"]
            status["uploadtask"] = task
        else:
            status["uploadid"] = None
            status["offset"] = None
            status["chunksize"] = None
            status["state"] = "notexisting"
            status["uploadtask"] = None

        status["message"] = message

        global Callbacks
        for callback in Callbacks:
            try:
                callback(notification, status)
            except Exception as e:
                logging.exception(
                    "A Upload Management event callback function crashed!")

    #####################################################################
    # State management                                                  #
    #####################################################################

    def SaveTask(self, task):
        """
        This method saves a task in the uploads directory under ``tasks/${Task ID}.json``

        Args:
            task (dict): The task to save

        Returns:
            *Nothing*
        """
        taskid = task["id"]
        data = json.dumps(task)
        path = self.cfg.uploads.path + "/tasks/" + taskid + ".json"

        if not self.uploadfs.IsDirectory("tasks"):
            logging.debug("tasks directory missing. Creating \"%s\"",
                          self.cfg.uploads.path + "/tasks")
            self.uploadfs.CreateSubdirectory("tasks")

        with open(path, "w+") as fd:
            fd.write(data)

        return

    def LoadTasks(self):
        """
        Loads all task from the JSON files inside the tasks-directory.
        The list of active tasks will be replaced by the loaded tasks.

        Returns:
            *Nothing*
        """
        logging.debug("Loading Upload-Tasks…")

        taskfilenames = self.uploadfs.ListDirectory("tasks")

        global Tasks
        Tasks = {}
        for taskfilename in taskfilenames:
            taskpath = self.cfg.uploads.path + "/tasks/" + taskfilename

            if self.uploadfs.GetFileExtension(taskpath) != "json":
                continue

            try:
                with open(taskpath) as fd:
                    task = json.load(fd)
            except Exception as e:
                logging.warning(
                    "Loading task file \"%s\" failed with error \"%s\". \033[1;30m(File will be ignored)",
                    str(taskpath), str(e))
                continue

            if "id" not in task:
                logging.warning(
                    "File \"%s\" is not a valid task (ID missing). \033[1;30m(File will be ignored)",
                    str(taskpath), str(e))
                continue

            Tasks[task["id"]] = task

        return

    #####################################################################
    # Management Functions                                              #
    #####################################################################

    def InitiateUpload(self, uploadid, mimetype, contenttype, filesize,
                       checksum, sourcefilename):
        """
        Initiates an upload of a file into a MusicDB managed file space.
        After calling this method, a notification gets triggered to request the first chunk of data from the clients.
        In case uploads are deactivated in the MusicDB Configuration, an ``"InternalError"`` Notification gets sent to the clients.

        Args:
            uploadid (str): Unique ID to identify the upload task 
            mimetype (str): MIME-Type of the file (example: ``"image/png"``)
            contenttype (str): Type of the content: (``"video"``, ``"album"``, ``"artwork"``)
            filesize (int): Size of the complete file in bytes
            checksum (str): SHA-1 check sum of the source file
            sourcefilename (str): File name (example: ``"test.png"``)

        Raises:
            TypeError: When one of the arguments has not the expected type
            ValueError: When *contenttype* does not have the expected values
        """
        if type(uploadid) != str:
            raise TypeError("Upload ID must be of type string")
        if type(mimetype) != str:
            raise TypeError("mime type must be of type string")
        if type(contenttype) != str:
            raise TypeError("content type must be of type string")
        if contenttype not in ["video", "album", "artwork"]:
            raise ValueError(
                "content type \"%s\" not valid. \"video\", \"album\" or \"artwork\" expected."
                % (str(contenttype)))
        if type(filesize) != int:
            raise TypeError("file size must be of type int")
        if filesize <= 0:
            raise ValueError("file size must be greater than 0")
        if type(checksum) != str:
            raise TypeError("Checksum must be of type string")
        if type(sourcefilename) != str:
            raise TypeError("Source file name must be of type string")

        if not self.cfg.uploads.allow:
            self.NotifyClient("InternalError", None, "Uploads deactivated")
            logging.warning(
                "Uploads not allowed! \033[1;30m(See MusicDB Configuration: [uploads]->allow)"
            )
            return

        fileextension = self.uploadfs.GetFileExtension(sourcefilename)
        destinationname = contenttype + "-" + checksum + "." + fileextension
        destinationpath = self.cfg.uploads.path + "/" + destinationname

        # TODO: Check if there is already a task with the given ID.
        # If this task is in waitforchunk state, the upload can be continued instead of restarting it.

        # Remove existing upload if destination path exists
        self.uploadfs.RemoveFile(
            destinationpath)  # Removes file when it exists

        # Create File
        with open(destinationpath, "w+b"):
            pass

        task = {}
        task["id"] = uploadid
        task["filesize"] = filesize
        task["offset"] = 0
        task["contenttype"] = contenttype
        task["mimetype"] = mimetype
        task["sourcefilename"] = sourcefilename
        task["sourcechecksum"] = checksum
        task["destinationpath"] = destinationpath
        task[
            "videofile"] = None  # Path to the video file in the music directory
        task["state"] = "waitforchunk"
        task["annotations"] = {}
        self.SaveTask(task)

        global Tasks
        Tasks[uploadid] = task

        self.NotifyClient("ChunkRequest", task)
        return

    def RequestRemoveUpload(self, uploadid):
        """
        This method triggers removing a specific upload.
        This includes the uploaded file as well as the upload task information and annotations.

        The upload task can be in any state.
        When the remove-operation is triggered, its state gets changed to ``"remove"``.

        Only the ``"remove"`` state gets set. Removing will be done by the Management Thread.

        Args:
            uploadid (str): ID of the upload-task

        Returns:
            ``True`` on success
        """
        try:
            task = self.GetTaskByID(uploadid)
        except Exception as e:
            logging.error(
                "Internal error while requesting a new chunk of data: %s",
                str(e))
            return False

        self.UpdateTaskState(task, "remove")
        return True

    def GetTaskByID(self, uploadid):
        """
        This method returns an existing task from the tasklist.
        The task gets identified by its ID aka Upload ID

        When the task does not exits, the clients get an ``"InternalError"`` notification.
        The tasks state is then ``"notexisting"``.

        Args:
            uploadid (str): ID of the upload-task

        Returns:
            A task dictionary

        Raises:
            TypeError: When *uploadid* is not a string
            ValueError: When *uploadid* is not a valid key in the Tasks-dictionary
        """
        if type(uploadid) != str:
            raise TypeError("Upload ID must be a string. Type was \"%s\"." %
                            (str(type(uploadid))))

        global Tasks
        if uploadid not in Tasks:
            self.NotifiyClient("InternalError", None, "Invalid Upload ID")
            raise ValueError("Upload ID \"%s\" not in Task Queue.",
                             str(uploadid))

        return Tasks[uploadid]

    def UpdateTaskState(self, task, state, errormessage=None):
        """
        This method updates and saves the state of an task.
        An ``"StateUpdate"`` notification gets send as well.

        If *errormessage* is not ``None``, the notification gets send as ``"InternalError"`` with the message

        Args:
            task (dict): Task object to update
            state (str): New state
            message (str): Optional message

        Returns:
            *Nothing*
        """
        task["state"] = state
        self.SaveTask(task)
        if errormessage:
            self.NotifyClient("InternalError", task, errormessage)
        else:
            self.NotifyClient("StateUpdate", task)
        return

    def NewChunk(self, uploadid, rawdata):
        """
        This method processes a new chunk received from the uploading client.

        Args:
            uploadid (str): Unique ID to identify the upload task
            rawdata (bytes): Raw data to append to the uploaded data

        Returns:
            ``False`` in case an error occurs. Otherwise ``True``.

        Raises:
            TypeError: When *rawdata* is not of type ``bytes``
        """
        if type(rawdata) != bytes:
            raise TypeError("raw data must be of type bytes. Type was \"%s\"" %
                            (str(type(rawdata))))

        try:
            task = self.GetTaskByID(uploadid)
        except Exception as e:
            logging.error(
                "Internal error while requesting a new chunk of data: %s",
                str(e))
            return False

        chunksize = len(rawdata)
        filepath = task["destinationpath"]

        try:
            with open(filepath, "ab") as fd:
                fd.write(rawdata)
        except Exception as e:
            logging.warning(
                "Writing chunk of uploaded data into \"%s\" failed: %s \033[1;30m(Upload canceled)",
                filepath, str(e))
            self.UpdateTaskState(
                task, "uploadfailed",
                "Writing data failed with error: \"%s\"" % (str(e)))
            return False

        task["offset"] += chunksize
        self.SaveTask(task)

        if task["offset"] >= task["filesize"]:
            # Upload complete
            self.UploadCompleted(task)
        else:
            # Get next chunk of data
            self.NotifyClient("ChunkRequest", task)
        return True

    def UploadCompleted(self, task):
        """
        This method continues the file management after an upload was completed.
        The following tasks were performed:

            * Checking the checksum of the destination file (SHA1) and compares it with the ``"sourcechecksum"`` from the *task*-dict.

        When the upload was successful, it notifies the clients with a ``"UploadComplete"`` notification.
        Otherwise with a ``"UploadFailed"`` one.

        Args:
            task (dict): The task that upload was completed

        Returns:
            ``True`` When the upload was successfully complete, otherwise ``False``
        """
        # Check checksum
        destchecksum = self.fileprocessing.Checksum(task["destinationpath"],
                                                    "sha1")
        if destchecksum != task["sourcechecksum"]:
            logging.error(
                "Upload Failed: \033[0;36m%s \e[1;30m(Checksum mismatch)",
                task["destinationpath"])
            self.UpdateTaskState(task, "uploadfailed", "Checksum mismatch")
            return False

        logging.info("Upload Complete: \033[0;36m%s", task["destinationpath"])
        self.UpdateTaskState(task, "uploadcomplete")
        # Now, the Management Thread takes care about post processing or removing no longer needed content
        return True

    def GetTasks(self):
        """
        Returns:
            The dictionary with all upload tasks
        """
        global Tasks
        return Tasks

    def PreProcessUploadedFile(self, task):
        """
        This method initiates pre-processing of an uploaded file.
        Depending on the *contenttype* different post processing methods are called:

            * ``"video"``: :meth:`~PreProcessVideo`
            * ``"artwork"``: :meth:`~PreProcessArtwork`

        The task must be in ``"uploadcomplete"`` state, otherwise nothing happens but printing an error message.
        If post processing was successful, the task state gets updated to ``"preprocessed"``.
        When an error occurred, the state will become ``"invalidcontent"``.

        Args:
            task (dict): the task object of an upload-task

        Returns:
            ``True`` on success, otherwise ``False``
        """
        if task["state"] != "uploadcomplete":
            logging.error(
                "task must be in \"uploadcomplete\" state for post processing. Actual state was \"%s\". \033[1;30m(Such a mistake should not happen. Anyway, the task won\'t be post process and nothing bad will happen.)",
                task["state"])
            return False

        # Perform post processing
        logging.debug("Preprocessing upload %s -> %s",
                      str(task["sourcefilename"]),
                      str(task["destinationpath"]))
        success = False
        if task["contenttype"] == "video":
            success = self.PreProcessVideo(task)
        elif task["contenttype"] == "artwork":
            success = self.PreProcessArtwork(task)
        else:
            logging.warning(
                "Unsupported content type of upload: \"%s\" \033[1;30m(Upload will be ignored)",
                str(task["contenttype"]))
            self.UpdateTaskState(task, "invalidcontent",
                                 "Unsupported content type")
            return False

        # Update task state
        if success == True:
            newstate = "preprocessed"
        else:
            newstate = "invalidcontent"

        self.UpdateTaskState(task, newstate)
        return success

    def PreProcessVideo(self, task):
        """

        Args:
            task (dict): the task object of an upload-task
        """
        meta = MetaTags()
        try:
            meta.Load(task["destinationpath"])
        except ValueError:
            logging.error(
                "The file \"%s\" uploaded as video to %s is not a valid video or the file format is not supported. \033[1;30m(File will be not further processed.)",
                task["sourcefilename"], task["destinationpath"])
            return False

        # Get all meta infos (for videos, this does not include any interesting information.
        # Maybe the only useful part is the Load-method to check if the file is supported by MusicDB
        #tags = meta.GetAllMetadata()
        #logging.debug(tags)
        return True

    def PreProcessArtwork(self, task):
        """

        Args:
            task (dict): the task object of an upload-task
        """
        origfile = task["destinationpath"]
        extension = self.uploadfs.GetFileExtension(origfile)
        jpegfile = origfile[:-len(extension)] + "jpg"
        if extension != "jpg":
            logging.debug(
                "Transcoding artwork file form %s (\"%s\") to JPEG (\"%s\")",
                extension, origfile, jpegfile)
            im = Image.open(origfile)
            im = im.convert("RGB")
            im.save(jpegfile, "JPEG", optimize=True, progressive=True)

        task["artworkfile"] = jpegfile
        return True

    def AnnotateUpload(self, uploadid, annotations):
        """
        This method can be used to add additional information to an upload.
        This can be done during or after the upload process.

        Args:
            uploadid (str): ID to identify the upload

        Returns:
            ``True`` on success, otherwise ``False``

        Raises:
            TypeError: When *uploadid* is not of type ``str``
            ValueError: When *uploadid* is not included in the Task Queue
        """
        try:
            task = self.GetTaskByID(uploadid)
        except Exception as e:
            logging.error(
                "Internal error while requesting a new chunk of data: %s",
                str(e))
            return False

        for key, item in annotations.items():
            task["annotations"][key] = item

        self.SaveTask(task)
        self.NotifyClient("StateUpdate", task)
        return True

    def IntegrateUploadedFile(self, uploadid, triggerimport):
        """
        This method integrated the uploaded files into the music directory.
        The whole file tree will be created following the MusicDB naming scheme.

        The upload task must be in ``preprocesses`` state. If not, nothing happens.

        When *triggerimport* is ``true``, the upload manager start importing the music.
        This happens asynchronously inside the Upload Manager Thread.

        Args:
            uploadid (str): ID to identify the upload

        Returns:
            ``True`` on success, otherwise ``False``

        Raises:
            TypeError: When *uploadid* is not of type ``str``
            ValueError: When *uploadid* is not included in the Task Queue
        """
        try:
            task = self.GetTaskByID(uploadid)
        except Exception as e:
            logging.error(
                "Internal error while requesting a new chunk of data: %s",
                str(e))
            return False

        if task["state"] != "preprocessed":
            logging.warning(
                "Cannot integrate an upload that is not in \"preprocessed\" state. Upload with ID \"%s\" was in \"%s\" state! \033[1;30m(Nothing will be done)",
                str(task["id"]), str(task["state"]))
            return

        # Perform post processing
        success = False
        if task["contenttype"] == "video":
            success = self.IntegrateVideo(task)
        elif task["contenttype"] == "artwork":
            success = True  # Importing artwork does not require the file at any specific place
        else:
            logging.warning(
                "Unsupported content type of upload: \"%s\" \033[1;30m(Upload will be ignored)",
                str(task["contenttype"]))
            self.UpdateTaskState(task, "integrationfailed",
                                 "Unsupported content type")
            return

        # Update task state
        if success == True:
            newstate = "integrated"
        else:
            newstate = "integrationfailed"
        self.UpdateTaskState(task, newstate)

        # Trigger import
        if success == False or triggerimport == False:
            return  # … but only if wanted, and previous step was successful

        self.UpdateTaskState(
            task,
            "startimport")  # The upload management thread will do the rest
        return

    def IntegrateVideo(self, task):
        """
        When an annotation needed for creating the video file path in the music directory is missing, ``False`` gets returned and an error message written into the log
        """
        uploadedfile = task["destinationpath"]  # uploaded file
        try:
            artistname = task["annotations"]["artistname"]
            releasedate = task["annotations"]["release"]
            videoname = task["annotations"]["name"]
        except KeyError as e:
            logging.error(
                "Collection video information for creating its path name failed with key-error for: %s \033[1;30m(Make sure all important annotations are given to that upload: name, artistname, release)",
                str(e))
            return False

        fileextension = self.uploadfs.GetFileExtension(uploadedfile)
        videofile = artistname + "/" + releasedate + " - " + videoname + "." + fileextension

        task["videofile"] = videofile
        logging.debug("Integrating upload %s -> %s", str(uploadedfile),
                      str(videofile))

        # Check if video file already exists
        if self.musicfs.Exists(videofile):
            logging.warning(
                "File \"%s\" already exists in the music directory! \033[1;30m(It will NOT be overwritten)",
                str(videofile))
            self.NotifyClient("InternalError", task,
                              "File already exists in the music directory")
            return False

        # Check if artist directory exists
        if not self.musicfs.IsDirectory(artistname):
            logging.info(
                "Artist directory for \"%s\" does not exist and will be created.",
                str(artistname))
            try:
                self.musicfs.CreateSubdirectory(artistname)
            except PermissionError:
                logging.error(
                    "Creating artist sub-directory \"%s\" failed! - Permission denied! \033[1;30m(MusicDB requires write permission to the music file tree)",
                    str(artistname))
                self.NotifyClient(
                    "InternalError", task,
                    "Creating artist directory failed - Permission denied")
                return False

        # Copy file, create Artist directory if not existing
        try:
            success = self.musicfs.CopyFile(uploadedfile, videofile)
        except PermissionError:
            logging.error(
                "Copying video file to \"%s\" failed! - Permission denied! \033[1;30m(MusicDB requires write permission to the music file tree)",
                str(videofile))
            self.NotifyClient("InternalError", task,
                              "Copying failed - Permission denied")
            return False

        if (success):
            logging.info("New video file at %s", str(videofile))
        else:
            logging.warning("Copying video file to \"%s\" failed!",
                            str(videofile))
        return success

    def ImportVideo(self, task):
        """
        Task state must be ``"startimport"`` and content type must be ``"video"``

        Returns:
            ``True`` on success.
        """
        # Check task state and type
        if task["state"] != "startimport":
            logging.warning(
                "Cannot import an upload that is not in \"startimport\" state. Upload with ID \"%s\" was in \"%s\" state! \033[1;30m(Nothing will be done)",
                str(task["id"]), str(task["state"]))
            return False

        success = False
        if task["contenttype"] != "video":
            logging.warning(
                "Video expected. Actual type of upload: \"%s\" \033[1;30m(No video will be imported)",
                str(task["contenttype"]))
            return False

        # Get important information
        try:
            artistname = task["annotations"]["artistname"]
            videopath = task["videofile"]
        except KeyError as e:
            logging.error(
                "Collecting video information for importing failed with key-error for: %s \033[1;30m(Make sure the artist name is annotated to the upload)",
                str(e))
            return False

        # Check if the artist already exists in the database - if not, add it
        artist = self.db.GetArtistByPath(artistname)
        if artist == None:
            logging.info("Importing new artist: \"%s\"", artistname)
            try:
                self.dbmanager.AddArtist(artistname)
            except Exception as e:
                logging.error(
                    "Importing artist \"%s\" failed with error: %s \033[1;30m(Video upload canceled)",
                    str(artistname), str(e))
                self.NotifyClient("InternalError", task,
                                  "Importing artist failed")
                return False
            artist = self.db.GetArtistByPath(artistname)

        # Import video
        try:
            success = self.dbmanager.AddVideo(videopath, artist["id"])
        except Exception as e:
            logging.error(
                "Importing video \"%s\" failed with error: %s \033[1;30m(Video upload canceled)",
                str(videopath), str(e))
            self.NotifyClient("InternalError", task, "Importing video failed")
            return False

        if not success:
            logging.error(
                "Importing video \"%s\" failed. \033[1;30m(Video upload canceled)",
                str(videopath), str(e))
            self.NotifyClient("InternalError", task, "Importing video failed")
            return False

        # Add origin information to database if annotated
        try:
            origin = task["annotations"]["origin"]
        except KeyError as e:
            pass
        else:
            video = self.db.GetVideoByPath(videopath)
            video["origin"] = origin
            self.db.WriteVideo(video)

        logging.info("Importing Video succeeded")
        return True

    def ImportVideoArtwork(self, task):
        """
        Returns:
            ``True`` on success
        """
        # Check task state and type
        if task["state"] != "importartwork":
            logging.warning(
                "Cannot import artwork that is not in \"importartwork\" state. Upload with ID \"%s\" was in \"%s\" state! \033[1;30m(Nothing will be done)",
                str(task["id"]), str(task["state"]))
            return False

        if task["contenttype"] != "video":
            logging.warning(
                "Video expected. Actual type of upload: \"%s\" \033[1;30m(No video will be imported)",
                str(task["contenttype"]))
            return False

        # Start generating the artworks
        videopath = task["videofile"]
        framemanager = VideoFrames(self.cfg, self.db)

        video = self.db.GetVideoByPath(videopath)
        if not video:
            logging.error(
                "Getting video \"%s\" from database failed. \033[1;30m(Artwork import canceled)",
                str(videopath), str(e))
            self.NotifyClient("InternalError", task,
                              "Video artwork import failed")
            return False

        retval = framemanager.UpdateVideoFrames(video)
        if retval == False:
            logging.error(
                "Generating video frames and preview failed for video \"%s\". \033[1;30m(Artwork import canceled)",
                str(videopath))
            self.NotifyClient("InternalError", task,
                              "Video artwork import failed")
            return False

        logging.info("Importing Video thumbnails and previews succeeded")
        return True

    def ImportArtwork(self, task):
        """
        Task state must be ``"startimport"`` and content type must be ``"artwork"``

        Returns:
            ``True`` on success.
        """
        # Check task state and type
        if task["state"] != "startimport":
            logging.warning(
                "Cannot import an upload that is not in \"startimport\" state. Upload with ID \"%s\" was in \"%s\" state! \033[1;30m(Nothing will be done)",
                str(task["id"]), str(task["state"]))
            return False

        success = False
        if task["contenttype"] != "artwork":
            logging.warning(
                "Album artwork expected. Actual type of upload: \"%s\" \033[1;30m(No artwork will be imported)",
                str(task["contenttype"]))
            return False

        # Get important information
        try:
            artistname = task["annotations"]["artistname"]
            albumname = task["annotations"]["albumname"]
            albumid = task["annotations"]["albumid"]
            sourcepath = task["artworkfile"]
        except KeyError as e:
            logging.error(
                "Collecting artwork information for importing failed with key-error for: %s \033[1;30m(Make sure the artist and album name is annotated as well as the album ID.)",
                str(e))
            return False

        # Import artwork
        awmanager = MusicDBArtwork(self.cfg, self.db)
        artworkname = awmanager.CreateArtworkName(artistname, albumname)
        success = awmanager.SetArtwork(albumid, sourcepath, artworkname)

        if not success:
            logging.error(
                "Importing artwork \"%s\" failed. \033[1;30m(Artwork upload canceled)",
                str(sourcepath))
            self.NotifyClient("InternalError", task,
                              "Importing artwork failed")
            return False

        # Add origin information to database if annotated
        try:
            origin = task["annotations"]["origin"]
        except KeyError as e:
            pass
        else:
            video = self.db.GetVideoByPath(videopath)
            video["origin"] = origin
            self.db.WriteVideo(video)

        logging.info("Importing Artwork succeeded")
        return True

    def RemoveTask(self, task):
        """
        ``tasks/${Task ID}.json``
        """
        logging.info(
            "Removing uploaded \"%s\" file and task \"%s\" information.",
            task["sourcefilename"], task["id"])
        datapath = task["destinationpath"]
        taskpath = "tasks/" + task["id"] + ".json"

        # if artwork, remove artworkfile as well
        if task["contenttype"] == "artwork":
            artworkfile = task["artworkfile"]
            logging.debug("Removing %s",
                          self.uploadfs.AbsolutePath(artworkfile))
            self.uploadfs.RemoveFile(artworkfile)

        logging.debug("Removing %s", self.uploadfs.AbsolutePath(datapath))
        self.uploadfs.RemoveFile(datapath)
        logging.debug("Removing %s", self.uploadfs.AbsolutePath(taskpath))
        self.uploadfs.RemoveFile(taskpath)
        return True
コード例 #6
0
class VideoFrames(object):
    """
    This class implements the concept described above.
    The most important method is :meth:`~UpdateVideoFrames` that generates all frames and previews for a given video.

    Args:
        config: MusicDB configuration object
        database: MusicDB database

    Raises:
        TypeError: if config or database are not of the correct type
        ValueError: If one of the working-paths set in the config file does not exist
    """
    def __init__(self, config, database):

        if type(config) != MusicDBConfig:
            raise TypeError("Config-class of unknown type")
        if type(database) != MusicDatabase:
            raise TypeError("Database-class of unknown type")

        self.db = database
        self.cfg = config
        self.fs = Filesystem()
        self.musicroot = Filesystem(self.cfg.music.path)
        self.framesroot = Filesystem(self.cfg.videoframes.path)
        self.metadata = MetaTags(self.cfg.music.path)
        self.maxframes = self.cfg.videoframes.frames
        self.previewlength = self.cfg.videoframes.previewlength
        self.scales = self.cfg.videoframes.scales

        # Check if all paths exist that have to exist
        pathlist = []
        pathlist.append(self.cfg.music.path)
        pathlist.append(self.cfg.videoframes.path)

        for path in pathlist:
            if not self.fs.Exists(path):
                raise ValueError("Path \"" + path + "\" does not exist.")

    def CreateFramesDirectoryName(self, artistname, videoname):
        """
        This method creates the name for a frames directory regarding the following schema:
        ``$Artistname/$Videoname``.
        If there is a ``/`` in the name, it gets replaced by ``∕`` (U+2215, DIVISION SLASH)

        Args:
            artistname (str): Name of an artist
            videoname (str): Name of a video

        Returns:
            valid frames sub directory name for a video
        """
        artistname = artistname.replace("/", "∕")
        videoname = videoname.replace("/", "∕")
        dirname = artistname + "/" + videoname
        return dirname

    def CreateFramesDirectory(self, artistname, videoname):
        """
        This method creates the directory that contains all frames and previews for a video.
        The ownership of the created directory will be the music user and music group set in the configuration file.
        The permissions will be set to ``rwxrwxr-x``.
        If the directory already exists, only the attributes will be updated.

        Args:
            artistname (str): Name of an artist
            videoname (str): Name of a video

        Returns:
            The name of the directory.
        """
        # Determine directory name
        dirname = self.CreateFramesDirectoryName(artistname, videoname)

        # Create directory if it does not yet exist
        if self.framesroot.IsDirectory(dirname):
            logging.debug("Frame directory \"%s\" already exists.", dirname)
        else:
            self.framesroot.CreateSubdirectory(dirname)

        # Set permissions to -rwxrwxr-x
        try:
            self.framesroot.SetAttributes(
                dirname, self.cfg.music.owner, self.cfg.music.group,
                stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP
                | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
        except Exception as e:
            logging.warning(
                "Setting frames sub directory attributes failed with error %s. \033[1;30m(Leaving them as they are)",
                str(e))

        return dirname

    def GenerateFrames(self, dirname, videopath):
        """
        This method creates all frame files, including scaled frames, from a video.
        After generating the frames, animations can be generated via :meth:`~GeneratePreviews`.

        To generate the frames, ``ffmpeg`` is used in the following way:

        .. code-block:: bash

            ffmpeg -ss $time -i $videopath -vf scale=iw*sar:ih -vframes 1 $videoframes/$dirname/frame-xx.jpg

        ``videopath`` and ``dirname`` are the parameters of this method.
        ``videoframes`` is the root directory for the video frames as configured in the MusicDB Configuration file.
        ``time`` is a moment in time in the video at which the frame gets selected.
        This value gets calculated depending of the videos length and amount of frames that shall be generated.
        The file name of the frames will be ``frame-xx.jpg`` where ``xx`` represents the frame number.
        The number is decimal, has two digits and starts with 01.

        The scale solves the differences between the Display Aspect Ratio (DAR) and the Sample Aspect Ratio (SAR).
        By using a scale of image width multiplied by the SAR, the resulting frame has the same ratio as the video in the video player.

        The total length of the video gets determined by :meth:`~lib.metatags.MetaTags.GetPlaytime`

        When there are already frames existing, nothing will be done.
        This implies that it is mandatory to remove existing frames manually when there are changes in the configuration.
        For example when increasing or decreasing the amount of frames to consider for the animation.
        The method will return ``True`` in this case, because there are frames existing.

        Args:
            dirname (str): Name/Path of the directory to store the generated frames
            videopath (str): Path to the video that gets processed

        Returns:
            ``True`` on success, otherwise ``False``
        """
        # Determine length of the video in seconds
        try:
            self.metadata.Load(videopath)
            videolength = self.metadata.GetPlaytime()
        except Exception as e:
            logging.exception(
                "Generating frames for video \"%s\" failed with error: %s",
                videopath, str(e))
            return False

        slicelength = videolength / (self.maxframes + 1)
        sliceoffset = slicelength / 2

        for framenumber in range(self.maxframes):
            # Calculate time point of the frame in seconds
            #moment = (videolength / self.maxframes) * framenumber
            moment = sliceoffset + slicelength * framenumber

            # Define destination path
            framename = "frame-%02d.jpg" % (framenumber + 1)
            framepath = dirname + "/" + framename

            # Only create frame if it does not yet exist
            if not self.framesroot.Exists(framepath):
                # create absolute paths for FFMPEG
                absframepath = self.framesroot.AbsolutePath(framepath)
                absvideopath = self.musicroot.AbsolutePath(videopath)

                # Run FFMPEG - use absolute paths
                process = [
                    "ffmpeg",
                    "-ss",
                    str(moment),
                    "-i",
                    absvideopath,
                    "-vf",
                    "scale=iw*sar:ih",  # Make sure the aspect ration is correct
                    "-vframes",
                    "1",
                    absframepath
                ]
                logging.debug("Getting frame via %s", str(process))
                try:
                    self.fs.Execute(process)
                except Exception as e:
                    logging.exception(
                        "Generating frame for video \"%s\" failed with error: %s",
                        videopath, str(e))
                    return False

            # Scale down the frame
            self.ScaleFrame(dirname, framenumber + 1)

        return True

    def ScaleFrame(self, dirname, framenumber):
        """
        This method creates a scaled version of the existing frames for a video.
        The aspect ration of the frame will be maintained.
        In case the resulting aspect ratio differs from the source file,
        the borders of the source frame will be cropped in the scaled version.

        If a scaled version exist, it will be skipped.

        The scaled JPEG will be stored with optimized and progressive settings.

        Args:
            dirname (str): Name of the directory where the frames are stored at (relative)
            framenumber (int): Number of the frame that will be scaled

        Returns:
            *Nothing*
        """
        sourcename = "frame-%02d.jpg" % (framenumber)
        sourcepath = dirname + "/" + sourcename
        abssourcepath = self.framesroot.AbsolutePath(sourcepath)

        for scale in self.scales:
            width, height = map(int, scale.split("x"))
            scaledframename = "frame-%02d (%d×%d).jpg" % (framenumber, width,
                                                          height)
            scaledframepath = dirname + "/" + scaledframename

            # In case the scaled version already exists, nothing will be done
            if self.framesroot.Exists(scaledframepath):
                continue

            absscaledframepath = self.framesroot.AbsolutePath(scaledframepath)

            size = (width, height)
            frame = Image.open(abssourcepath)
            frame.thumbnail(size, Image.BICUBIC)
            frame.save(absscaledframepath,
                       "JPEG",
                       optimize=True,
                       progressive=True)
        return

    def GeneratePreviews(self, dirname):
        """
        This method creates all preview animations (.webp), including scaled versions, from frames.
        The frames can be generated via :meth:`~GenerateFrames`.

        In case there is already a preview file, the method returns ``True`` without doing anything.

        Args:
            dirname (str): Name/Path of the directory to store the generated frames

        Returns:
            ``True`` on success, otherwise ``False``
        """
        # Create original sized preview
        framepaths = []
        for framenumber in range(self.maxframes):
            framename = "frame-%02d.jpg" % (framenumber + 1)
            framepath = dirname + "/" + framename
            framepaths.append(framepath)
        previewpath = dirname + "/preview.webp"

        success = True
        success &= self.CreateAnimation(framepaths, previewpath)

        # Create scaled down previews
        for scale in self.scales:
            framepaths = []
            width, height = map(int, scale.split("x"))

            for framenumber in range(self.maxframes):
                scaledframename = "frame-%02d (%d×%d).jpg" % (framenumber + 1,
                                                              width, height)
                scaledframepath = dirname + "/" + scaledframename
                framepaths.append(scaledframepath)

            previewpath = dirname + "/preview (%d×%d).webp" % (width, height)
            success &= self.CreateAnimation(framepaths, previewpath)

        return success

    def CreateAnimation(self, framepaths, animationpath):
        """
        This method creates a WebP animation from frames that are addresses by a sorted list of paths.
        Frame paths that do not exists or cannot be opened will be ignored.
        If there already exists an animation addressed by animation path, nothing will be done.

        The list of frame paths must at least contain 2 entries.

        Args:
            framepaths (list(str)): A list of relative frame paths that will be used to create an animation
            animationpath (str): A relative path where the animation shall be stored at.

        Returns:
            ``True`` when an animation has been created or exists, otherwise ``False``
        """
        if self.framesroot.IsFile(animationpath):
            logging.debug(
                "There is already an animation \"%s\" (Skipping frame generation process)",
                animationpath)
            return True

        # Load all frames
        frames = []
        for framepath in framepaths:
            absframepath = self.framesroot.AbsolutePath(framepath)

            try:
                frame = Image.open(absframepath)
            except FileNotFoundError as e:
                logging.warning(
                    "Unable to load frame \"$s\": %s \033[1;30m(Frame will be ignored)",
                    absframepath, str(e))
                continue

            frames.append(frame)

        # Check if enough frames for a preview have been loaded
        if len(frames) < 2:
            logging.error(
                "Not enough frames were loaded. Cannot create a preview animation. \033[1;30m(%d < 2)",
                len(frames))
            return False

        # Create absolute animation file path
        absanimationpath = self.framesroot.AbsolutePath(animationpath)

        # Calculate time for each frame in ms being visible
        duration = int((self.previewlength * 1000) / self.maxframes)

        # Store as WebP animation
        preview = frames[0]  # Start with frame 0
        preview.save(
            absanimationpath,
            save_all=True,  # Save all frames
            append_images=frames[1:],  # Save these frames
            duration=duration,  # Display time for each frame
            loop=0,  # Show in infinite loop
            method=6)  # Slower but better method [1]

        # [1] https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp

        return True

    def SetVideoFrames(self,
                       videoid,
                       framesdir,
                       thumbnailfile=None,
                       previewfile=None):
        """
        Set Database entry for the video with video ID ``videoid``.
        Using this method defines the frames directory to which all further paths are relative to.
        The thumbnail file addresses a static source frame (like ``frame-01.jpg``),
        the preview file addresses the preview animation (usually ``preview.webp``).

        If ``thumbnailfile`` or ``previewfile`` is ``None``, it will not be changed in the database.

        This method checks if the files exists.
        If not, ``False`` gets returned an *no* changes will be done in the database.

        Example:

            .. code-block:: python

                retval = vf.SetVideoFrames(1000, "Fleshgod Apocalypse/Monnalisa", "frame-02.jpg", "preview.webp")
                if retval == False:
                    print("Updating video entry failed.")

        Args:
            videoid (int): ID of the video that database entry shall be updated
            framesdir (str): Path of the video specific sub directory containing all frames/preview files. Relative to the video frames root directory
            thumbnailfile (str, NoneType): File name of the frame that shall be used as thumbnail, relative to ``framesdir``
            previewfile (str, NoneType): File name of the preview animation, relative to ``framesdir``

        Returns:
            ``True`` on success, otherwise ``False``
        """
        # Check if all files are valid
        if not self.framesroot.IsDirectory(framesdir):
            logging.error(
                "The frames directory \"%s\" does not exist in the video frames root directory.",
                framesdir)
            return False

        if thumbnailfile and not self.framesroot.IsFile(framesdir + "/" +
                                                        thumbnailfile):
            logging.error(
                "The thumbnail file \"%s\" does not exits in the frames directory \"%s\".",
                thumbnailfile, framesdir)
            return False

        if previewfile and not self.framesroot.IsFile(framesdir + "/" +
                                                      previewfile):
            logging.error(
                "The preview file \"%s\" does not exits in the frames directory \"%s\".",
                previewfile, framesdir)
            return False

        # Change paths in the database
        retval = self.db.SetVideoFrames(videoid, framesdir, thumbnailfile,
                                        previewfile)

        return retval

    def UpdateVideoFrames(self, video):
        """
        #. Create frames directory (:meth:`~CreateFramesDirectory`)
        #. Generate frames (:meth:`~GenerateFrames`)
        #. Generate previews (:meth:`~GeneratePreviews`)

        Args:
            video: Database entry for the video for that the frames and preview animation shall be updated

        Returns:
            ``True`` on success, otherwise ``False``
        """
        logging.info("Updating frames and previews for %s", video["path"])

        artist = self.db.GetArtistById(video["artistid"])
        artistname = artist["name"]
        videopath = video["path"]
        videoname = video["name"]
        videoid = video["id"]

        # Prepare everything to start generating frames and previews
        framesdir = self.CreateFramesDirectory(artistname, videoname)

        # Generate Frames
        retval = self.GenerateFrames(framesdir, videopath)
        if retval == False:
            return False

        # Generate Preview
        retval = self.GeneratePreviews(framesdir)
        if retval == False:
            return False

        # Update database
        retval = self.SetVideoFrames(videoid, framesdir, "frame-01.jpg",
                                     "preview.webp")
        return retval

    def ChangeThumbnail(self, video, timestamp):
        """
        This method creates a thumbnail image files, including scaled a version, from a video.
        The image will be generated from a frame addressed by the ``timestamp`` argument.

        To generate the thumbnail, ``ffmpeg`` is used in the following way:

        .. code-block:: bash

            ffmpeg -y -ss $timestamp -i $video["path"] -vf scale=iw*sar:ih -vframes 1 $videoframes/$video["framesdirectory"]/thumbnail.jpg

        ``video`` and ``timestamp`` are the parameters of this method.
        ``videoframes`` is the root directory for the video frames as configured in the MusicDB Configuration file.

        The scale solves the differences between the Display Aspect Ratio (DAR) and the Sample Aspect Ratio (SAR).
        By using a scale of image width multiplied by the SAR, the resulting frame has the same ratio as the video in the video player.

        The total length of the video gets determined by :meth:`~lib.metatags.MetaTags.GetPlaytime`
        If the time stamp is not between 0 and the total length, the method returns ``False`` and does nothing.

        When there is already a thumbnail existing it will be overwritten.

        Args:
            video: A video entry that shall be updated
            timestamp (int): Time stamp of the frame to select in seconds

        Returns:
            ``True`` on success, otherwise ``False``
        """

        dirname = video["framesdirectory"]
        videopath = video["path"]
        videoid = video["id"]

        # Determine length of the video in seconds
        try:
            self.metadata.Load(videopath)
            videolength = self.metadata.GetPlaytime()
        except Exception as e:
            logging.exception(
                "Generating a thumbnail for video \"%s\" failed with error: %s",
                videopath, str(e))
            return False

        if timestamp < 0:
            logging.warning(
                "Generating a thumbnail for video \"%s\" requires a time stamp > 0. Given was: %s",
                videopath, str(timestamp))
            return False

        if timestamp > videolength:
            logging.warning(
                "Generating a thumbnail for video \"%s\" requires a time stamp smaller than the video play time (%s). Given was: %s",
                videopath, str(videolength), str(timestamp))
            return False

        # Define destination path
        framename = "thumbnail.jpg"
        framepath = dirname + "/" + framename

        # create absolute paths for FFMPEG
        absframepath = self.framesroot.AbsolutePath(framepath)
        absvideopath = self.musicroot.AbsolutePath(videopath)

        # Run FFMPEG - use absolute paths
        process = [
            "ffmpeg",
            "-y",  # Yes, overwrite existing frame
            "-ss",
            str(timestamp),
            "-i",
            absvideopath,
            "-vf",
            "scale=iw*sar:ih",  # Make sure the aspect ration is correct
            "-vframes",
            "1",
            absframepath
        ]
        logging.debug("Getting thumbnail via %s", str(process))
        try:
            self.fs.Execute(process)
        except Exception as e:
            logging.exception(
                "Generating a thumbnail for video \"%s\" failed with error: %s",
                videopath, str(e))
            return False

        # Scale down the frame
        self.ScaleThumbnail(dirname)

        # Set new Thumbnail
        retval = self.SetVideoFrames(videoid,
                                     dirname,
                                     thumbnailfile="thumbnail.jpg",
                                     previewfile=None)
        if not retval:
            return False

        return True

    def ScaleThumbnail(self, dirname):
        """
        This method creates a scaled version of the existing thumbnail for a video.
        The aspect ration of the frame will be maintained.
        In case the resulting aspect ratio differs from the source file,
        the borders of the source frame will be cropped in the scaled version.

        If a scaled version exist, it will be overwritten.

        The scaled JPEG will be stored with optimized and progressive settings.

        Args:
            dirname (str): Name of the directory where the frames are stored at (relative)

        Returns:
            *Nothing*
        """
        sourcename = "thumbnail.jpg"
        sourcepath = dirname + "/" + sourcename
        abssourcepath = self.framesroot.AbsolutePath(sourcepath)

        for scale in self.scales:
            width, height = map(int, scale.split("x"))
            scaledframename = "thumbnail (%d×%d).jpg" % (width, height)
            scaledframepath = dirname + "/" + scaledframename

            absscaledframepath = self.framesroot.AbsolutePath(scaledframepath)

            size = (width, height)
            frame = Image.open(abssourcepath)
            frame.thumbnail(size, Image.BICUBIC)
            frame.save(absscaledframepath,
                       "JPEG",
                       optimize=True,
                       progressive=True)
        return