Add functionality to save set images

This commit is contained in:
James Skemp 2024-01-11 22:17:45 -06:00
parent 440d414692
commit c6eded2a47
2 changed files with 37 additions and 0 deletions

View File

@ -94,6 +94,41 @@ if (args.Length >= 1)
Console.WriteLine("File saved.");
}
if (args.Any(a => a.StartsWith("images"))) {
var sets = args.First(a => a.StartsWith("images")).Split(":").Skip(1);
if (sets.Any()) {
using var httpClient = new HttpClient();
foreach (var set in sets) {
var setCards = interestedCards.Where(c => c.Set == set).ToList();
if (!setCards.Any()) {
continue;
}
Console.WriteLine($"Downloading images from set {set}.");
var setImagesPath = Path.Combine("images", set);
Directory.CreateDirectory(setImagesPath);
Console.WriteLine();
string cardName = "";
foreach (var setCard in setCards) {
// TODO this doesn't work
Console.Write(new string('\b', cardName.Length));
cardName = setCard.Name;
Console.Write(cardName);
if (!string.IsNullOrWhiteSpace(setCard.ImageUris?.Normal)) {
using var stream = httpClient.GetStreamAsync(setCard.ImageUris?.Normal);
using var fileStream = new FileStream(Path.Combine(setImagesPath, $"{setCard.Id}.jpg"), FileMode.Create);
stream.Result.CopyTo(fileStream);
// Add a short delay between image downloads.
Thread.Sleep(2000);
}
}
Console.WriteLine($"Card images saved: {setCards.Count}");
}
}
}
var randomCard = interestedCards[new Random().Next(interestedCards.Count)];

View File

@ -11,6 +11,8 @@ Downloads card images from Scryfall.
- Optional arguments:
- `sets` to get a listing of sets (identifier, name, and release date)
- `save` will save interested cards to a **cards.json** file in the current directory.
- `images` will pull and save the normal (JPG) images for the sets listed after, using a `:` separator.
- For example, `images:3ed:ice` downloads the 3rd Edition and Ice Age card images.
## Pending Improvements
- Allow sets to download to be passed via argument or via configuration.