예제 #1
0
        // Get AH data from API and return it.
        // Returns dynamic (bypasses static type checking)
        private dynamic getAhData(AhDownloadLink downloadLink)
        {
            string  ahDataString;
            dynamic json;

            try
            {
                using (WebClient wc = new WebClient())
                {
                    Logger.Write(LogType.INFO, "Starting to download AH data");
                    ahDataString = wc.DownloadString(downloadLink.URL);
                }
            }
            catch (WebException ex)
            {
                Logger.Write(LogType.FATAL, "Unable to download current AH data: " + ex.Message);
                return(null); // Just return null so IDE is happy. We won't get in here since FATAL errors close the application.
            }

            Logger.Write(LogType.INFO, "Got AH data. Length: " + System.Text.Encoding.UTF8.GetByteCount(ahDataString) + " bytes.");

            try
            {
                json = JsonConvert.DeserializeObject(ahDataString);
            }
            catch (JsonException ex)
            {
                Logger.Write(LogType.FATAL, "Unable to parse AH data json: " + ex.Message);
                return(null); // Just return null so IDE is happy. We won't get in here since FATAL errors close the application.
            }

            return(json);
        }
예제 #2
0
        private AhDownloadLink getAhDataDlLink()
        {
            string  jsonString;
            dynamic json;

            // Download url
            try
            {
                using (WebClient wc = new WebClient())
                {
                    jsonString = wc.DownloadString(apiurl);
                }
            }
            catch (WebException ex)
            {
                Logger.Write(LogType.FATAL, "Could not retrieve the ah download string: " + ex.Message);
                return(null);
            }

            // Parse json
            try
            {
                json = JsonConvert.DeserializeObject(jsonString);
            }
            catch (Exception ex)
            {
                Logger.Write(LogType.FATAL, "Unable to parse JSON: " + ex.Message);
                return(null);
            }

            string url = Convert.ToString(json["files"][0]["url"]);
            string lastModifiedString = Convert.ToString(json["files"][0]["lastModified"]);

            timestamp = long.Parse(lastModifiedString.Remove(lastModifiedString.Length - 3));

            long lastModified = long.Parse(lastModifiedString);


            AhDownloadLink downloadLink = new AhDownloadLink(url, lastModified);

            return(downloadLink);
        }