Exemplo n.º 1
0
        void FanArtTV_CheckArtistImage(MDBImageType imageType, MDBArtist artist, JsonNode node)
        {
            foreach (JsonNode v in node.Values)
            {
                try
                {
                    string source = v["url"].Value.ToString();

                    MDBFolder folder = mdb.GetArtistArtFolder(artist);
                    string    fileNameWithoutExtension = imageType.ToString() + " " + v["id"].Value.ToString();

                    byte[] imageData = null;
                    //download only if not exists
                    string fullPath = mdb.GetImageFileNameWithExtension(folder, fileNameWithoutExtension);
                    if (fullPath != null)
                    {
                        //file exists
                        MDBFile mdbFile;
                        mdb.RegisterFile(folder, fullPath, out mdbFile);
                        if (mdb.Images.Exist(nameof(MDBImage.FileID), mdbFile.ID))
                        {
                            continue;
                        }
                        //register image (image dataset missing)
                        imageData = mdb.TryLoadImageData(fullPath);
                    }
                    //download
                    if (imageData == null)
                    {
                        this.LogInfo("Download FanArtTV Image <cyan>{0}", source);
                        imageData = mdb.TryDownloadImageData(source);
                        if (imageData == null)
                        {
                            this.LogInfo("<yellow>Invalid FanArtTV Artist image <default>for artist <red>{0}", artist.Name);
                            continue;
                        }
                    }
                    mdb.SaveArtistImage(folder, fileNameWithoutExtension, imageType, artist, imageData);
                }
                catch (Exception ex)
                {
                    this.LogWarning(ex, "Error saving artist image {0}.", artist);
                }
            }
        }
