mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 12:18:00 +00:00
ImageFetcher now uses scryfallCode instead of testing URLs to fetch images.
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package forge.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -25,8 +22,6 @@ public abstract class ImageFetcher {
|
||||
// see https://scryfall.com/docs/api/languages and
|
||||
// https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
|
||||
private static final HashMap<String, String> langCodeMap = new HashMap<>();
|
||||
private static final Map<String, String> scryfallSetCodes = new HashMap<>();
|
||||
private static final String INVALID_SCRYFALL_SET_CODE = "NotFound";
|
||||
|
||||
static {
|
||||
langCodeMap.put("en-US", "en");
|
||||
@@ -44,63 +39,15 @@ public abstract class ImageFetcher {
|
||||
private HashMap<String, HashSet<Callback>> currentFetches = new HashMap<>();
|
||||
private HashMap<String, String> tokenImages;
|
||||
|
||||
private static boolean isValidScryfallURL(final String urlString){
|
||||
try {
|
||||
URL u = new URL(urlString);
|
||||
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
|
||||
huc.setInstanceFollowRedirects(true);
|
||||
huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) " +
|
||||
"Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
|
||||
huc.setRequestMethod("HEAD");
|
||||
return (huc.getResponseCode() == HttpURLConnection.HTTP_OK);
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String getScryfallDownloadURL(PaperCard c, boolean backFace, String langCode){
|
||||
String setCode = scryfallSetCodes.getOrDefault(c.getEdition(), null);
|
||||
if ((setCode != null) && (!setCode.equals(INVALID_SCRYFALL_SET_CODE))){
|
||||
return ForgeConstants.URL_PIC_SCRYFALL_DOWNLOAD +
|
||||
ImageUtil.getScryfallDownloadUrl(c, backFace, setCode, langCode);
|
||||
}
|
||||
|
||||
// No entry matched yet for edition
|
||||
StaticData data = StaticData.instance();
|
||||
CardEdition edition = data.getEditions().get(c.getEdition());
|
||||
if (edition == null) // edition does not exist - some error occurred with card data
|
||||
return null;
|
||||
// 1. Try MCI code first, as it original.
|
||||
String mciCode = edition.getMciCode().toLowerCase();
|
||||
String url = ForgeConstants.URL_PIC_SCRYFALL_DOWNLOAD +
|
||||
ImageUtil.getScryfallDownloadUrl(c, backFace, mciCode, langCode);
|
||||
if (isValidScryfallURL(url)) {
|
||||
scryfallSetCodes.put(c.getEdition(), setCode);
|
||||
return url;
|
||||
}
|
||||
// 2. MCI didn't work, so now try all other codes available in edition, alias included.
|
||||
// skipping dups with set, and returning as soon as one will work.
|
||||
Set<String> cardSetCodes = new HashSet<>();
|
||||
// all set-codes should be lower case
|
||||
cardSetCodes.add(mciCode); // add MCI
|
||||
cardSetCodes.add(edition.getCode().toLowerCase());
|
||||
cardSetCodes.add(edition.getCode2().toLowerCase());
|
||||
if (edition.getAlias() != null)
|
||||
cardSetCodes.add(edition.getAlias().toLowerCase());
|
||||
for (String code : cardSetCodes) {
|
||||
if (code.equals(mciCode))
|
||||
continue; // Already checked, SKIP
|
||||
url = ForgeConstants.URL_PIC_SCRYFALL_DOWNLOAD +
|
||||
ImageUtil.getScryfallDownloadUrl(c, backFace, code, langCode);
|
||||
if (isValidScryfallURL(url)) {
|
||||
scryfallSetCodes.put(c.getEdition(), setCode);
|
||||
return url;
|
||||
}
|
||||
}
|
||||
// If we're here, no valid URL has been found. Record this for the future
|
||||
scryfallSetCodes.put(c.getEdition(), INVALID_SCRYFALL_SET_CODE);
|
||||
return null;
|
||||
String setCode = edition.getScryfallCode();
|
||||
return ForgeConstants.URL_PIC_SCRYFALL_DOWNLOAD +
|
||||
ImageUtil.getScryfallDownloadUrl(c, backFace, setCode, langCode);
|
||||
}
|
||||
|
||||
public void fetchImage(final String imageKey, final Callback callback) {
|
||||
@@ -140,11 +87,9 @@ public abstract class ImageFetcher {
|
||||
if (langCodeMap.containsKey(UILang))
|
||||
langCode = langCodeMap.get(UILang);
|
||||
final String scryfallURL = this.getScryfallDownloadURL(paperCard, backFace, langCode);
|
||||
if (scryfallURL == null)
|
||||
return; // Non existing card, or Card's set not found in Scryfall
|
||||
downloadUrls.add(scryfallURL);
|
||||
if (scryfallURL != null)
|
||||
downloadUrls.add(scryfallURL);
|
||||
}
|
||||
|
||||
} else if (prefix.equals(ImageKeys.TOKEN_PREFIX)) {
|
||||
if (tokenImages == null) {
|
||||
tokenImages = new HashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user