コード例 #1
0
        JsonReader MusicBrainz_Get(string uri, string path, Guid mbid)
        {
            bool   writeFile  = false;
            string mbJsonFile = FileSystem.Combine(path, mbid + ".json");
            string mbJsonData = null;

            if (File.Exists(mbJsonFile))
            {
                mbJsonData = File.ReadAllText(mbJsonFile);
            }
            if (mbJsonData == null)
            {
                bool ok = false;
                for (int i = 0; i < 1000; i++)
                {
                    try
                    {
                        if (i == 0)
                        {
                            this.LogDebug("Download MusicBrainz " + uri);
                        }
                        if (m_Exit)
                        {
                            return(null);
                        }
                        string mbPageData = HttpConnection.GetString(uri);
                        mbJsonData = StringExtensions.GetString(mbPageData, -1, "<script type=\"application/ld+json\">", "</script>");
                        ok         = true;
                        break;
                    }
                    catch (WebException ex)
                    {
                        if (mdb.CheckDownloadRetry(ex))
                        {
                            Thread.Sleep(1000);
                            this.LogDebug(ex, "Retry: {0} <red>{1}", uri, ex.Message);
                            continue;
                        }
                        this.LogDebug(ex, "<yellow>No musicbrainz dataset<default> for {0} <red>{1}", uri, ex.Message);
                        return(null);
                    }
                    catch (Exception ex)
                    {
                        this.LogDebug(ex, "<yellow>No musicbrainz dataset<default> for {0} <red>{1}", uri, ex.Message);
                        return(null);
                    }
                }
                if (!ok)
                {
                    this.LogError("<red>Error:<default> Could not retrieve <red>" + uri);
                    return(null);
                }
                writeFile = true;
            }
            var reader = new JsonReader(Encoding.UTF8.GetBytes(mbJsonData));

            if (writeFile)
            {
                File.WriteAllText(mbJsonFile, mbJsonData);
            }
            return(reader);
        }