private List <CardSet> GetMTGJSONSets() { const string URL = "https://mtgjson.com/json/SetList.json.zip"; var sets = new List <CardSet>(); try { var zip = DownloadZIP(URL); var unzipped = Unzipper.Unzip(zip); string json = System.Text.Encoding.UTF8.GetString(unzipped); MTGJSONSetListSet[] setList = JsonConvert.DeserializeObject <MTGJSONSetListSet[]>(json); if (setList == null) { throw new InvalidDataException("Invalid JSON encountered"); } CardSet set; foreach (var mtgjsonSet in setList) { var code = mtgjsonSet.code; set = new CardSet { Name = mtgjsonSet.name, Code = code, MTGJSONURL = $"https://mtgjson.com/json/{code}.json.zip" }; sets.Add(set); } } catch (Exception ex) { DebugOutput.WriteLine(ex.ToString()); MessageBox.Show("Failed to gather list of available sets."); } return(sets); }
protected override void OnDoWork(DoWorkEventArgs e) { try { DownloadIcons(); CompletedWorkUnits = 4; UpdateIcon(); string name = CardSet.Name; string mtgjsonUrl = CardSet.MTGJSONURL; var zipped = DownloadZIP(CardSet.MTGJSONURL); var unzipped = Unzipper.Unzip(zipped); string json = System.Text.Encoding.UTF8.GetString(unzipped); CardSet = JsonConvert.DeserializeObject <CardSet>(json); if (CardSet == null) { throw new InvalidDataException("Invalid JSON encountered"); } CardSet.Name = name; CardSet.MTGJSONURL = mtgjsonUrl; foreach (var card in CardSet.Cards) { card.SetCode = CardSet.Code; card.Edition = CardSet.Name; } (CardSet.MythicRareIcon, CardSet.RareIcon, CardSet.UncommonIcon, CardSet.CommonIcon) = (mythicIcon, rareIcon, uncommonIcon, commonIcon); using (var context = new MyDbContext()) { context.Upsert(CardSet); foreach (var card in CardSet.Cards) { if (card.type.Contains("Basic Land")) // workaround needed because mtgjson thinks basic land is not a separate rarity from common { card.rarity = "basic land"; } context.Upsert(card); } context.SaveChanges(); } RunState = RunState.Completed; CompletedWorkUnits = TotalWorkUnits; } catch (Exception ex) { DebugOutput.WriteLine(ex.ToString()); RunState = RunState.Failed; } finally { UpdateIcon(); watch.Stop(); } }