From f6d7080e0cf99afec5963064808bf51175ad4376 Mon Sep 17 00:00:00 2001 From: North Date: Sun, 29 May 2011 14:44:51 +0300 Subject: [PATCH] Added wizards.com download source. --- Mage.Plugins/Mage.Card.Plugin/pom.xml | 6 ++ .../card/dl/sources/CardImageSource.java | 10 +++ .../dl/sources/MagicCardsImageSource.java | 31 +++++++ .../dl/sources/WizardCardsImageSource.java | 90 +++++++++++++++++++ .../plugins/card/images/DownloadPictures.java | 44 ++++++--- .../plugins/card/utils/CardImageUtils.java | 15 +--- 6 files changed, 173 insertions(+), 23 deletions(-) create mode 100644 Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/CardImageSource.java create mode 100644 Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java create mode 100644 Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java diff --git a/Mage.Plugins/Mage.Card.Plugin/pom.xml b/Mage.Plugins/Mage.Card.Plugin/pom.xml index cd8ff2c493..2ce3d9f254 100644 --- a/Mage.Plugins/Mage.Card.Plugin/pom.xml +++ b/Mage.Plugins/Mage.Card.Plugin/pom.xml @@ -51,6 +51,12 @@ 0.2.4 provided + + + org.jsoup + jsoup + 1.5.2 + diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/CardImageSource.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/CardImageSource.java new file mode 100644 index 0000000000..00feb2a4f5 --- /dev/null +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/CardImageSource.java @@ -0,0 +1,10 @@ +package org.mage.plugins.card.dl.sources; + +/** + * + * @author North + */ +public interface CardImageSource { + + public String generateURL(Integer collectorId, String cardSet) throws Exception; +} diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java new file mode 100644 index 0000000000..ba95e2879f --- /dev/null +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java @@ -0,0 +1,31 @@ +package org.mage.plugins.card.dl.sources; + +import org.mage.plugins.card.utils.CardImageUtils; + +/** + * + * @author North + */ +public class MagicCardsImageSource implements CardImageSource { + + private static CardImageSource instance = new MagicCardsImageSource(); + + public static CardImageSource getInstance() { + if (instance == null) { + instance = new MagicCardsImageSource(); + } + return instance; + } + + @Override + public String generateURL(Integer collectorId, String cardSet) throws Exception { + if (collectorId == null || cardSet == null) { + throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet); + } + String set = CardImageUtils.updateSet(cardSet, true); + String url = "http://magiccards.info/scans/en/"; + url += set.toLowerCase() + "/" + collectorId + ".jpg"; + + return url; + } +} diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java new file mode 100644 index 0000000000..15625e5d5f --- /dev/null +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java @@ -0,0 +1,90 @@ +package org.mage.plugins.card.dl.sources; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.select.Elements; +import org.mage.plugins.card.utils.CardImageUtils; + +/** + * + * @author North + */ +public class WizardCardsImageSource implements CardImageSource { + + private static CardImageSource instance; + private static Map setsAliases; + private Map sets; + + public static CardImageSource getInstance() { + if (instance == null) { + instance = new WizardCardsImageSource(); + } + return instance; + } + + public WizardCardsImageSource() { + sets = new HashMap(); + setsAliases = new HashMap(); + setsAliases.put("NPH", "newphyrexia"); + setsAliases.put("MBS", "mirrodinbesieged"); + setsAliases.put("SOM", "scarsofmirrodin"); + setsAliases.put("M11", "magic2011"); + setsAliases.put("ROE", "riseoftheeldrazi"); + setsAliases.put("WWK", "worldwake"); + setsAliases.put("ZEN", "zendikar"); + setsAliases.put("M10", "magic2010"); + setsAliases.put("ARB", "alarareborn"); + setsAliases.put("CON", "conflux"); + setsAliases.put("ALA", "shardsofalara"); + } + + private List getSetLinks(String cardSet) { + List setLinks = new ArrayList(); + try { + Document doc = Jsoup.connect("http://www.wizards.com/magic/tcg/article.aspx?x=mtg/tcg/" + cardSet + "/spoiler").get(); + Elements cardsImages = doc.select("img[height$=370]"); + for (int i = 0; i < cardsImages.size(); i++) { + setLinks.add(cardsImages.get(i).attr("src")); + } + } catch (IOException ex) { + ex.printStackTrace(); + } + return setLinks; + } + + @Override + public String generateURL(Integer collectorId, String cardSet) throws Exception { + if (collectorId == null || cardSet == null) { + throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet); + } + 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 setLinks = (List) sets.get(cardSet); + if (setLinks == null) { + setLinks = getSetLinks((String) setsAliases.get(cardSet)); + sets.put(cardSet, setLinks); + } + String link; + if (setLinks.size() >= collectorId) { + link = setLinks.get(collectorId - 1); + } else { + link = setLinks.get(collectorId - 21); + link = link.replace(Integer.toString(collectorId - 20), (Integer.toString(collectorId - 20) + "a")); + } + if (!link.startsWith("http://")) { + link = "http://www.wizards.com" + link; + } + return link; + } + } +} \ No newline at end of file diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/DownloadPictures.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/DownloadPictures.java index c6178ca4a7..ba66c39356 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/DownloadPictures.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/images/DownloadPictures.java @@ -2,7 +2,6 @@ package org.mage.plugins.card.images; import java.awt.Component; import java.awt.Dimension; -import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedInputStream; @@ -20,10 +19,8 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.FutureTask; import javax.swing.AbstractButton; import javax.swing.Box; @@ -51,6 +48,9 @@ import mage.cards.Card; import org.apache.log4j.Logger; import org.mage.plugins.card.CardUrl; import org.mage.plugins.card.constants.Constants; +import org.mage.plugins.card.dl.sources.CardImageSource; +import org.mage.plugins.card.dl.sources.MagicCardsImageSource; +import org.mage.plugins.card.dl.sources.WizardCardsImageSource; import org.mage.plugins.card.properties.SettingsManager; import org.mage.plugins.card.utils.CardImageUtils; @@ -69,8 +69,10 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab private JLabel jLabel1; private static boolean offlineMode = false; private JCheckBox checkBox; - private Object sync = new Object(); + private final Object sync = new Object(); + private static CardImageSource cardImageSource; + private Proxy p; private ExecutorService executor = Executors.newFixedThreadPool(10); @@ -100,13 +102,14 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab public JDialog getDlg(JFrame frame) { String title = "Downloading"; - final JDialog dlg = this.dlg.createDialog(frame, title); + final JDialog dialog = this.dlg.createDialog(frame, title); close.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { - dlg.setVisible(false); + dialog.setVisible(false); } }); - return dlg; + return dialog; } public void setCancel(boolean cancel) { @@ -153,17 +156,34 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab p0.add(jLabel1); p0.add(Box.createVerticalStrut(5)); - ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(new String[] { "magiccards.info" }); + ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(new String[] { "magiccards.info", "wizards.com" }); jComboBox1 = new JComboBox(); + + cardImageSource = MagicCardsImageSource.getInstance(); jComboBox1.setModel(jComboBox1Model); jComboBox1.setAlignmentX(Component.LEFT_ALIGNMENT); + jComboBox1.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JComboBox cb = (JComboBox) e.getSource(); + switch (cb.getSelectedIndex()) { + case 0: + cardImageSource = MagicCardsImageSource.getInstance(); + break; + case 1: + cardImageSource = WizardCardsImageSource.getInstance(); + break; + } + } + }); p0.add(jComboBox1); p0.add(Box.createVerticalStrut(5)); // Start final JButton b = new JButton("Start download"); b.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { new Thread(DownloadPictures.this).start(); b.setEnabled(false); @@ -190,6 +210,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab checkBox.setEnabled(!offlineMode); checkBox.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { if (checkBox.isSelected()) { int count = DownloadPictures.this.cardsInGame.size(); @@ -265,7 +286,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab } else { try { log.info("Card to download: " + card.name + " (" + card.set + ") " - + CardImageUtils.generateURL(card.collector, card.set)); + + cardImageSource.generateURL(card.collector, card.set)); } catch (Exception e) { log.error(e); } @@ -392,6 +413,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab this.type = type; } + @Override public void stateChanged(ChangeEvent e) { if (((AbstractButton) e.getSource()).isSelected()) { DownloadPictures.this.type = type; @@ -401,6 +423,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab } } + @Override public void run() { BufferedInputStream in; BufferedOutputStream out; @@ -430,7 +453,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab log.info("Downloading card: " + card.name + " (" + card.set + ")"); - URL url = new URL(CardImageUtils.generateURL(card.collector, card.set)); + URL url = new URL(cardImageSource.generateURL(card.collector, card.set)); if (ignoreUrls.contains(card.set) || card.token) { if (card.collector != 0) { continue; @@ -463,6 +486,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab this.url = url; } + @Override public void run() { try { BufferedInputStream in = new BufferedInputStream(url.openConnection(p).getInputStream()); diff --git a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java index f669b38277..bc8fbc2a8b 100644 --- a/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java +++ b/Mage.Plugins/Mage.Card.Plugin/src/main/java/org/mage/plugins/card/utils/CardImageUtils.java @@ -117,7 +117,7 @@ public class CardImageUtils { public static String cleanString(String in) { in = in.trim(); - StringBuffer out = new StringBuffer(); + StringBuilder out = new StringBuilder(); char c; for (int i = 0; i < in.length(); i++) { c = in.charAt(i); @@ -131,18 +131,7 @@ public class CardImageUtils { return out.toString().toLowerCase(); } - public static String generateURL(Integer collectorId, String cardSet) throws Exception { - if (collectorId == null || cardSet == null) { - throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet); - } - String set = updateSet(cardSet,true); - String url = "http://magiccards.info/scans/en/"; - url += set.toLowerCase() + "/" + collectorId + ".jpg"; - - return url; - } - - private static String updateSet(String cardSet, boolean forUrl) { + public static String updateSet(String cardSet, boolean forUrl) { String set = cardSet.toLowerCase(); if (set.equals("con")) { set = "cfx";