Improved Android downloader for token images.

This commit is contained in:
Vittorio Alfieri
2023-06-20 19:14:02 +02:00
parent f9bcaf53c2
commit 1458b4a872

View File

@@ -170,7 +170,7 @@ public class ImgDownloader {
} }
} }
public static String findCardImageUrl(JSONObject jsonObject, String primitiveCardName, String format){ public static String findCardImageUrl(JSONObject jsonObject, String primitiveCardName, String multiverseId, String format){
Map<String, String> imageUris = new HashMap<String, String>(); Map<String, String> imageUris = new HashMap<String, String>();
if (jsonObject.get("image_uris") != null) { if (jsonObject.get("image_uris") != null) {
JSONObject imageUrisObject = (JSONObject) jsonObject.get("image_uris"); JSONObject imageUrisObject = (JSONObject) jsonObject.get("image_uris");
@@ -191,12 +191,12 @@ public class ImgDownloader {
} }
} }
} else { } else {
System.err.println("Cannot retrieve image url for card: " + primitiveCardName); System.err.println("Cannot retrieve image url for card: " + primitiveCardName + "(" + multiverseId + ")");
return ""; return "";
} }
String imageUrl = imageUris.get(format); String imageUrl = imageUris.get(format);
if(imageUrl == null){ if(imageUrl == null){
System.err.println("Cannot retrieve image url for card: " + primitiveCardName); System.err.println("Cannot retrieve image url for card: " + primitiveCardName + "(" + multiverseId + ")");
return ""; return "";
} }
if(imageUrl.indexOf(".jpg") < imageUrl.length()) if(imageUrl.indexOf(".jpg") < imageUrl.length())
@@ -204,7 +204,7 @@ public class ImgDownloader {
return imageUrl; return imageUrl;
} }
public static String findTokenImageUrl(JSONObject jsonObject, String format){ public static String findTokenImageUrl(JSONObject jsonObject, String multiverseId, String format, String filterName){
String imageUrl = ""; String imageUrl = "";
try { try {
Document document = Jsoup.connect((String )jsonObject.get("scryfall_uri")).get(); Document document = Jsoup.connect((String )jsonObject.get("scryfall_uri")).get();
@@ -219,13 +219,11 @@ public class ImgDownloader {
Element aElement = row.selectFirst("td > a"); Element aElement = row.selectFirst("td > a");
String tokenName = aElement.text(); String tokenName = aElement.text();
tokenName = tokenName.substring(0, tokenName.indexOf(" Token,")); tokenName = tokenName.substring(0, tokenName.indexOf(" Token,"));
if(tokenName.equals("Copy")){ if(tokenName.equals(filterName)){
System.out.println("The token " + tokenName + " has been filtered for card: " + (String)jsonObject.get("name")); System.out.println("The token " + tokenName + " has been filtered for card: " + (String)jsonObject.get("name") + " (" + multiverseId + ")");
} else { } else {
imageUrl = aElement.attr("data-card-image-front"); imageUrl = aElement.attr("data-card-image-front");
if(imageUrl == null){ if(imageUrl != null){
System.err.println("Cannot retrieve image url for token: " + tokenName + " created by: " + (String)jsonObject.get("name"));
} else {
howmany++; howmany++;
if(imageUrl.indexOf(".jpg") < imageUrl.length()) if(imageUrl.indexOf(".jpg") < imageUrl.length())
imageUrl = imageUrl.substring(0, imageUrl.indexOf(".jpg")+4); imageUrl = imageUrl.substring(0, imageUrl.indexOf(".jpg")+4);
@@ -234,18 +232,22 @@ public class ImgDownloader {
} }
} }
if (howmany > 1) { if (howmany > 1) {
System.out.println("Found " + howmany + " valid image urls for token created by: " + (String)jsonObject.get("name")); System.out.println("Found " + howmany + " valid image urls for token created by: " + (String)jsonObject.get("name") + " (" + multiverseId + ")");
} }
} }
} }
} catch (IOException e) { } catch (IOException e) {
System.err.println("There was an error while retrieving token image..."); System.err.println("There was an error while retrieving the token image for card: " + (String)jsonObject.get("name") + " (" + multiverseId + ")");
return null; return "";
}
if(imageUrl == null) {
System.err.println("There was an error while retrieving the token image for card: " + (String)jsonObject.get("name") + " (" + multiverseId + ")");
return "";
} }
return imageUrl.replace("large", format); return imageUrl.replace("large", format);
} }
public static String findTokenName(JSONObject jsonObject){ public static String findTokenName(JSONObject jsonObject, String multiverseId, String filterName){
String tokenName = ""; String tokenName = "";
try { try {
Document document = Jsoup.connect((String) jsonObject.get("scryfall_uri")).get(); Document document = Jsoup.connect((String) jsonObject.get("scryfall_uri")).get();
@@ -258,19 +260,26 @@ public class ImgDownloader {
for (Element row : rows) { for (Element row : rows) {
if (row.text().contains(" Token,") && !row.text().contains("Faces,")) { if (row.text().contains(" Token,") && !row.text().contains("Faces,")) {
Element aElement = row.selectFirst("td > a"); Element aElement = row.selectFirst("td > a");
tokenName = aElement.text(); String tok = aElement.text();
tokenName = tokenName.substring(0, tokenName.indexOf(" Token,")); if(tok != null) {
howmany++; tok = tok.substring(0, tok.indexOf(" Token,"));
if (tok.equals(filterName)) {
System.out.println("The token " + tok + " has been filtered for card: " + (String) jsonObject.get("name") + " (" + multiverseId + ")");
} else {
howmany++;
tokenName = tok;
}
}
} }
} }
if (howmany > 1) { if (howmany > 1) {
System.out.println("Found " + howmany + " valid token name created by: " + (String)jsonObject.get("name")); System.out.println("Found " + howmany + " valid token name created by: " + (String)jsonObject.get("name") + " (" + multiverseId + ")");
} }
} }
} }
} catch (IOException e) { } catch (IOException e) {
System.err.println("There was an error while retrieving the token image..."); System.err.println("There was an error while retrieving the token name for card: " + (String)jsonObject.get("name") + " (" + multiverseId + ")");
return null; return "";
} }
return tokenName; return tokenName;
} }
@@ -3975,7 +3984,7 @@ public class ImgDownloader {
String specialcardurl = getSpecialCardUrl(id, set); String specialcardurl = getSpecialCardUrl(id, set);
JSONObject card = findCardJsonById(id); JSONObject card = findCardJsonById(id);
if(specialcardurl.isEmpty() && card != null) if(specialcardurl.isEmpty() && card != null)
specialcardurl = findCardImageUrl(card, mappa.get(id), "large"); specialcardurl = findCardImageUrl(card, mappa.get(id), id, "large");
if (!specialcardurl.isEmpty()) { if (!specialcardurl.isEmpty()) {
URL url = new URL(specialcardurl); URL url = new URL(specialcardurl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
@@ -4082,11 +4091,11 @@ public class ImgDownloader {
} }
if(card != null && hasToken(id)) { if(card != null && hasToken(id)) {
String text = (String) card.get("oracle_text"); String text = (String) card.get("oracle_text");
String nametoken = findTokenName(card); String nametoken = findTokenName(card, id, "Copy");
if (!nametoken.isEmpty() || (text != null && !text.isEmpty() && !text.trim().toLowerCase().contains("nontoken") && ((text.trim().toLowerCase().contains("create") && text.trim().toLowerCase().contains("creature token")) || if (!nametoken.isEmpty() || (text != null && !text.isEmpty() && !text.trim().toLowerCase().contains("nontoken") && ((text.trim().toLowerCase().contains("create") && text.trim().toLowerCase().contains("creature token")) ||
(text.trim().toLowerCase().contains("put") && text.trim().toLowerCase().contains("token"))))) { (text.trim().toLowerCase().contains("put") && text.trim().toLowerCase().contains("token"))))) {
System.out.println("The card: " + mappa.get(id) + " (" + id + ".jpg) can create a token, i will try to download that image too as " + id + "t.jpg"); System.out.println("The card: " + mappa.get(id) + " (" + id + ".jpg) can create a token, i will try to download that image too as " + id + "t.jpg");
String specialtokenurl = findTokenImageUrl(card, "large"); String specialtokenurl = findTokenImageUrl(card, id, "large", "Copy");
if (!specialtokenurl.isEmpty()) { if (!specialtokenurl.isEmpty()) {
URL urltoken = null; URL urltoken = null;
urltoken = new URL(specialtokenurl); urltoken = new URL(specialtokenurl);
@@ -4897,9 +4906,9 @@ public class ImgDownloader {
if(specialtokenurl.isEmpty()) if(specialtokenurl.isEmpty())
specialtokenurl = getSpecialCardUrl(id + "t", set); specialtokenurl = getSpecialCardUrl(id + "t", set);
if(specialtokenurl.isEmpty() && card != null) if(specialtokenurl.isEmpty() && card != null)
specialtokenurl = findTokenImageUrl(card, "large"); specialtokenurl = findTokenImageUrl(card, id, "large", "Copy");
if(nametoken.isEmpty() && card != null) if(nametoken.isEmpty() && card != null)
nametoken = findTokenName(card); nametoken = findTokenName(card, id, "Copy");
if (!specialtokenurl.isEmpty()) { if (!specialtokenurl.isEmpty()) {
try { try {
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get(); doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();