This commit is contained in:
magenoxx 2011-06-12 17:06:48 +04:00
commit cedf0c9b91
10 changed files with 309 additions and 272 deletions

View file

@ -1,55 +0,0 @@
package org.mage.plugins.card;
import java.io.Serializable;
/**
* Contains card data and image url.
*
* @author nantuko
*/
public class CardUrl implements Serializable {
public String name;
public String set;
public boolean token = false;
public Integer collector;
public String url = "";
public boolean existsInTheGame = false;
public CardUrl(String cardName, String cardSet, Integer collectorId, boolean isToken) {
name = cardName;
set = cardSet;
collector = collectorId;
token = isToken;
}
@Override
public boolean equals(Object other) {
if (other == null) { return false; }
if (other instanceof CardUrl) {
return name.equals(((CardUrl) other).name) && set.equals(((CardUrl) other).set)
&& collector.equals(((CardUrl) other).collector) && token==((CardUrl)other).token;
}
return false;
}
@Override
public int hashCode() {
int hash = 0;
hash = name.hashCode();
hash = 31*hash + set.hashCode();
hash = 31*hash + collector.hashCode();
hash = 31*hash + (token ? 1 : 0);
return hash;
}
public boolean isExistsInTheGame() {
return existsInTheGame;
}
public void setExistsInTheGame(boolean existsInTheGame) {
this.existsInTheGame = existsInTheGame;
}
private static final long serialVersionUID = 2L;
}

View file

@ -6,5 +6,7 @@ package org.mage.plugins.card.dl.sources;
*/ */
public interface CardImageSource { public interface CardImageSource {
public String generateURL(Integer collectorId, String cardSet) throws Exception; String generateURL(Integer collectorId, String cardSet) throws Exception;
String generateTokenUrl(String name, String set);
Float getAverageSize();
} }

View file

