image downloader now downloads card text from magiccard.info site. I still have to add all of the urls for the cards.

master
wait321 2011-10-09 11:59:28 -07:00
parent acbe2f339c
commit e23a6ef220
10 changed files with 245 additions and 48 deletions

View File

@ -750,6 +750,7 @@ timing=fmain
>Akroma, Angel of Wrath
image=http://magiccards.info/scans/en/tsts/1.jpg
url=http://magiccards.info/dvd/en/1.html
value=5
rarity=R
type=Legendary,Creature
@ -8441,6 +8442,7 @@ timing=equipment
>Akroma's Memorial
image=http://magiccards.info/scans/en/fut/159.jpg
url=http://magiccards.info/fut/en/159.html
value=5
rarity=R
type=Legendary,Artifact

View File

@ -1,5 +1,6 @@
package magic.data;
import magic.MagicMain;
import magic.model.MagicAbility;
import magic.model.MagicCardDefinition;
import magic.model.MagicColor;
@ -10,15 +11,20 @@ import magic.model.MagicSubType;
import magic.model.event.MagicTiming;
import magic.model.mstatic.MagicStatic;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Load card definitions from cards.txt and cards2.txt
@ -30,6 +36,12 @@ public class CardDefinitions {
private static final String CARDS_FILENAME="cards.txt";
private static final String EXTRA_CARDS_FILENAME="cards2.txt";
public static final String CARD_TEXT_FOLDER = "texts";
public static final String CARD_IMAGE_FOLDER = "cards";
public static final String TOKEN_IMAGE_FOLDER = "tokens";
public static final String CARD_IMAGE_EXT = CardImagesProvider.IMAGE_EXTENSION;
public static final String CARD_TEXT_EXT = ".txt";
private final List<MagicCardDefinition> cards;
private final List<MagicCardDefinition> landCards;
private final List<MagicCardDefinition> spellCards;
@ -45,6 +57,8 @@ public class CardDefinitions {
private static void setProperty(final MagicCardDefinition card,final String property,final String value) {
if ("image".equals(property)) {
card.setImageURL(value);
} else if ("url".equals(property)) {
card.setCardInfoURL(value);
} else if ("num_images".equals(property)) {
card.setImageCount(Integer.parseInt(value));
} else if ("cube".equals(property)) {
@ -230,6 +244,9 @@ public class CardDefinitions {
System.err.println(getNumberOfCards()+ " card definitions");
MagicCardDefinition.printStatistics();
// set card text
loadCardTexts();
}
public int getNumberOfCards() {
@ -252,6 +269,30 @@ public class CardDefinitions {
}
return cardDefinition;
}
public void loadCardTexts() {
for(MagicCardDefinition card : getCards()) {
if(card != MagicCardDefinition.UNKNOWN && card.getText().length() == 0) {
// try to load text from file
final StringBuilder buffer = new StringBuilder();
buffer.append(MagicMain.getGamePath());
buffer.append(File.separator);
buffer.append(CARD_TEXT_FOLDER);
buffer.append(File.separator);
buffer.append(card.getCardTextName());
buffer.append(CARD_TEXT_EXT);
try {
String text = FileIO.toStr(new File(buffer.toString()));
if(text != null) {
card.setText(text);
}
} catch (IOException e) {
// text not downloaded or missing
}
}
}
}
public MagicCardDefinition getBasicLand(final MagicColor color) {
if (MagicColor.Black.equals(color)) {

View File

@ -0,0 +1,59 @@
package magic.data;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
public class DownloadCardTextFile extends WebDownloader {
private static final String startPattern = "ctext\">";
private static final String endPattern = "</p>";
private final File file;
private URL url;
DownloadCardTextFile(final File file, final URL url) {
this.file = file;
this.url = url;
}
public String getFilename() {
return file.getName();
}
public void download(final Proxy proxy) {
String html = WebDownloader.getHTML(proxy, url);
// find text in html
int iStart = html.indexOf(startPattern);
String foundText = "";
if(iStart > -1) {
iStart += startPattern.length();
int iEnd = html.indexOf(endPattern, iStart);
foundText = html.substring(iStart, iEnd);
foundText = foundText.replaceAll("\\<br\\>", " "); // replace newlines
foundText = foundText.replaceAll("\\<[^\\>]*\\>", ""); // remove other html tags
}
// write text out to file
if(foundText.length() > 0) {
FileWriter outputStream = null;
try {
outputStream = new FileWriter(file);
outputStream.write(foundText);
} catch (final IOException ex) {
System.err.println("ERROR! Unable to write to card text file");
System.err.println(ex.getMessage());
ex.printStackTrace();
final boolean isDeleted = file.delete();
if (!isDeleted) {
System.err.println("ERROR! Unable to delete " + file);
}
} finally {
magic.data.FileIO.close(outputStream);
}
}
}
}

View File

@ -1,15 +1,10 @@
package magic.data;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Proxy;
import java.net.URL;
public class DownloadImageFile {
public class DownloadImageFile extends WebDownloader {
private final File file;
private final URL url;
@ -24,30 +19,6 @@ public class DownloadImageFile {
}
public void download(final Proxy proxy) {
OutputStream outputStream = null;
InputStream inputStream = null;
try { //download image
outputStream = new BufferedOutputStream(new FileOutputStream(file));
inputStream = url.openConnection(proxy).getInputStream();
final byte buffer[]=new byte[65536];
while (true) {
final int len=inputStream.read(buffer);
if (len<0) {
break;
}
outputStream.write(buffer,0,len);
}
} catch (final IOException ex) {
System.err.println("ERROR! Unable to download file");
System.err.println(ex.getMessage());
ex.printStackTrace();
final boolean isDeleted = file.delete();
if (!isDeleted) {
System.err.println("ERROR! Unable to delete " + file);
}
} finally {
magic.data.FileIO.close(inputStream);
magic.data.FileIO.close(outputStream);
}
WebDownloader.downloadToFile(proxy, url, file);
}
}

View File

@ -13,18 +13,20 @@ import java.util.ArrayList;
import java.util.Scanner;
/**
* Download the necessary images from the WWW
* Download the necessary images and files from the WWW
*/
public class DownloadImageFiles extends ArrayList<DownloadImageFile> {
public class DownloadMissingFiles extends ArrayList<WebDownloader> {
private static final long serialVersionUID = 1L;
public DownloadImageFiles(final String filename) {
public DownloadMissingFiles(final String filename) {
loadDownloadImageFiles(filename);
}
private void loadDownloadImageFiles(final String filename) {
final InputStream stream;
// download additional images
if (filename.startsWith("file://")) {
try { //create file input stream
stream=new FileInputStream(filename.substring(7));
@ -73,23 +75,50 @@ public class DownloadImageFiles extends ArrayList<DownloadImageFile> {
}
}
final File cardsPathFile=new File(gamePathFile,"cards");
// download card images and texts
final File cardsPathFile=new File(gamePathFile, CardDefinitions.CARD_IMAGE_FOLDER);
final File textPathFile = new File(gamePathFile, CardDefinitions.CARD_TEXT_FOLDER);
if (!textPathFile.mkdir()) {
System.err.println("WARNING. Unable to create " + textPathFile);
}
for (final MagicCardDefinition cardDefinition : CardDefinitions.getInstance().getCards()) {
final String imageURL=cardDefinition.getImageURL();
if (imageURL!=null) {
final File imageFile=new File(cardsPathFile,cardDefinition.getImageName()+".jpg");
// card image
final String imageURL = cardDefinition.getImageURL();
if (imageURL != null) {
final File imageFile=new File(cardsPathFile,cardDefinition.getImageName() + CardDefinitions.CARD_IMAGE_EXT);
//download if the file does not exists OR it is zero length OR it is outdated
if (!imageFile.exists() ||
imageFile.length() == 0L ||
cardDefinition.isIgnored(imageFile.length())) {
imageFile.length() == 0L ||
cardDefinition.isIgnored(imageFile.length())) {
try { //create URL
add(new DownloadImageFile(imageFile,new URL(imageURL)));
add(new DownloadCardTextFile(imageFile,new URL(imageURL)));
} catch (final java.net.MalformedURLException ex) {
System.err.println("ERROR! URL malformed " + imageURL);
}
}
}
// card text
final String textUrl = cardDefinition.getCardInfoURL();
if (textUrl != null && textUrl.length() > 0) {
final File textFile = new File(textPathFile, cardDefinition.getCardTextName() + CardDefinitions.CARD_TEXT_EXT);
// download if the file does not exists OR it is zero length OR it is outdated
if (!textFile.exists() ||
textFile.length() == 0L ||
cardDefinition.isIgnored(textFile.length())) {
try { // create URL
add(new DownloadCardTextFile(textFile, new URL(textUrl)));
} catch (final java.net.MalformedURLException ex) {
System.err.println("ERROR! URL malformed " + textUrl);
}
}
}
}
CardDefinitions.getInstance().loadCardTexts();
}
}

View File

@ -29,7 +29,7 @@ public class HighQualityCardImagesProvider implements CardImagesProvider {
final int imageIndex=index%cardDefinition.getImageCount();
final StringBuilder buffer=new StringBuilder();
buffer.append(MagicMain.getGamePath()).append(File.separator);
buffer.append(cardDefinition.isToken()?"tokens":"cards").append(File.separator);
buffer.append(cardDefinition.isToken()? CardDefinitions.TOKEN_IMAGE_FOLDER : CardDefinitions.CARD_IMAGE_FOLDER).append(File.separator);
buffer.append(cardDefinition.getImageName());
buffer.append(imageIndex>0?String.valueOf(imageIndex+1):"");
buffer.append(IMAGE_EXTENSION);

View File

@ -0,0 +1,71 @@
package magic.data;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Proxy;
import java.net.URL;
public class WebDownloader {
public void download(final Proxy proxy) { }
public String getFilename() { return ""; }
public static void downloadToFile(final Proxy proxy, final URL url, final File file) {
OutputStream outputStream = null;
InputStream inputStream = null;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(file));
inputStream = url.openConnection(proxy).getInputStream();
final byte buffer[]=new byte[65536];
while (true) {
final int len=inputStream.read(buffer);
if (len<0) {
break;
}
outputStream.write(buffer,0,len);
}
} catch (final IOException ex) {
System.err.println("ERROR! Unable to download file");
System.err.println(ex.getMessage());
ex.printStackTrace();
final boolean isDeleted = file.delete();
if (!isDeleted) {
System.err.println("ERROR! Unable to delete " + file);
}
} finally {
magic.data.FileIO.close(inputStream);
magic.data.FileIO.close(outputStream);
}
}
public static String getHTML(final Proxy proxy, final URL url) {
InputStream inputStream = null;
BufferedReader dataStream = null;
StringBuilder sb = new StringBuilder();
String line;
try {
inputStream = url.openConnection(proxy).getInputStream();
dataStream = new BufferedReader(new InputStreamReader(inputStream));
while( (line = dataStream.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (final IOException ex) {
System.err.println("ERROR! Unable to download webpage");
System.err.println(ex.getMessage());
ex.printStackTrace();
} finally {
magic.data.FileIO.close(inputStream);
}
return sb.toString();
}
}

View File

@ -63,6 +63,7 @@ public class MagicCardDefinition {
private final String name;
private final String fullName;
private String imageURL;
private String cardInfoUrl = "";
private int imageCount = 1;
private Collection<Long> ignore;
private int index=-1;
@ -82,6 +83,7 @@ public class MagicCardDefinition {
private int power=0;
private int toughness=0;
private long abilityFlags=0;
private String text = "";
private MagicStaticType staticType=MagicStaticType.None;
private MagicTiming timing=MagicTiming.None;
private MagicCardEvent cardEvent=MagicPlayCardEvent.getInstance();
@ -168,6 +170,18 @@ public class MagicCardDefinition {
public String getImageURL() {
return imageURL;
}
public void setCardInfoURL(final String url) {
this.cardInfoUrl = url;
}
public String getCardInfoURL() {
return this.cardInfoUrl;
}
public String getCardTextName() {
return getImageName();
}
public void setValue(final int value) {
this.value = value;
@ -591,6 +605,14 @@ public class MagicCardDefinition {
return ability.hasAbility(abilityFlags);
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return this.text;
}
public void setStaticType(final MagicStaticType staticType) {
this.staticType=staticType;
}

View File

@ -235,6 +235,7 @@ public class CardTableModel implements TableModel {
case 5: return card.getLongTypeString();
case 6: return card.getSubTypeString();
case 7: return card.getRarityString();
case 8: return card.getText();
}
return "";

View File

@ -1,8 +1,9 @@
package magic.ui;
import magic.data.DownloadImageFile;
import magic.data.DownloadImageFiles;
import magic.data.DownloadMissingFiles;
import magic.data.IconImages;
import magic.data.WebDownloader;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
@ -30,7 +31,7 @@ public class DownloadImagesDialog extends JDialog implements Runnable,ActionList
private static final String DOWNLOAD_IMAGES_FILENAME="images.txt";
private final MagicFrame frame;
private final DownloadImageFiles files;
private final DownloadMissingFiles files;
private final JComboBox proxyComboBox;
private final JTextField addressTextField;
private final JTextField portTextField;
@ -47,7 +48,7 @@ public class DownloadImagesDialog extends JDialog implements Runnable,ActionList
super(frame,true);
this.frame = frame;
this.setLayout(new BorderLayout());
this.setTitle("Download images");
this.setTitle("Download card images and text");
this.setSize(300,405);
this.setLocationRelativeTo(frame);
this.setResizable(false);
@ -116,12 +117,12 @@ public class DownloadImagesDialog extends JDialog implements Runnable,ActionList
buttonPanel.add(cancelButton);
add(buttonPanel,BorderLayout.SOUTH);
files=new DownloadImageFiles(DOWNLOAD_IMAGES_FILENAME);
files=new DownloadMissingFiles(DOWNLOAD_IMAGES_FILENAME);
if (files.isEmpty()) {
okButton.setEnabled(false);
progressBar.setMaximum(1);
progressBar.setValue(1);
downloadLabel.setText("All images are present.");
downloadLabel.setText("All images and text are present.");
} else {
downloadLabel.setText("Press OK to begin or Cancel.");
}
@ -151,7 +152,7 @@ public class DownloadImagesDialog extends JDialog implements Runnable,ActionList
});
int count=0;
for (final DownloadImageFile file : files) {
for (final WebDownloader file : files) {
final int curr = count + 1;
SwingUtilities.invokeLater(new Runnable() {
public void run() {