Added wizards.com download source.

This commit is contained in:
North 2011-05-29 14:44:51 +03:00
parent e7510eb209
commit f6d7080e0c
6 changed files with 173 additions and 23 deletions

View file

@ -51,6 +51,12 @@
<version>0.2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>
<build>

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -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<String> getSetLinks(String cardSet) {
List<String> setLinks = new ArrayList<String>();
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<String> setLinks = (List<String>) 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;
}
}
}

View file

@ -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());

View file

@ -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";