static async Task Main(string[] args) { var httpClient = new HttpClient { BaseAddress = ScryfallApiClientConfig.GetDefault().ScryfallApiBaseAddress }; var client = new ScryfallApiClient(httpClient); do { try { var randomCard = await client.Cards.GetRandom(); Console.Clear(); Console.WriteLine(DrawCard(randomCard)); } catch (ScryfallApiException ex) { Console.Clear(); Console.WriteLine($"Error: {ex.Message}"); Console.WriteLine($"Status Code: {ex.ResponseStatusCode}"); Console.WriteLine($"Remote Call: {ex.RequestMethod} {ex.RequestUri}"); } Console.WriteLine(Environment.NewLine + "Press any key for a new card. Press Esc to quit."); } while (Console.ReadKey().Key != ConsoleKey.Escape); }
public static async Task SaveImageFiles(string path, ScryfallApiClient client, IEnumerable <Deck> decks) { var sort = new CardSort(); var webClient = new WebClient(); var invalidChars = Path.GetInvalidFileNameChars(); foreach (var deck in decks) { var deckName = Path.GetFileNameWithoutExtension(deck.FilePath); var deckPath = Path.Combine(path, "Downloaded", deckName); Directory.CreateDirectory(deckPath); foreach (var card in deck.Cards) { var name = new string(card.Name.Where(x => !invalidChars.Contains(x)).ToArray()); var filePath = Path.Combine(deckPath, name + " (" + card.Quantity + ").jpg"); if (File.Exists(filePath)) { continue; } Console.WriteLine("Downloading " + deckName + ": " + card.Name + "..."); var data = await GetCard(card, client, sort); var images = data.ImageUris; if (images != null) { var imageUri = images["border_crop"]; webClient.DownloadFile(imageUri.ToString(), filePath); } else { Console.WriteLine("Multiple faces..."); webClient.DownloadFile(data.CardFaces[0].ImageUris["border_crop"].ToString(), filePath); var facePath = Path.Combine(deckPath, name + " (" + card.Quantity + ")"); Directory.CreateDirectory(facePath); int i = 1; foreach (var face in data.CardFaces) { filePath = Path.Combine(facePath, i++ + ".jpg"); var imageUri = face.ImageUris["border_crop"]; webClient.DownloadFile(imageUri.ToString(), filePath); } } } } }
public async Task GetWarOfTheSpark() { var client = new ScryfallApiClient(new HttpClient()); var cards = await client.GetCardsFromSetAsync("WAR"); cards.Should().HaveCount(265); cards.Should().Contain(c => c.name == "Karn, the Great Creator"); cards.Should().Contain(c => c.name == "Oath of Kaya"); cards.Should().Contain(c => c.name == "Mobilized District"); cards.Should().Contain(c => c.name == "Forest"); }
public SearchModel(ScryfallApiClient scryfallApi) { _scryfallApi = scryfallApi ?? throw new ArgumentNullException(nameof(scryfallApi)); }
public CardParser(ScryfallApiClient client) { this.client = client; }
private static async Task <ScryfallApi.Client.Models.Card> GetCard(CardEntry card, ScryfallApiClient client, CardSort sort) { if (card.ScryfallId != null) { try { return(await client.Cards.GetById(card.ScryfallId)); } catch (Exception) { Console.Error.WriteLine("Could not get data by id, trying by name instead..."); } } var results = (await client.Cards.Search(card.Name, 0, sort)).Data; return(results.FirstOrDefault(x => x.Name == card.Name) ?? results.First()); }