@ -1,5 +1,7 @@
package org.mage.plugins.card.dl.sources; package org.mage.plugins.card.dl.sources;
import java.util.HashMap;
import java.util.Map;
import org.mage.plugins.card.utils.CardImageUtils; import org.mage.plugins.card.utils.CardImageUtils;
/** /**
@ -9,6 +11,35 @@ import org.mage.plugins.card.utils.CardImageUtils;
public class MagicCardsImageSource implements CardImageSource { public class MagicCardsImageSource implements CardImageSource {
private static CardImageSource instance = new MagicCardsImageSource(); private static CardImageSource instance = new MagicCardsImageSource();
private static final Map<String, String> setNameReplacement = new HashMap<String, String>() {
{
put("NPH", "new-phyrexia");
put("MBS", "mirrodin-besieged");
put("SOM", "scars-of-mirrodin");
put("M11", "magic-2011");
put("ROE", "rise-of-the-eldrazi");
put("PVC", "duel-decks-phyrexia-vs-the-coalition");
put("WWK", "worldwake");
put("ZEN", "zendikar");
put("HOP", "planechase");
put("M10", "magic-2010");
put("GVL", "duel-decks-garruk-vs-liliana");
put("ARB", "alara-reborn");
put("DVD", "duel-decks-divine-vs-demonic");
put("CON", "conflux");
put("JVC", "duel-decks-jace-vs-chandra");
put("ALA", "shards-of-alara");
put("EVE", "eventide");
put("SHM", "shadowmoor");
put("EVG", "duel-decks-elves-vs-goblins");
put("MOR", "morningtide");
put("LRW", "lorwyn");
put("10E", "tenth-edition");
put("CSP", "coldsnap");
}
private static final long serialVersionUID = 1L;
};
public static CardImageSource getInstance() { public static CardImageSource getInstance() {
if (instance == null) { if (instance == null) {
@ -28,4 +59,23 @@ public class MagicCardsImageSource implements CardImageSource {
return url; return url;
} }
@Override
public String generateTokenUrl(String name, String set) {
String _name = name.replaceAll(" ", "-").toLowerCase();
String _set = "not-supported-set";
if (setNameReplacement.containsKey(set)) {
_set = setNameReplacement.get(set);
} else {
_set += "-" + set;
}
String url = "http://magiccards.info/extras/token/" + _set + "/" + _name + ".jpg";
return url;
}
@Override
public Float getAverageSize() {
return 70.0f;
}
} }

View file

@ -21,7 +21,6 @@ public class MtgatheringRuImageSource implements CardImageSource {
static { static {
setsAliases = new HashMap(); setsAliases = new HashMap();
setsAliases.put("MBS", "mirrodinbesieged");
setsAliases.put("M11", "magic2011"); setsAliases.put("M11", "magic2011");
} }
private String quality; private String quality;
@ -56,17 +55,28 @@ public class MtgatheringRuImageSource implements CardImageSource {
if (collectorId == null || cardSet == null) { if (collectorId == null || cardSet == null) {
throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet); throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet);
} }
if (setsAliases.get(cardSet) == null) { if (setsAliases.get(cardSet) != null) {
String set = CardImageUtils.updateSet(cardSet, true);
String url = "http://magiccards.info/scans/en/";
url += set.toLowerCase() + "/" + collectorId + ".jpg";
return url;
} else {
String set = CardImageUtils.updateSet(cardSet, true); String set = CardImageUtils.updateSet(cardSet, true);
String url = "http://mtgathering.ru/scans/en/"; String url = "http://mtgathering.ru/scans/en/";
url += set.toLowerCase() + "/" + quality + "/" + collectorId + ".jpg"; url += set.toLowerCase() + "/" + quality + "/" + collectorId + ".jpg";
return url; return url;
} }
return null;
}
@Override
public String generateTokenUrl(String name, String set) {
return null;
}
@Override
public Float getAverageSize() {
if(quality.equalsIgnoreCase("hq"))
return 80.0f;
if(quality.equalsIgnoreCase("md"))
return 30.0f;
if(quality.equalsIgnoreCase("lq"))
return 9.0f;
return 0f;
} }
} }

View file

@ -8,7 +8,6 @@ import java.util.Map;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import org.mage.plugins.card.utils.CardImageUtils;
/** /**
* *
@ -52,7 +51,7 @@ public class WizardCardsImageSource implements CardImageSource {
setLinks.add(cardsImages.get(i).attr("src")); setLinks.add(cardsImages.get(i).attr("src"));
} }
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); System.out.println("Exception when parsing the wizards page: " + ex.getMessage());
} }
return setLinks; return setLinks;
} }
@ -62,13 +61,7 @@ public class WizardCardsImageSource implements CardImageSource {
if (collectorId == null || cardSet == null) { if (collectorId == null || cardSet == null) {
throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet); throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet);
} }
if (setsAliases.get(cardSet) == null) { if (setsAliases.get(cardSet) != null) {
String set = CardImageUtils.updateSet(cardSet, true);
String url = "http://magiccards.info/scans/en/";
url += set.toLowerCase() + "/" + collectorId + ".jpg";
return url;
} else {
List<String> setLinks = (List<String>) sets.get(cardSet); List<String> setLinks = (List<String>) sets.get(cardSet);
if (setLinks == null) { if (setLinks == null) {
setLinks = getSetLinks((String) setsAliases.get(cardSet)); setLinks = getSetLinks((String) setsAliases.get(cardSet));
@ -86,5 +79,16 @@ public class WizardCardsImageSource implements CardImageSource {
} }
return link; return link;
} }
return null;
}
@Override
public String generateTokenUrl(String name, String set) {
return null;
}
@Override
public Float getAverageSize() {
return 60.0f;
} }
} }

View file

@ -1,13 +1,100 @@
package org.mage.plugins.card.images; package org.mage.plugins.card.images;
/**
*
* @author North
*/
public class CardInfo { public class CardInfo {
public String name;
public String set; private String name;
public Integer collectorId; private String set;
public boolean isToken = false; private Integer collectorId;
private boolean token;
public CardInfo(String name, String set, Integer collectorId) { public CardInfo(String name, String set, Integer collectorId) {
this.name = name; this.name = name;
this.set = set; this.set = set;
this.collectorId = collectorId; this.collectorId = collectorId;
token = false;
}
public CardInfo(String name, String set, Integer collectorId, boolean token) {
this.name = name;
this.set = set;
this.collectorId = collectorId;
this.token = token;
}
public CardInfo(final CardInfo card) {
this.name = card.name;
this.set = card.set;
this.collectorId = card.collectorId;
this.token = card.token;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CardInfo other = (CardInfo) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if ((this.set == null) ? (other.set != null) : !this.set.equals(other.set)) {
return false;
}
if (this.collectorId != other.collectorId && (this.collectorId == null || !this.collectorId.equals(other.collectorId))) {
return false;
}
if (this.token != other.token) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 5;
hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 47 * hash + (this.set != null ? this.set.hashCode() : 0);
hash = 47 * hash + (this.collectorId != null ? this.collectorId.hashCode() : 0);
hash = 47 * hash + (this.token ? 1 : 0);
return hash;
}
public Integer getCollectorId() {
return collectorId;
}
public void setCollectorId(Integer collectorId) {
this.collectorId = collectorId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
public boolean isToken() {
return token;
}
public void setToken(boolean token) {
this.token = token;
} }
} }

View file

@ -15,9 +15,8 @@ import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -46,7 +45,6 @@ import javax.swing.event.ChangeListener;
import mage.cards.Card; import mage.cards.Card;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.mage.plugins.card.CardUrl;
import org.mage.plugins.card.constants.Constants; import org.mage.plugins.card.constants.Constants;
import org.mage.plugins.card.dl.sources.CardImageSource; import org.mage.plugins.card.dl.sources.CardImageSource;
import org.mage.plugins.card.dl.sources.MagicCardsImageSource; import org.mage.plugins.card.dl.sources.MagicCardsImageSource;
@ -62,10 +60,10 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
private JProgressBar bar; private JProgressBar bar;
private JOptionPane dlg; private JOptionPane dlg;
private boolean cancel; private boolean cancel;
private JButton close; private JButton closeButton;
private JButton startDownloadButton;
private int cardIndex; private int cardIndex;
private ArrayList<CardUrl> cards; private ArrayList<CardInfo> cards;
private ArrayList<CardUrl> cardsInGame;
private JComboBox jComboBox1; private JComboBox jComboBox1;
private JLabel jLabel1; private JLabel jLabel1;
private static boolean offlineMode = false; private static boolean offlineMode = false;
@ -85,7 +83,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} }
public static void startDownload(JFrame frame, Set<Card> allCards) { public static void startDownload(JFrame frame, Set<Card> allCards) {
ArrayList<CardUrl> cards = getNeededCards(allCards); ArrayList<CardInfo> cards = getNeededCards(allCards);
/* /*
* if (cards == null || cards.size() == 0) { * if (cards == null || cards.size() == 0) {
@ -104,7 +102,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
String title = "Downloading"; String title = "Downloading";
final JDialog dialog = this.dlg.createDialog(frame, title); final JDialog dialog = this.dlg.createDialog(frame, title);
close.addActionListener(new ActionListener() { closeButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
dialog.setVisible(false); dialog.setVisible(false);
@ -117,15 +115,9 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
this.cancel = cancel; this.cancel = cancel;
} }
public DownloadPictures(ArrayList<CardUrl> cards) { public DownloadPictures(ArrayList<CardInfo> cards) {
this.cards = cards; this.cards = cards;
this.cardsInGame = new ArrayList<CardUrl>();
for (CardUrl url : cards) {
if (url.isExistsInTheGame())
cardsInGame.add(url);
}
addr = new JTextField("Proxy Address"); addr = new JTextField("Proxy Address");
port = new JTextField("Proxy Port"); port = new JTextField("Proxy Port");
bar = new JProgressBar(this); bar = new JProgressBar(this);
@ -185,18 +177,22 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
cardImageSource = MtgatheringRuImageSource.getLqInstance(); cardImageSource = MtgatheringRuImageSource.getLqInstance();
break; break;
} }
int count = DownloadPictures.this.cards.size();
float mb = (count * cardImageSource.getAverageSize()) / 1024;
bar.setString(String.format(cardIndex == count ? "%d of %d cards finished! Please close!"
: "%d of %d cards finished! Please wait! [%.1f Mb]", 0, count, mb));
} }
}); });
p0.add(jComboBox1); p0.add(jComboBox1);
p0.add(Box.createVerticalStrut(5)); p0.add(Box.createVerticalStrut(5));
// Start // Start
final JButton b = new JButton("Start download"); startDownloadButton = new JButton("Start download");
b.addActionListener(new ActionListener() { startDownloadButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
new Thread(DownloadPictures.this).start(); new Thread(DownloadPictures.this).start();
b.setEnabled(false); startDownloadButton.setEnabled(false);
checkBox.setEnabled(false); checkBox.setEnabled(false);
} }
}); });
@ -206,7 +202,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
p0.add(bar); p0.add(bar);
bar.setStringPainted(true); bar.setStringPainted(true);
int count = cards.size(); int count = cards.size();
float mb = (count * 70.0f) / 1024; float mb = (count * cardImageSource.getAverageSize()) / 1024;
bar.setString(String.format(cardIndex == cards.size() ? "%d of %d cards finished! Please close!" bar.setString(String.format(cardIndex == cards.size() ? "%d of %d cards finished! Please close!"
: "%d of %d cards finished! Please wait! [%.1f Mb]", 0, cards.size(), mb)); : "%d of %d cards finished! Please wait! [%.1f Mb]", 0, cards.size(), mb));
Dimension d = bar.getPreferredSize(); Dimension d = bar.getPreferredSize();
@ -222,40 +218,33 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
checkBox.addActionListener(new ActionListener() { checkBox.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (checkBox.isSelected()) {
int count = DownloadPictures.this.cardsInGame.size();
float mb = (count * 70.0f) / 1024;
bar.setString(String.format(count == 0 ? "No images to download!" : "%d of %d cards finished! Please wait! [%.1f Mb]",
0, DownloadPictures.this.cardsInGame.size(), mb));
} else {
int count = DownloadPictures.this.cards.size(); int count = DownloadPictures.this.cards.size();
float mb = (count * 70.0f) / 1024; float mb = (count * cardImageSource.getAverageSize()) / 1024;
bar.setString(String.format(cardIndex == count ? "%d of %d cards finished! Please close!" bar.setString(String.format(cardIndex == count ? "%d of %d cards finished! Please close!"
: "%d of %d cards finished! Please wait! [%.1f Mb]", 0, count, mb)); : "%d of %d cards finished! Please wait! [%.1f Mb]", 0, count, mb));
} }
}
}); });
// JOptionPane // JOptionPane
Object[] options = { b, close = new JButton("Cancel") }; Object[] options = { startDownloadButton, closeButton = new JButton("Cancel") };
dlg = new JOptionPane(p0, JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]); dlg = new JOptionPane(p0, JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
} }
private static ArrayList<CardUrl> getNeededCards(Set<Card> allCards) { private static ArrayList<CardInfo> getNeededCards(Set<Card> allCards) {
ArrayList<CardUrl> cardsToDownload = new ArrayList<CardUrl>(); ArrayList<CardInfo> cardsToDownload = new ArrayList<CardInfo>();
/** /**
* read all card names and urls * read all card names and urls
*/ */
ArrayList<CardUrl> allCardsUrls = new ArrayList<CardUrl>(); ArrayList<CardInfo> allCardsUrls = new ArrayList<CardInfo>();
try { try {
offlineMode = true; offlineMode = true;
for (Card card : allCards) { for (Card card : allCards) {
if (card.getCardNumber() > 0 && !card.getExpansionSetCode().isEmpty()) { if (card.getCardNumber() > 0 && !card.getExpansionSetCode().isEmpty()) {
CardUrl url = new CardUrl(card.getName(), card.getExpansionSetCode(), card.getCardNumber(), false); CardInfo url = new CardInfo(card.getName(), card.getExpansionSetCode(), card.getCardNumber(), false);
allCardsUrls.add(url); allCardsUrls.add(url);
} else { } else {
if (card.getCardNumber() < 1) { if (card.getCardNumber() < 1) {
@ -278,10 +267,10 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
/** /**
* check to see which cards we already have * check to see which cards we already have
*/ */
for (CardUrl card : allCardsUrls) { for (CardInfo card : allCardsUrls) {
boolean withCollectorId = false; boolean withCollectorId = false;
if (card.name.equals("Forest") || card.name.equals("Mountain") || card.name.equals("Swamp") || card.name.equals("Island") if (card.getName().equals("Forest") || card.getName().equals("Mountain") || card.getName().equals("Swamp") || card.getName().equals("Island")
|| card.name.equals("Plains")) { || card.getName().equals("Plains")) {
withCollectorId = true; withCollectorId = true;
} }
file = new File(CardImageUtils.getImagePath(card, withCollectorId)); file = new File(CardImageUtils.getImagePath(card, withCollectorId));
@ -290,13 +279,12 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} }
} }
for (CardUrl card : cardsToDownload) { for (CardInfo card : cardsToDownload) {
if (card.token) { if (card.isToken()) {
log.info("Card to download: " + card.name + " (Token) " + card.url); log.info("Card to download: " + card.getName() + " (Token) ");
} else { } else {
try { try {
log.info("Card to download: " + card.name + " (" + card.set + ") " log.info("Card to download: " + card.getName() + " (" + card.getSet() + ")");
+ cardImageSource.generateURL(card.collector, card.set));
} catch (Exception e) { } catch (Exception e) {
log.error(e); log.error(e);
} }
@ -306,15 +294,15 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
return cardsToDownload; return cardsToDownload;
} }
private static ArrayList<CardUrl> getTokenCardUrls() throws RuntimeException { private static ArrayList<CardInfo> getTokenCardUrls() throws RuntimeException {
ArrayList<CardUrl> list = new ArrayList<CardUrl>(); ArrayList<CardInfo> list = new ArrayList<CardInfo>();
HashSet<String> filter = new HashSet<String>(); HashSet<String> filter = new HashSet<String>();
InputStream in = DownloadPictures.class.getClassLoader().getResourceAsStream("card-pictures-tok.txt"); InputStream in = DownloadPictures.class.getClassLoader().getResourceAsStream("card-pictures-tok.txt");
readImageURLsFromFile(in, list, filter); readImageURLsFromFile(in, list, filter);
return list; return list;
} }
private static void readImageURLsFromFile(InputStream in, ArrayList<CardUrl> list, Set<String> filter) throws RuntimeException { private static void readImageURLsFromFile(InputStream in, ArrayList<CardInfo> list, Set<String> filter) throws RuntimeException {
if (in == null) { if (in == null) {
log.error("resources input stream is null"); log.error("resources input stream is null");
return; return;
@ -335,18 +323,8 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
if (params.length >= 4) { if (params.length >= 4) {
if (params[1].toLowerCase().equals("generate") && params[2].startsWith("TOK:")) { if (params[1].toLowerCase().equals("generate") && params[2].startsWith("TOK:")) {
String set = params[2].substring(4); String set = params[2].substring(4);
CardUrl cardUrl = new CardUrl(params[3], set, 0, true); CardInfo card = new CardInfo(params[3], set, 0, true);
cardUrl.token = true; list.add(card);
cardUrl.url = generateTokenUrl(params[3], set);
list.add(cardUrl);
} else {
CardUrl cardUrl = new CardUrl(params[2], params[1].toUpperCase(), 0, false);
cardUrl.url = params[3];
if (cardUrl.set.startsWith("TOK:")) {
cardUrl.token = true;
cardUrl.set = cardUrl.set.substring(4);
}
list.add(cardUrl);
} }
} else { } else {
log.error("wrong format for image urls: " + line); log.error("wrong format for image urls: " + line);
@ -377,47 +355,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} }
} }
final static Map<String, String> setNameReplacement = new HashMap<String, String>() {
{
put("NPH", "new-phyrexia");
put("MBS", "mirrodin-besieged");
put("SOM", "scars-of-mirrodin");
put("M11", "magic-2011");
put("ROE", "rise-of-the-eldrazi");
put("PVC", "duel-decks-phyrexia-vs-the-coalition");
put("WWK", "worldwake");
put("ZEN", "zendikar");
put("HOP", "planechase");
put("M10", "magic-2010");
put("GVL", "duel-decks-garruk-vs-liliana");
put("ARB", "alara-reborn");
put("DVD", "duel-decks-divine-vs-demonic");
put("CON", "conflux");
put("JVC", "duel-decks-jace-vs-chandra");
put("ALA", "shards-of-alara");
put("EVE", "eventide");
put("SHM", "shadowmoor");
put("EVG", "duel-decks-elves-vs-goblins");
put("MOR", "morningtide");
put("LRW", "lorwyn");
put("10E", "tenth-edition");
put("CSP", "coldsnap");
}
private static final long serialVersionUID = 1L;
};
private static String generateTokenUrl(String name, String set) {
String _name = name.replaceAll(" ", "-").toLowerCase();
String _set = "not-supported-set";
if (setNameReplacement.containsKey(set)) {
_set = setNameReplacement.get(set);
} else {
_set += "-" + set;
}
String url = "http://magiccards.info/extras/token/" + _set + "/" + _name + ".jpg";
return url;
}
private class ProxyHandler implements ChangeListener { private class ProxyHandler implements ChangeListener {
private int type; private int type;
@ -437,8 +374,6 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
@Override @Override
public void run() { public void run() {
BufferedInputStream in;
BufferedOutputStream out;
File base = new File(Constants.IO.imageBaseDir); File base = new File(Constants.IO.imageBaseDir);
if (!base.exists()) { if (!base.exists()) {
@ -458,23 +393,31 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
HashSet<String> ignoreUrls = SettingsManager.getIntance().getIgnoreUrls(); HashSet<String> ignoreUrls = SettingsManager.getIntance().getIgnoreUrls();
update(0); update(0);
for (int i = 0; (checkBox.isSelected() ? i < cardsInGame.size() : i < cards.size()) && !cancel; i++) { for (int i = 0; i < cards.size() && !cancel; i++) {
try { try {
CardUrl card = checkBox.isSelected() ? cardsInGame.get(i) : cards.get(i); CardInfo card = cards.get(i);
log.info("Downloading card: " + card.name + " (" + card.set + ")"); log.info("Downloading card: " + card.getName() + " (" + card.getSet() + ")");
URL url = new URL(cardImageSource.generateURL(card.collector, card.set)); String url;
if (ignoreUrls.contains(card.set) || card.token) { if (ignoreUrls.contains(card.getSet()) || card.isToken()) {
if (card.collector != 0) { if (card.getCollectorId() != 0) {
continue; continue;
} }
url = new URL(card.url); url = cardImageSource.generateTokenUrl(card.getName(), card.getSet());
} else {
url = cardImageSource.generateURL(card.getCollectorId(), card.getSet());
} }
Runnable task = new DownloadTask(card, url); if (url != null) {
Runnable task = new DownloadTask(card, new URL(url));
executor.execute(task); executor.execute(task);
} else {
synchronized (sync) {
update(cardIndex + 1);
}
}
} catch (Exception ex) { } catch (Exception ex) {
log.error(ex, ex); log.error(ex, ex);
} }
@ -486,14 +429,14 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} catch (InterruptedException ie) {} } catch (InterruptedException ie) {}
} }
} }
close.setText("Close"); closeButton.setText("Close");
} }
private final class DownloadTask implements Runnable { private final class DownloadTask implements Runnable {
private CardUrl card; private CardInfo card;
private URL url; private URL url;
public DownloadTask(CardUrl card, URL url) { public DownloadTask(CardInfo card, URL url) {
this.card = card; this.card = card;
this.url = url; this.url = url;
} }
@ -506,8 +449,8 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
createDirForCard(card); createDirForCard(card);
boolean withCollectorId = false; boolean withCollectorId = false;
if (card.name.equals("Forest") || card.name.equals("Mountain") || card.name.equals("Swamp") if (card.getName().equals("Forest") || card.getName().equals("Mountain") || card.getName().equals("Swamp")
|| card.name.equals("Island") || card.name.equals("Plains")) { || card.getName().equals("Island") || card.getName().equals("Plains")) {
withCollectorId = true; withCollectorId = true;
} }
File fileOut = new File(CardImageUtils.getImagePath(card, withCollectorId)); File fileOut = new File(CardImageUtils.getImagePath(card, withCollectorId));
@ -542,7 +485,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} }
} }
private static File createDirForCard(CardUrl card) throws Exception { private static File createDirForCard(CardInfo card) throws Exception {
File setDir = new File(CardImageUtils.getImageDir(card)); File setDir = new File(CardImageUtils.getImageDir(card));
if (!setDir.exists()) { if (!setDir.exists()) {
setDir.mkdirs(); setDir.mkdirs();
@ -552,20 +495,35 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
private void update(int card) { private void update(int card) {
this.cardIndex = card; this.cardIndex = card;
if (checkBox.isSelected()) { int count = DownloadPictures.this.cards.size();
int count = DownloadPictures.this.cardsInGame.size();
int countLeft = count - card; if (cardIndex < count) {
float mb = (countLeft * 70.0f) / 1024; float mb = ((count - card) * cardImageSource.getAverageSize()) / 1024;
bar.setString(String.format(card == count ? "%d of %d cards finished! Please close!" bar.setString(String.format("%d of %d cards finished! Please wait! [%.1f Mb]",
: "%d of %d cards finished! Please wait! [%.1f Mb]",
card, count, mb)); card, count, mb));
} else { } else {
int count = DownloadPictures.this.cards.size(); Iterator<CardInfo> cardsIterator = DownloadPictures.this.cards.iterator();
int countLeft = count - card; while (cardsIterator.hasNext()) {
float mb = (countLeft * 70.0f) / 1024; CardInfo cardInfo = cardsIterator.next();
bar.setString(String.format(cardIndex == count ? "%d of %d cards finished! Please close!" boolean withCollectorId = false;
: "%d of %d cards finished! Please wait! [%.1f Mb]", if (cardInfo.getName().equals("Forest") || cardInfo.getName().equals("Mountain") || cardInfo.getName().equals("Swamp") || cardInfo.getName().equals("Island")
card, count, mb)); || cardInfo.getName().equals("Plains")) {
withCollectorId = true;
}
File file = new File(CardImageUtils.getImagePath(cardInfo, withCollectorId));
if (file.exists()) {
cardsIterator.remove();
}
}
count = DownloadPictures.this.cards.size();
if (count == 0) {
bar.setString(String.format("0 cards remaining! Please close!", count));
} else {
bar.setString(String.format("%d cards remaining! Please choose another source!", count));
executor = Executors.newFixedThreadPool(10);
startDownloadButton.setEnabled(true);
}
} }
} }

View file

@ -50,6 +50,7 @@ public class ImageCache {
static { static {
imageCache = new MapMaker().softValues().makeComputingMap(new Function<String, BufferedImage>() { imageCache = new MapMaker().softValues().makeComputingMap(new Function<String, BufferedImage>() {
@Override
public BufferedImage apply(String key) { public BufferedImage apply(String key) {
try { try {
boolean thumbnail = false; boolean thumbnail = false;
@ -66,7 +67,7 @@ public class ImageCache {
CardInfo info = new CardInfo(name, set, collectorId); CardInfo info = new CardInfo(name, set, collectorId);
if (collectorId == 0) info.isToken = true; if (collectorId == 0) info.setToken(true);
String path = CardImageUtils.getImagePath(info); String path = CardImageUtils.getImagePath(info);
if (path == null) return null; if (path == null) return null;
File file = new File(path); File file = new File(path);

View file

@ -3,6 +3,7 @@ package org.mage.plugins.card.properties;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Properties; import java.util.Properties;
@ -55,9 +56,7 @@ public class SettingsManager {
String result = imageUrlProperties.getProperty("ignore.urls"); String result = imageUrlProperties.getProperty("ignore.urls");
if (result != null) { if (result != null) {
String[] ignore = result.split(","); String[] ignore = result.split(",");
for (String i : ignore) { ignoreUrls.addAll(Arrays.asList(ignore));
ignoreUrls.add(i);
}
} }
} }
return ignoreUrls; return ignoreUrls;
@ -69,9 +68,7 @@ public class SettingsManager {
String result = imageUrlProperties.getProperty("token.lookup.order"); String result = imageUrlProperties.getProperty("token.lookup.order");
if (result != null) { if (result != null) {
String[] sets = result.split(","); String[] sets = result.split(",");
for (String s : sets) { order.addAll(Arrays.asList(sets));
order.add(s);
}
} }
} }
return order; return order;

View file

@ -3,35 +3,27 @@ package org.mage.plugins.card.utils;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import mage.cards.Card;
import mage.game.permanent.PermanentToken;
import org.mage.plugins.card.CardUrl;
import org.mage.plugins.card.constants.Constants; import org.mage.plugins.card.constants.Constants;
import org.mage.plugins.card.images.CardInfo; import org.mage.plugins.card.images.CardInfo;
import org.mage.plugins.card.properties.SettingsManager; import org.mage.plugins.card.properties.SettingsManager;
public class CardImageUtils { public class CardImageUtils {
private static HashMap<CardUrl, String> pathCache = new HashMap<CardUrl, String>(); private static HashMap<CardInfo, String> pathCache = new HashMap<CardInfo, String>();
/** /**
* Get path to image for specific card. * Get path to image for specific card.
* *
* @param c * @param card
* card to get path for * card to get path for
* @return String if image exists, else null * @return String if image exists, else null
*/ */
public static String getImagePath(CardInfo c) { public static String getImagePath(CardInfo card) {
String filePath; String filePath;
String suffix = ".jpg"; String suffix = ".jpg";
String cardname = c.name;
String set = c.set;
CardUrl card = new CardUrl(cardname, set, c.collectorId, c.isToken);
File file = null; File file = null;
if (c.isToken) { if (card.isToken()) {
if (pathCache.containsKey(card)) { if (pathCache.containsKey(card)) {
return pathCache.get(card); return pathCache.get(card);
} }
@ -60,7 +52,7 @@ public class CardImageUtils {
* try current directory * try current directory
*/ */
if (file == null || !file.exists()) { if (file == null || !file.exists()) {
filePath = cleanString(c.name) + suffix; filePath = cleanString(card.getName()) + suffix;
file = new File(filePath); file = new File(filePath);
} }
@ -71,22 +63,17 @@ public class CardImageUtils {
} }
} }
private static boolean isToken(Card c) { private static String getTokenImagePath(CardInfo card) {
return c != null && c instanceof PermanentToken;
}
private static String getTokenImagePath(CardUrl card) {
String filename = getImagePath(card, false); String filename = getImagePath(card, false);
CardUrl c = new CardUrl(card.name, card.set, 0, card.token);
File file = new File(filename); File file = new File(filename);
if (!file.exists()) { if (!file.exists()) {
c.name = card.name + " 1"; card.setName(card.getName() + " 1");
filename = getImagePath(c, false); filename = getImagePath(card, false);
file = new File(filename); file = new File(filename);
if (!file.exists()) { if (!file.exists()) {
c.name = card.name + " 2"; card.setName(card.getName() + " 2");
filename = getImagePath(c, false); filename = getImagePath(card, false);
file = new File(filename); file = new File(filename);
} }
} }
@ -94,15 +81,13 @@ public class CardImageUtils {
return filename; return filename;
} }
private static String searchForCardImage(CardUrl card) { private static String searchForCardImage(CardInfo card) {
File file = null; File file = null;
String path = ""; String path = "";
CardUrl c = new CardUrl(card.name, card.set, 0, card.token); CardInfo c = new CardInfo(card);
boolean found = false; // search only in older sets
for (String set : SettingsManager.getIntance().getTokenLookupOrder()) { for (String set : SettingsManager.getIntance().getTokenLookupOrder()) {
if (found || card.set.isEmpty()) { // start looking for image only if we have found card.set in the list (as this list is ordered) c.setSet(set);
c.set = set;
path = getTokenImagePath(c); path = getTokenImagePath(c);
file = new File(path); file = new File(path);
if (file.exists()) { if (file.exists()) {
@ -110,8 +95,6 @@ public class CardImageUtils {
return path; return path;
} }
} }
//if (set.equals(card.set)) found = true;
}
return ""; return "";
} }
@ -142,23 +125,23 @@ public class CardImageUtils {
return set; return set;
} }
public static String getImageDir(CardUrl card) { public static String getImageDir(CardInfo card) {
if (card.set == null) { if (card.getSet() == null) {
return ""; return "";
} }
String set = updateSet(card.set,false).toUpperCase(); String set = updateSet(card.getSet(), false).toUpperCase();
if (card.token) { if (card.isToken()) {
return Constants.IO.imageBaseDir + File.separator + "TOK" + File.separator + set; return Constants.IO.imageBaseDir + File.separator + "TOK" + File.separator + set;
} else { } else {
return Constants.IO.imageBaseDir + set; return Constants.IO.imageBaseDir + set;
} }
} }
public static String getImagePath(CardUrl card, boolean withCollector) { public static String getImagePath(CardInfo card, boolean withCollector) {
if (withCollector) { if (withCollector) {
return getImageDir(card) + File.separator + card.name + "." + card.collector + ".full.jpg"; return getImageDir(card) + File.separator + card.getName() + "." + card.getCollectorId() + ".full.jpg";
} else { } else {
return getImageDir(card) + File.separator + card.name + ".full.jpg"; return getImageDir(card) + File.separator + card.getName() + ".full.jpg";
} }
} }
} }