Exemple #1
0
        /// <summary>Tries to download the image data.</summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        public byte[] TryDownloadImageData(string url)
        {
            byte[] imageData = null;
            int    i         = 0;

            while (imageData == null && i++ < 10)
            {
                try
                {
                    var http = new HttpConnection
                    {
                        Timeout = TimeSpan.FromSeconds(60)
                    };
                    imageData = http.Download(url);
                    break;
                }
                catch (WebException ex)
                {
                    if (CheckDownloadRetry(ex))
                    {
                        Thread.Sleep(1000);
                        this.LogDebug(ex, "Retry: {0} <red>{1}", url, ex.Message);
                        continue;
                    }
                    this.LogDebug(ex, "Could not download <red>{0}", url);
                    break;
                }
                catch (Exception ex)
                {
                    this.LogDebug(ex, "Could not download <red>{0}", url);
                    break;
                }
            }
            if (imageData != null)
            {
                imageData = TryLoadImageData(imageData);
            }

            return(imageData);
        }
        void Check(IPAddress ip, int port, int streamBasePort)
        {
            try
            {
                this.LogDebug("Test mdb server <cyan>{0}<default> at <cyan>{1}:{2}", Host, ip, port);
                string server = (ip.AddressFamily == AddressFamily.InterNetworkV6) ? $"[{ip}]" : ip.ToString();

                var con = new HttpConnection();
                {
                    con.Timeout = TimeSpan.FromSeconds(2);
                    Image       = con.Download($"http://{server}:{port}/avatar/get?text={Host}");
                }
                var        req = new XmlRequest(new Uri($"http://{server}:{port}/mdb/player/state.xml"));
                WebMessage msg = req.Get();
                if (msg.Error == WebError.None)
                {
                    lock (this)
                    {
                        if (Checked)
                        {
                            return;
                        }
                        this.LogNotice("Selected mdb server <green>{0}<default> at <green>{1}:{2}", Host, ip, port);
                        Address        = ip;
                        Port           = port;
                        StreamBasePort = streamBasePort;
                        Checked        = true;
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                this.LogVerbose(ex, "Could not reach mdb server <red>{0}<default> at <red>{1}:{2}", Host, ip, port);
            }
        }
        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));
        }