public static void Read(string filename = "") { Entries.Clear(); if (String.IsNullOrEmpty(filename)) { string executable = System.Reflection.Assembly.GetEntryAssembly().Location; filename = Path.GetDirectoryName(executable) + "\\" + LocalFilename; if (!File.Exists(filename)) { return; } } if (!Helper.IsFileValid(filename)) { return; } using (FileStream stream = File.Open(filename, FileMode.Open)) { using (StreamReader reader = new StreamReader(stream)) { string json = reader.ReadToEnd(); JArray entries = (JArray)JsonConvert.DeserializeObject(json); if (entries == null) { return; } foreach (JToken token in entries.Children()) { WulinshuRaymonfAPIEntry entry = new WulinshuRaymonfAPIEntry { Path = token.SelectToken("Path").Value <string>(), Hash = token.SelectToken("Hash").Value <string>(), Matches = token.SelectToken("Matches").Value <int>(), Game = token.SelectToken("Game").Value <string>() }; Entries.Add(entry); } } } }
public void FetchData(string game) { aborted = false; Entries.Clear(); string url = String.Format(GetFormat, Url, 1, game); WebRequest request = WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response == null || response.StatusCode != HttpStatusCode.OK) { using (StreamReader reader = new StreamReader(Resources.data["backup"])) { string json = reader.ReadToEnd(); JArray entries = (JArray)JsonConvert.DeserializeObject(json); foreach (JToken token in entries.Children()) { WulinshuRaymonfAPIEntry entry = new WulinshuRaymonfAPIEntry { Path = token.SelectToken("Path").Value <string>(), Hash = token.SelectToken("Hash").Value <string>(), Matches = token.SelectToken("Matches").Value <int>(), Game = token.SelectToken("Game").Value <string>() }; Entries.Add(entry); } } Finished(this, new FinishedArgs(true)); return; } string text; int pageCount = 1; using (StreamReader sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(text); pageCount = (int)data.SelectToken("last_page").Value; } for (int i = 1; i < pageCount; i++) { if (aborted) { Finished(this, new FinishedArgs(false)); return; } DescriptionChanged(this, new DescriptionChangedArgs(String.Format("Fetching page {0}...", i))); ProgressChanged(this, new ProgressChangedArgs(i, pageCount)); url = String.Format(GetFormat, Url, i, game); request = WebRequest.Create(url); response = (HttpWebResponse)request.GetResponse(); using (StreamReader sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(text); JToken token = data.SelectToken("data"); foreach (JToken child in token.Children()) { WulinshuRaymonfAPIEntry entry = new WulinshuRaymonfAPIEntry { Path = child.SelectToken("path").Value <string>(), Hash = child.SelectToken("hash").Value <string>(), Matches = child.SelectToken("matches").Value <int>(), Game = child.SelectToken("game").Value <string>() }; Entries.Add(entry); } } } Finished(this, new FinishedArgs(true)); }