images: added double faced images support from REX, improved errors processing;

This commit is contained in:
Oleg Agafonov 2024-01-20 15:29:05 +04:00
parent bbe452352f
commit 258a98bddd
3 changed files with 36 additions and 18 deletions

View file

@ -81,7 +81,7 @@ public enum ScryfallImageSource implements CardImageSource {
String link = ScryfallImageSupportCards.findDirectDownloadLink(card.getSet(), card.getName(), card.getCollectorId());
if (link != null) {
if (ScryfallImageSupportCards.isApiLink(link)) {
// api
// api link - must prepare direct link
baseUrl = link + localizedCode + "?format=image";
alternativeUrl = link + defaultCode + "?format=image";
// workaround to use cards without english images (some promos or special cards)
@ -89,7 +89,7 @@ public enum ScryfallImageSource implements CardImageSource {
alternativeUrl = alternativeUrl.replace("/en?format=image", "?format=image");
}
} else {
// image
// direct link to image
baseUrl = link;
}
}

View file

@ -556,6 +556,7 @@ public class ScryfallImageSupportCards {
//
// example:
// api link: https://api.scryfall.com/cards/trix/6/
// api direct link: https://api.scryfall.com/cards/rex/26/en?format=image&face=back
// image link: https://c1.scryfall.com/file/scryfall-cards/large/back/d/5/d5dfd236-b1da-4552-b94f-ebf6bb9dafdf.jpg
//
// key for one card:
@ -565,6 +566,11 @@ public class ScryfallImageSupportCards {
// set/card_name/card_number_1
// set/card_name/card_number_2
//
// double faced cards:
// front face image: format=image&face=front
// back face image: format=image&face=back
// example: https://api.scryfall.com/cards/rex/26/en?format=image&face=back
//
// Cards with non-ASCII collector numbers must use direct download (cause xmage uses different card number)
// Verify checks must check and show missing data from that list,
// see test_checkMissingScryfallSettingsAndCardNumbers
@ -1013,8 +1019,22 @@ public class ScryfallImageSupportCards {
// PRES
put("PRES/Lathliss, Dragon Queen/149*", "https://api.scryfall.com/cards/pres/149★/");
// CALC -- custom alchemy version of cards.
// CALC - custom alchemy version of cards.
put("CALC/C-Pillar of the Paruns", "https://api.scryfall.com/cards/dis/176/");
// REX - double faced lands (xmage uses two diff lands for it)
put("REX/Command Tower/26", "https://api.scryfall.com/cards/rex/26/en?format=image");
put("REX/Command Tower/26b", "https://api.scryfall.com/cards/rex/26/en?format=image&face=back");
put("REX/Forest/25", "https://api.scryfall.com/cards/rex/25/en?format=image");
put("REX/Forest/25b", "https://api.scryfall.com/cards/rex/25/en?format=image&face=back");
put("REX/Island/22", "https://api.scryfall.com/cards/rex/22/en?format=image");
put("REX/Island/22b", "https://api.scryfall.com/cards/rex/22/en?format=image&face=back");
put("REX/Mountain/24", "https://api.scryfall.com/cards/rex/24/en?format=image");
put("REX/Mountain/24b", "https://api.scryfall.com/cards/rex/24/en?format=image&face=back");
put("REX/Plains/21", "https://api.scryfall.com/cards/rex/21/en?format=image");
put("REX/Plains/21b", "https://api.scryfall.com/cards/rex/21/en?format=image&face=back");
put("REX/Swamp/23", "https://api.scryfall.com/cards/rex/23/en?format=image");
put("REX/Swamp/23b", "https://api.scryfall.com/cards/rex/23/en?format=image&face=back");
}
};
@ -1069,7 +1089,7 @@ public class ScryfallImageSupportCards {
}
public static boolean isApiLink(String link) {
return !link.endsWith(".jpg") && !link.endsWith(".png");
return !link.endsWith(".jpg") && !link.endsWith(".png") && !link.contains("format=image");
}
public static Map<String, String> getDirectDownloadLinks() {

View file

@ -51,6 +51,8 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
private static final int MAX_ERRORS_COUNT_BEFORE_CANCEL = 50;
private static final int MIN_FILE_SIZE_OF_GOOD_IMAGE = 1024 * 10; // protect from wrong data save
private final DownloadImagesDialog uiDialog;
private boolean needCancel;
private int errorCount;
@ -431,7 +433,9 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
List<CardDownloadData> allCardsUrls = Collections.synchronizedList(new ArrayList<>());
try {
allCards.parallelStream().forEach(card -> {
if (!card.getCardNumber().isEmpty() && !"0".equals(card.getCardNumber()) && !card.getSetCode().isEmpty()) {
if (!card.getCardNumber().isEmpty()
&& !"0".equals(card.getCardNumber())
&& !card.getSetCode().isEmpty()) {
String cardName = card.getName();
CardDownloadData url = new CardDownloadData(cardName, card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, false, card.isDoubleFaced(), card.isNightCard());
@ -545,9 +549,14 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
cardsToDownload.add(card);
} else {
// need missing cards
File file = new TFile(CardImageUtils.buildImagePathToCardOrToken(card));
String imagePath = CardImageUtils.buildImagePathToCardOrToken(card);
File file = new TFile(imagePath);
if (!file.exists()) {
cardsToDownload.add(card);
} else if (file.length() < MIN_FILE_SIZE_OF_GOOD_IMAGE) {
// how-to fix: if it really downloads image data then set lower file size
logger.error("Found broken file: " + imagePath);
cardsToDownload.add(card);
}
}
});
@ -744,17 +753,6 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
destFile = new TFile(CardImageUtils.buildImagePathToCardOrToken(card));
}
// FILE already exists (in zip or in dir)
// don't use, images can be re-downloaded
/*
if (destFile.exists()) {
synchronized (sync) {
update(cardIndex + 1, count);
}
return;
}
*/
// check zip access
TFile testArchive = destFile.getTopLevelArchive();
if (testArchive != null && testArchive.exists()) {
@ -916,7 +914,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
List<CardDownloadData> downloadedCards = Collections.synchronizedList(new ArrayList<>());
DownloadPicturesService.this.cardsMissing.parallelStream().forEach(cardDownloadData -> {
TFile file = new TFile(CardImageUtils.buildImagePathToCardOrToken(cardDownloadData));
if (file.exists()) {
if (file.exists() && file.length() > MIN_FILE_SIZE_OF_GOOD_IMAGE) {
downloadedCards.add(cardDownloadData);
}
});