* Some fixes to token image downloading.

This commit is contained in:
LevelX2 2018-02-11 23:30:15 +01:00
parent 7e99a027d2
commit c9603d1994
14 changed files with 231 additions and 255 deletions

View file

@ -39,6 +39,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import mage.constants.SubType;
import org.apache.log4j.Logger;
import org.mage.plugins.card.images.CardDownloadData;
import org.mage.plugins.card.images.DownloadPictures;
@ -83,35 +84,6 @@ public enum TokensMtgImageSource implements CardImageSource {
return null;
}
private static final String[] EMBLEMS = {
"Ajani",
"Arlinn",
"Chandra",
"Dack",
"Daretti",
"Dovin",
"Domri",
"Elspeth",
"Garruk",
"Gideon",
"Huatli",
"Jace",
"Kiora",
"Koth",
"Liliana",
"Narset",
"Nixilis",
"Sarkhan",
"Sorin",
"Tamiyo",
"Teferi",
"Venser",
// Custom Emblems
"Yoda",
"Obi-Wan Kenobi",
"Aurra Sing"
};
private static final Map<String, String> SET_NAMES_REPLACEMENT = new HashMap<String, String>() {
{
put("con", "CFX");
@ -119,6 +91,16 @@ public enum TokensMtgImageSource implements CardImageSource {
}
};
private String getEmblemName(String originalName) {
for (SubType subType : SubType.getPlaneswalkerTypes(true)) {
if (originalName.toLowerCase().contains(subType.toString().toLowerCase())) {
return subType.getDescription() + " Emblem";
}
}
return null;
}
@Override
public String generateTokenUrl(CardDownloadData card) throws IOException {
String name = card.getName();
@ -127,12 +109,7 @@ public enum TokensMtgImageSource implements CardImageSource {
// handle emblems
if (name.toLowerCase().contains("emblem")) {
for (String emblem : EMBLEMS) {
if (name.toLowerCase().contains(emblem.toLowerCase())) {
name = emblem + " Emblem";
break;
}
}
name = getEmblemName(name);
}
// we should replace some set names
@ -152,20 +129,16 @@ public enum TokensMtgImageSource implements CardImageSource {
String key = set + "/" + name;
List<TokenData> list = tokensData.get(key);
if (list == null) {
logger.info("Could not find data for token " + name + ", set " + set + ".");
logger.warn("Could not find data for token " + name + ", set " + set + ".");
return null;
}
TokenData tokenData;
if (type == 0) {
if (list.size() > 1) {
logger.info("Multiple images were found for token " + name + ", set " + set + '.');
}
logger.info("Token found: " + name + ", set " + set + '.');
tokenData = list.get(0);
} else {
if (type > list.size()) {
logger.warn("Not enough images for token with type " + type + ", name " + name + ", set " + set + '.');
logger.warn("Not enough images variants for token with type number " + type + ", name " + name + ", set " + set + '.');
return null;
}
tokenData = list.get(card.getType() - 1);
@ -177,6 +150,57 @@ public enum TokensMtgImageSource implements CardImageSource {
return url;
}
@Override
public int getTotalImages() {
return getTokenImages();
}
@Override
public int getTokenImages() {
try {
getTokensData();
} catch (IOException ex) {
logger.error(getSourceName() + ": Loading available data failed. " + ex.getMessage());
}
return tokensData.size();
}
@Override
public boolean isTokenSource() {
return true;
}
@Override
public void doPause(String httpImageUrl) {
}
@Override
public ArrayList<String> getSupportedSets() {
ArrayList<String> supportedSetsCopy = new ArrayList<>();
supportedSetsCopy.addAll(supportedSets);
return supportedSetsCopy;
}
@Override
public boolean isImageProvided(String setCode, String cardName) {
String searchName = cardName;
if (cardName.toLowerCase().contains("emblem")) {
searchName = getEmblemName(cardName);
}
try {
getTokensData();
} catch (IOException ex) {
java.util.logging.Logger.getLogger(TokensMtgImageSource.class.getName()).log(Level.SEVERE, null, ex);
}
String key = setCode + "/" + searchName;
return (tokensData.containsKey(key));
}
@Override
public boolean isSetSupportedComplete(String setCode) {
return false;
}
private HashMap<String, ArrayList<TokenData>> getTokensData() throws IOException {
synchronized (tokensDataSync) {
if (tokensData == null) {
@ -193,7 +217,7 @@ public enum TokensMtgImageSource implements CardImageSource {
list = new ArrayList<>();
tokensData.put(key, list);
supportedSets.add(tokenData.getExpansionSetCode());
logger.info("Added key: " + key);
logger.debug("Added key: " + key);
}
list.add(tokenData);
}
@ -207,6 +231,7 @@ public enum TokensMtgImageSource implements CardImageSource {
try (InputStream inputStream = url.openStream()) {
List<TokenData> siteTokensData = parseTokensData(inputStream);
for (TokenData siteData : siteTokensData) {
// logger.info("TOK: " + siteData.getExpansionSetCode() + "/" + siteData.getName());
String key = siteData.getExpansionSetCode() + "/" + siteData.getName();
supportedSets.add(siteData.getExpansionSetCode());
ArrayList<TokenData> list = tokensData.get(key);
@ -267,7 +292,7 @@ public enum TokensMtgImageSource implements CardImageSource {
}
String[] split = line.split(",");
// replace special comma for cards like 'Ashaya the Awoken World'
String name = split[0].replace('', ',');
String name = split[0].replace('', ',').replace("‚", ",");
String number = split[1];
TokenData token = new TokenData(name, number, set);
newTokensData.add(token);
@ -305,51 +330,4 @@ public enum TokensMtgImageSource implements CardImageSource {
}
}
@Override
public int getTotalImages() {
return getTokenImages();
}
@Override
public int getTokenImages() {
try {
getTokensData();
} catch (IOException ex) {
logger.error(getSourceName() + ": Loading available data failed. " + ex.getMessage());
}
return tokensData.size();
}
@Override
public boolean isTokenSource() {
return true;
}
@Override
public void doPause(String httpImageUrl) {
}
@Override
public ArrayList<String> getSupportedSets() {
ArrayList<String> supportedSetsCopy = new ArrayList<>();
supportedSetsCopy.addAll(supportedSets);
return supportedSetsCopy;
}
@Override
public boolean isImageProvided(String setCode, String cardName) {
try {
getTokensData();
} catch (IOException ex) {
java.util.logging.Logger.getLogger(TokensMtgImageSource.class.getName()).log(Level.SEVERE, null, ex);
}
String key = setCode + "/" + cardName;
return (tokensData.containsKey(key));
}
@Override
public boolean isSetSupportedComplete(String setCode) {
return false;
}
}

View file

@ -352,7 +352,7 @@ public enum WizardCardsImageSource implements CardImageSource {
setsAliases.put("EVG", "Duel Decks: Elves vs. Goblins");
setsAliases.put("EXO", "Exodus");
setsAliases.put("FEM", "Fallen Empires");
setsAliases.put("FNMP", "Friday Night Magic");
// setsAliases.put("FNMP", "Friday Night Magic");
setsAliases.put("FRF", "Fate Reforged");
setsAliases.put("FUT", "Future Sight");
setsAliases.put("GPT", "Guildpact");

View file

@ -2,24 +2,17 @@ package org.mage.plugins.card.images;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import java.nio.file.AccessDeniedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;
import javax.swing.*;
import mage.cards.ExpansionSet;
import mage.cards.Sets;
@ -38,13 +31,11 @@ import org.apache.log4j.Logger;
import org.mage.plugins.card.dl.sources.*;
import org.mage.plugins.card.properties.SettingsManager;
import org.mage.plugins.card.utils.CardImageUtils;
import static org.mage.plugins.card.utils.CardImageUtils.getImagesDir;
public class DownloadPictures extends DefaultBoundedRangeModel implements Runnable {
// don't forget to remove new sets from ignore.urls to download (propeties file in resources)
private static DownloadPictures instance;
private static final Logger logger = Logger.getLogger(DownloadPictures.class);
@ -300,7 +291,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
if (expansionSet != null) {
setNames.add(expansionSet.getName());
} else {
logger.error(cardImageSource.getSourceName() + ": Expansion set for code " + setCode + " not found!");
logger.warn("Source: " + cardImageSource.getSourceName() + ": Expansion set for code " + setCode + " not found in xmage sets!");
}
}
@ -352,7 +343,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
if (cardImageSource.isTokenSource() && cardImageSource.isImageProvided(data.getSet(), data.getName())) {
numberTokenImagesAvailable++;
cardsToDownload.add(data);
}else{
} else {
//logger.warn("Source do not support token (set " + data.getSet() + ", token " + data.getName() + ")");
}
} else {
@ -433,7 +424,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
}
CardInfo secondSideCard = CardRepository.instance.findCard(card.getSecondSideName());
if (secondSideCard == null){
if (secondSideCard == null) {
throw new IllegalStateException("Can''t find second side card in database: " + card.getSecondSideName());
}
@ -473,6 +464,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
logger.debug(card.getName() + " (is_token=" + card.isToken() + "). Image is here:" + file.getAbsolutePath() + " (exists=" + file.exists() + ')');
if (!file.exists()) {
logger.debug("Missing: " + file.getAbsolutePath());
// logger.info("Missing image: " + (card.isToken() ? "TOKEN " : "CARD ") + card.getSet() + "/" + card.getName() + " type: " + card.getType());
cardsToDownload.add(card);
}
});
@ -517,6 +509,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
CardDownloadData card = new CardDownloadData(params[3], set, "0", false, type, "", "", true);
card.setTokenClassName(tokenClassName);
list.add(card);
// logger.debug("Token: " + set + "/" + card.getName() + " type: " + type);
} else if (params[1].toLowerCase().equals("generate") && params[2].startsWith("EMBLEM:")) {
String set = params[2].substring(7);
CardDownloadData card = new CardDownloadData("Emblem " + params[3], set, "0", false, type, "", "", true, fileName);
@ -696,7 +689,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
TFile destFile;
try {
if (card == null){
if (card == null) {
synchronized (sync) {
update(cardIndex + 1, count);
}
@ -705,29 +698,31 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
// gen temp file (download to images folder)
String tempPath = getImagesDir() + File.separator + "downloading" + File.separator;
if(useSpecifiedPaths){
fileTempImage = new TFile(tempPath + actualFilename + "-" + card.hashCode() + ".jpg");
}else{
fileTempImage = new TFile(tempPath + CardImageUtils.prepareCardNameForFile(card.getName()) + "-" + card.hashCode() + ".jpg");
if (useSpecifiedPaths) {
fileTempImage = new TFile(tempPath + actualFilename + "-" + card.hashCode() + ".jpg");
} else {
fileTempImage = new TFile(tempPath + CardImageUtils.prepareCardNameForFile(card.getName()) + "-" + card.hashCode() + ".jpg");
}
if(!fileTempImage.getParentFile().exists()){
fileTempImage.getParentFile().mkdirs();
TFile parentFile = fileTempImage.getParentFile();
if (parentFile != null) {
if (!parentFile.exists()) {
parentFile.mkdirs();
}
}
// gen dest file name
if(useSpecifiedPaths)
{
if(card.isToken()){
if (useSpecifiedPaths) {
if (card.isToken()) {
destFile = new TFile(CardImageUtils.buildImagePathToSet(card) + actualFilename + ".jpg");
}else{
} else {
destFile = new TFile(CardImageUtils.buildImagePathToTokens() + actualFilename + ".jpg");
}
}else{
} else {
destFile = new TFile(CardImageUtils.buildImagePathToCard(card));
}
// FILE already exists (in zip or in dir)
if (destFile.exists()){
if (destFile.exists()) {
synchronized (sync) {
update(cardIndex + 1, count);
}
@ -753,9 +748,9 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
if(!destFile.getParentFile().exists()){
destFile.getParentFile().mkdirs();
}
*/
*/
/*
/*
// WTF start?! TODO: wtf
File existingFile = new File(imagePath.replaceFirst("\\w{3}.zip", ""));
if (existingFile.exists()) {
@ -775,9 +770,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
return;
}
// WTF end?!
*/
*/
// START to download
cardImageSource.doPause(url.getPath());
URLConnection httpConn = url.openConnection(p);
@ -785,7 +778,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
httpConn.connect();
int responseCode = ((HttpURLConnection) httpConn).getResponseCode();
if (responseCode == 200){
if (responseCode == 200) {
// download OK
// save data to temp
BufferedOutputStream out;
@ -803,18 +796,18 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
// stop download, save current state and exit
TFile archive = destFile.getTopLevelArchive();
///* not need to unmout/close - it's auto action
if (archive != null && archive.exists()){
if (archive != null && archive.exists()) {
logger.info("User canceled download. Closing archive file: " + destFile.toString());
try {
TVFS.umount(archive);
}catch (Exception e) {
} catch (Exception e) {
logger.error("Can't close archive file: " + e.getMessage(), e);
}
}//*/
try {
TFile.rm(fileTempImage);
}catch (Exception e) {
} catch (Exception e) {
logger.error("Can't delete temp file: " + e.getMessage(), e);
}
return;
@ -827,25 +820,24 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
out.close();
// TODO: add two faces card correction? (WTF)
// SAVE final data
if (fileTempImage.exists()) {
if (!destFile.getParentFile().exists()){
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
new TFile(fileTempImage).cp_rp(destFile);
try {
TFile.rm(fileTempImage);
}catch (Exception e) {
} catch (Exception e) {
logger.error("Can't delete temp file: " + e.getMessage(), e);
}
}
}else{
} else {
// download ERROR
logger.warn("Image download for " + card.getName()
+ (!card.getDownloadName().equals(card.getName()) ? " downloadname: " + card.getDownloadName() : "")
+ " (" + card.getSet() + ") failed - responseCode: " + responseCode + " url: " + url.toString()
+ (!card.getDownloadName().equals(card.getName()) ? " downloadname: " + card.getDownloadName() : "")
+ " (" + card.getSet() + ") failed - responseCode: " + responseCode + " url: " + url.toString()
);
if (logger.isDebugEnabled()) {
@ -928,8 +920,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
logger.debug("Returned HTML ERROR:\n" + convertStreamToString(((HttpURLConnection) httpConn).getErrorStream()));
}
}
*/
*/
} catch (AccessDeniedException e) {
logger.error("Can't access to files: " + card.getName() + "(" + card.getSet() + "). Try rebooting your system to remove the file lock.");
} catch (Exception e) {
@ -942,25 +933,25 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
}
}
private void writeImageToFile(BufferedImage image, TFile file) throws IOException {
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.96f);
File tempFile = new File(getImagesDir() + File.separator + image.hashCode() + file.getName());
FileImageOutputStream output = new FileImageOutputStream(tempFile);
writer.setOutput(output);
IIOImage image2 = new IIOImage(image, null, null);
writer.write(null, image2, iwp);
writer.dispose();
output.close();
new TFile(tempFile).cp_rp(file);
tempFile.delete();
}
// private void writeImageToFile(BufferedImage image, TFile file) throws IOException {
// Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
//
// ImageWriter writer = (ImageWriter) iter.next();
// ImageWriteParam iwp = writer.getDefaultWriteParam();
// iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// iwp.setCompressionQuality(0.96f);
//
// File tempFile = new File(getImagesDir() + File.separator + image.hashCode() + file.getName());
// FileImageOutputStream output = new FileImageOutputStream(tempFile);
// writer.setOutput(output);
// IIOImage image2 = new IIOImage(image, null, null);
// writer.write(null, image2, iwp);
// writer.dispose();
// output.close();
//
// new TFile(tempFile).cp_rp(file);
// tempFile.delete();
// }
}
private void update(int card, int count) {

View file

@ -56,8 +56,7 @@ public final class CardImageUtils {
*/
public static String generateFullTokenImagePath(CardDownloadData card) {
if (card.isToken()) {
String filePath = getTokenImagePath(card);
return filePath;
return getTokenImagePath(card);
}
return "";
}
@ -67,23 +66,29 @@ public final class CardImageUtils {
TFile file = new TFile(filename);
if (!file.exists()) {
filename = generateTokenDescriptorImagePath(card);
}
file = new TFile(filename);
if (!file.exists()) {
CardDownloadData updated = new CardDownloadData(card);
updated.setName(card.getName() + " 1");
filename = buildImagePathToCard(updated);
file = new TFile(filename);
if (!file.exists()) {
updated = new CardDownloadData(card);
updated.setName(card.getName() + " 2");
filename = buildImagePathToCard(updated);
String tokenDescriptorfilename = generateTokenDescriptorImagePath(card);
if (!tokenDescriptorfilename.isEmpty()) {
file = new TFile(filename);
if (file.exists()) {
return tokenDescriptorfilename;
}
}
}
return filename;
// makes no longer sense
// file = new TFile(filename);
// if (!file.exists()) {
// CardDownloadData updated = new CardDownloadData(card);
// updated.setName(card.getName() + " 1");
// filename = buildImagePathToCard(updated);
// file = new TFile(filename);
// if (!file.exists()) {
// updated = new CardDownloadData(card);
// updated.setName(card.getName() + " 2");
// filename = buildImagePathToCard(updated);
// }
// }
}
private static String searchForCardImage(CardDownloadData card) {
@ -208,7 +213,7 @@ public final class CardImageUtils {
if (card.getUsesVariousArt()) {
finalFileName = cardName + '.' + card.getCollectorId() + ".full.jpg";
} else {
if (card.getUsesVariousArt()){
if (card.getUsesVariousArt()) {
// only various arts can be same name, but different postfixes (a,b,c,d,e)
int len = card.getCollectorId().length();
if (Character.isLetter(card.getCollectorId().charAt(len - 1))) {