mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 04:08:01 +00:00
Merge branch 'automatic_image_downloader' into 'master'
Automatic image downloader See merge request core-developers/forge!107
This commit is contained in:
@@ -17,15 +17,20 @@
|
||||
*/
|
||||
package forge.download;
|
||||
|
||||
import forge.StaticData;
|
||||
import forge.item.PaperCard;
|
||||
import forge.model.FModel;
|
||||
import forge.properties.ForgeConstants;
|
||||
import forge.util.ImageUtil;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
public class GuiDownloadPicturesLQ extends GuiDownloadService {
|
||||
final Map<String, String> downloads = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
Set<String> existingSets;
|
||||
ArrayList<String> existingImages;
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return "Download LQ Card Pictures";
|
||||
@@ -33,17 +38,19 @@ public class GuiDownloadPicturesLQ extends GuiDownloadService {
|
||||
|
||||
@Override
|
||||
protected final Map<String, String> getNeededFiles() {
|
||||
final Map<String, String> downloads = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
|
||||
File f = new File(ForgeConstants.CACHE_CARD_PICS_DIR);
|
||||
existingImages = new ArrayList<String>(Arrays.asList(f.list()));
|
||||
existingSets = retrieveManifestDirectory();
|
||||
|
||||
for (final PaperCard c : FModel.getMagicDb().getCommonCards().getAllCards()) {
|
||||
addDLObject(c, downloads, false);
|
||||
addDLObject(c, false);
|
||||
if (ImageUtil.hasBackFacePicture(c)) {
|
||||
addDLObject(c, downloads, true);
|
||||
addDLObject(c, true);
|
||||
}
|
||||
}
|
||||
|
||||
for (final PaperCard c : FModel.getMagicDb().getVariantCards().getAllCards()) {
|
||||
addDLObject(c, downloads, false);
|
||||
addDLObject(c, false);
|
||||
}
|
||||
|
||||
// Add missing tokens to the list of things to download.
|
||||
@@ -52,16 +59,26 @@ public class GuiDownloadPicturesLQ extends GuiDownloadService {
|
||||
return downloads;
|
||||
}
|
||||
|
||||
private static void addDLObject(final PaperCard c, final Map<String, String> downloads, final boolean backFace) {
|
||||
String[] result = ImageUtil.getDownloadUrlAndDestination(ForgeConstants.CACHE_CARD_PICS_DIR, c, backFace);
|
||||
if (result == null) {
|
||||
private void addDLObject(final PaperCard c, final boolean backFace) {
|
||||
final String imageKey = ImageUtil.getImageKey(c, backFace, false);
|
||||
final String destPath = ForgeConstants.CACHE_CARD_PICS_DIR + imageKey + ".jpg";
|
||||
|
||||
if (existingImages.contains(imageKey + ".jpg")) {
|
||||
return;
|
||||
}
|
||||
final String urlToDownload = result[0];
|
||||
final String destPath = result[1];
|
||||
|
||||
if (downloads.containsKey(destPath)) {
|
||||
return;
|
||||
}
|
||||
downloads.put(destPath, urlToDownload);
|
||||
|
||||
final String setCode3 = c.getEdition();
|
||||
final String setCode2 = StaticData.instance().getEditions().getCode2ByCode(setCode3);
|
||||
|
||||
if (!(existingSets.contains(setCode3) || existingSets.contains(setCode2))) {
|
||||
// If set doesn't exist on server, don't try to download cards for it
|
||||
return;
|
||||
}
|
||||
|
||||
downloads.put(destPath, ForgeConstants.URL_PIC_DOWNLOAD + ImageUtil.getDownloadUrl(c, backFace));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,11 +25,14 @@ import forge.error.BugReporter;
|
||||
import forge.interfaces.IButton;
|
||||
import forge.interfaces.IProgressBar;
|
||||
import forge.interfaces.ITextField;
|
||||
import forge.properties.ForgeConstants;
|
||||
import forge.util.FileUtil;
|
||||
import forge.util.HttpUtil;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
@@ -340,4 +343,27 @@ public abstract class GuiDownloadService implements Runnable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static HashSet<String> retrieveManifestDirectory() {
|
||||
String manifestUrl = ForgeConstants.URL_PIC_DOWNLOAD;
|
||||
HashSet<String> existingSets = new HashSet<>();
|
||||
|
||||
String response = HttpUtil.getURL(manifestUrl);
|
||||
|
||||
if (response == null) return null;
|
||||
|
||||
String[] strings = response.split("<a href=\"");
|
||||
|
||||
for (String s : strings) {
|
||||
int idx = s.indexOf('/');
|
||||
if (!Character.isLetterOrDigit(s.charAt(0)) || idx > 4 || idx == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String set = s.substring(0, idx);
|
||||
existingSets.add(set);
|
||||
}
|
||||
|
||||
return existingSets;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import forge.card.CardEdition;
|
||||
import forge.item.PaperCard;
|
||||
import forge.model.FModel;
|
||||
import forge.properties.ForgeConstants;
|
||||
import forge.util.HttpUtil;
|
||||
import forge.util.ImageUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@@ -87,27 +86,4 @@ public class GuiDownloadSetPicturesLQ extends GuiDownloadService {
|
||||
downloads.put(destFile.getAbsolutePath(), ForgeConstants.URL_PIC_DOWNLOAD + urlPath);
|
||||
}
|
||||
}
|
||||
|
||||
private HashSet<String> retrieveManifestDirectory() {
|
||||
String manifestUrl = ForgeConstants.URL_PIC_DOWNLOAD;
|
||||
HashSet<String> existingSets = new HashSet<>();
|
||||
|
||||
String response = HttpUtil.getURL(manifestUrl);
|
||||
|
||||
if (response == null) return null;
|
||||
|
||||
String[] strings = response.split("<a href=\"");
|
||||
|
||||
for (String s : strings) {
|
||||
int idx = s.indexOf('/');
|
||||
if (!Character.isLetterOrDigit(s.charAt(0)) || idx > 4 || idx == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String set = s.substring(0, idx);
|
||||
existingSets.add(set);
|
||||
}
|
||||
|
||||
return existingSets;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,7 +276,8 @@ public final class ForgeConstants {
|
||||
// URLs
|
||||
private static final String URL_CARDFORGE = "https://downloads.cardforge.org";
|
||||
public static final String URL_PIC_DOWNLOAD = URL_CARDFORGE + "/images/cards/";
|
||||
public static final String URL_PRICE_DOWNLOAD = "https://downloads.cardforge.org/all-prices.txt";
|
||||
public static final String URL_TOKEN_DOWNLOAD = URL_CARDFORGE + "/images/tokens/";
|
||||
public static final String URL_PRICE_DOWNLOAD = URL_CARDFORGE + "/all-prices.txt";
|
||||
|
||||
// Constants for Display Card Identity game setting
|
||||
public static final String DISP_CURRENT_COLORS_ALWAYS = "Always";
|
||||
|
||||
Reference in New Issue
Block a user