Exemplo n.º 2
0
        void FanArtTV_ParseGuidRoot(MDBArtist artist, JsonNode node)
        {
            foreach (JsonNode v in node.SubNodes)
            {
                if (string.IsNullOrEmpty(v.Name))
                {
                    continue;
                }
                var guid   = new Guid(v.Name);
                var albums = mdb.Albums.GetStructs(
                    Search.FieldEquals(nameof(MDBAlbum.MusicBrainzAlbumGuid), guid) |
                    Search.FieldEquals(nameof(MDBAlbum.MusicBrainzReleaseGroupGuid), guid));

                foreach (JsonNode n in v.SubNodes)
                {
                    FanArtTV_ParseNode(artist, albums, n);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>Loads the specified album.</summary>
        /// <param name="mdb">The MDB.</param>
        /// <param name="album">The album.</param>
        /// <returns></returns>
        public static RPCAlbum Load(MusicDataBase mdb, MDBAlbum album)
        {
            MDBArtist artist = mdb.Artists.GetStruct(album.ArtistID);
            var       files  = mdb.AudioFiles.GetStructs(nameof(MDBAudioFile.AlbumID), album.ID);
            var       result = new RPCAlbum()
            {
                ID         = album.ID,
                Name       = album.Name,
                ArtistID   = artist.ID,
                ArtistName = artist.Name,
                Duration   = new TimeSpan(files.Sum(f => f.Duration.Ticks)),
                Tags       = files.SelectMany(f => f.TagNames).Distinct().Join(";"),
                Genres     = files.SelectMany(f => f.GenreNames).Distinct().Join(";"),
                TrackCount = files.Count,
                Errors     = (MDBMetaErrors)files.BinaryOr(f => (long)f.MetaErrors),
            };

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>Loads the artist from the specified MusicDataBase tables.</summary>
        /// <param name="mdb">The MusicDataBase.</param>
        /// <param name="artist">The artist.</param>
        /// <returns>Returns a new <see cref="RPCArtist"/> instance</returns>
        public static RPCArtist Load(MusicDataBase mdb, MDBArtist artist)
        {
            var files = mdb.AudioFiles.GetStructs(
                Search.FieldEquals(nameof(MDBAudioFile.SongArtistID), artist.ID) |
                Search.FieldEquals(nameof(MDBAudioFile.AlbumArtistID), artist.ID));

            var result = new RPCArtist()
            {
                ID         = artist.ID,
                Name       = artist.Name,
                Errors     = (MDBMetaErrors)files.BinaryOr(f => (long)f.MetaErrors),
                TitleCount = files.Count,
                Tags       = files.SelectMany(f => f.TagNames).Distinct().Join(";"),
                Genres     = files.SelectMany(f => f.GenreNames).Distinct().Join(";"),
                Duration   = new TimeSpan(files.Sum(f => f.Duration.Ticks)),
            };

            result.AlbumCount = mdb.Albums.Count(nameof(MDBAlbum.ArtistID), artist.ID);
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>Writes the artist ini.</summary>
        /// <param name="folder">The folder.</param>
        /// <param name="artist">The artist.</param>
        /// <exception cref="ArgumentNullException">MusicBrainzArtistGuid</exception>
        public void WriteArtistIni(MDBFolder folder, MDBArtist artist)
        {
            if (artist.MusicBrainzArtistGuid == null)
            {
                throw new ArgumentNullException("MusicBrainzArtistGuid");
            }

            string artistIniFile = folder.GetFullPath(this, "artist.ini");

            FileSystem.TouchFile(artistIniFile);
            var ini = IniReader.FromFile(artistIniFile);

            var writer = new IniWriter(ini);

            writer.WriteSetting("Artist", "Guid", artist.MusicBrainzArtistGuid.ToString());
            var artists = new Set <string>(ini.ReadSection("Artists"));

            artists.Include(artist.Name);
            writer.WriteSection("Artists", artists);
            writer.Save();
        }
Exemplo n.º 6
0
        public MDBFolder GetArtistArtFolder(MDBArtist artist)
        {
            string guidData = Base32.Safe.Encode(artist.MusicBrainzArtistGuid.ToArray());

            var sb = new StringBuilder();
            int i  = 0;

            foreach (char c in guidData)
            {
                if (++i == 2)
                {
                    sb.Append('/');
                    i = 0;
                }
                sb.Append(c);
            }
            string folder = FileSystem.Combine(ArtistArtFolder, sb.ToString());

            Directory.CreateDirectory(folder);
            return(GetOrCreateFolder(folder));
        }
Exemplo n.º 7
0
        void FanArtTV_ParseNode(MDBArtist artist, IEnumerable <MDBAlbum> albums, JsonNode node)
        {
            MDBImageType imageType = MDBImageType.Undefined;

            switch (node.Name)
            {
            case "mbid_id":
            case "name":
                return;

            case "hdmusiclogo": imageType = MDBImageType.ArtistMusicLogoHD; break;

            case "musiclogo": imageType = MDBImageType.ArtistMusicLogo; break;

            case "albums": FanArtTV_ParseGuidRoot(artist, node); break;

            case "cdart": imageType = MDBImageType.AlbumCDArt; break;

            case "albumcover": imageType = MDBImageType.AlbumCover; break;

            case "artistthumb": imageType = MDBImageType.ArtistThumb; break;

            case "artistbackground": imageType = MDBImageType.ArtistBackground; break;

            case "musicbanner": imageType = MDBImageType.ArtistMusicBanner; break;

            default: throw new NotImplementedException(string.Format("Unknown FanArtTV node {0}", node.Name));
            }
            if (imageType == MDBImageType.Undefined)
            {
                return;
            }
            if (imageType.IsAlbumArt())
            {
                FanArtTV_CheckAlbumImage(imageType, artist, albums, node);
                return;
            }
            FanArtTV_CheckArtistImage(imageType, artist, node);
        }
Exemplo n.º 8
0
        /// <summary>Saves the artist image.</summary>
        /// <param name="folder">The folder.</param>
        /// <param name="name">The name.</param>
        /// <param name="imageType">Type of the image.</param>
        /// <param name="artist">The artist.</param>
        /// <param name="data">The data.</param>
        /// <exception cref="Exception">Invalid image type!</exception>
        public void SaveArtistImage(MDBFolder folder, string name, MDBImageType imageType, MDBArtist artist, byte[] data)
        {
            var image = new MDBImage()
            {
                MusicBrainzGuid = artist.MusicBrainzArtistGuid,
                Type            = imageType,
            };

            if (!image.IsArtistArt)
            {
                throw new Exception("Invalid image type!");
            }

            SaveImage(data, folder, name, ref image, artist);
            WriteArtistIni(folder, artist);
        }
Exemplo n.º 9
0
        void FanArtTV_DownloadArtist(MDBArtist artist)
        {
            byte[]     data   = null;
            JsonReader reader = null;

            {
                this.LogDebug("Starting FanArtTV lookup of {0}", artist);
                string fileName = FileSystem.Combine(mdb.CacheFolder, "FanArtTV", artist.MusicBrainzArtistGuid.ToString() + ".json");
                if (File.Exists(fileName) && (DateTime.UtcNow - FileSystem.GetLastWriteTimeUtc(fileName) < TimeSpan.FromDays(30)))
                {
                    this.LogDebug("Load from cache {0}", fileName);
                    try { reader = new JsonReader(File.ReadAllBytes(fileName)); }
                    catch { try { File.Delete(fileName); } catch { } }
                }
                if (reader == null)
                {
                    //download json
                    if (!mdb.CheckBlackList(MDBCrawlerBlackListItemType.FanArtTV, artist.MusicBrainzArtistGuid))
                    {
                        return;
                    }

                    for (int retry = 0; ; retry++)
                    {
                        if (m_Exit)
                        {
                            return;
                        }
                        try
                        {
                            this.LogDebug("Download FanArtTV Artist <cyan>" + artist.Name + " <default>" + artist.MusicBrainzArtistGuid);
                            var http = new HttpConnection();
                            http.Timeout = TimeSpan.FromSeconds(30);
                            data         = http.Download("http://webservice.fanart.tv/v3/music/" + artist.MusicBrainzArtistGuid + "?api_key=" + FanArtTV_APIKEY);
                            reader       = new JsonReader(data);
                            Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                            File.WriteAllBytes(fileName, data);
                            this.LogNotice("<green>New fanart.tv dataset <default>for <cyan>" + artist.Name + " <default>" + artist.MusicBrainzArtistGuid);
                            break;
                        }
                        catch (WebException wex)
                        {
                            if (retry < 10 && mdb.CheckDownloadRetry(wex))
                            {
                                Thread.Sleep(1000);
                                this.LogDebug(wex, "Retry {0}: {1} <red>{2}", retry, artist, wex.Message);
                                continue;
                            }
                            this.LogWarning(wex, "<yellow>No fanart.tv dataset <default>for {0} - <red>{1}", artist, wex.Message);
                            return;
                        }
                        catch (Exception ex)
                        {
                            this.LogWarning(ex, "<yellow>No fanart.tv dataset <default>for {0} - <red>{1}", artist, ex.Message);
                            return;
                        }
                    }
                }
            }
            foreach (var node in reader.Root.SubNodes)
            {
                try
                {
                    FanArtTV_ParseNode(artist, null, node);
                }
                catch (Exception ex)
                {
                    this.LogDebug(ex, "Error while parsing FanArtTV node");
                    return;
                }
            }

            this.LogVerbose(string.Format("<cyan>{0} <default>{1} <green>ok", artist.Name, artist.MusicBrainzArtistGuid));
        }
Exemplo n.º 10
0
 void FanArtTV_CheckAlbumImage(MDBImageType imageType, MDBArtist artist, IEnumerable <MDBAlbum> albums, JsonNode node)
 {
     if (!albums.Any())
     {
         return;
     }
     foreach (JsonNode v in node.Values)
     {
         try
         {
             string source = v["url"].Value.ToString();
             string fileNameWithoutExtension = imageType.ToString() + " " + v["id"].Value.ToString();
             //check if each album has this image, autoload if we already have it at any album
             byte[] imageData = null;
             var    albumsWithMissingImages = new List <MDBAlbum>();
             FindAlbumImage(albums, fileNameWithoutExtension, out imageData, out albumsWithMissingImages);
             if (albumsWithMissingImages.Count == 0)
             {
                 continue;
             }
             //try load from files table
             if (imageData == null)
             {
                 foreach (var file in mdb.Files.GetStructs(nameof(MDBFile.Name), fileNameWithoutExtension))
                 {
                     string fullPath = file.GetFullPath(mdb);
                     imageData = mdb.TryLoadImageData(fullPath);
                     if (imageData == null)
                     {
                         File.Delete(fullPath);
                         mdb.Files.Delete(file.ID);
                     }
                 }
             }
             //if not found, load from web
             if (imageData == null)
             {
                 this.LogInfo("Download FanArtTV Image <blue>{0}", source);
                 imageData = mdb.TryDownloadImageData(source);
                 if (imageData == null)
                 {
                     throw new Exception("Could not load image data!");
                 }
             }
             //save to all albums
             foreach (MDBAlbum album in albumsWithMissingImages)
             {
                 var albumFolder = GetAlbumFolder(album);
                 if (albumFolder.ID == 0)
                 {
                     continue;
                 }
                 mdb.SaveAlbumImage(albumFolder, fileNameWithoutExtension, imageType, album, imageData);
             }
         }
         catch (Exception ex)
         {
             this.LogWarning(ex, "Error saving {0} image {1} for artist {2}.", imageType, v, artist);
         }
     }
 }