Fixed land download from Gatherer source.

This commit is contained in:
LevelX2 2017-12-29 11:33:56 +01:00
parent 9325a5d311
commit 5d5c724565
2 changed files with 121 additions and 92 deletions

View file

@ -262,6 +262,7 @@ public enum MagicCardsImageSource implements CardImageSource {
put("DDQ", "duel-decks-blessed-vs-cursed");
put("DDR", "duel-decks-nissa-vs-ob-nixilis");
put("DDS", "duel-decks-mind-vs-might");
put("DDS", "duel-decks-merfolk-vs-goblin");
put("DGM", "dragons-maze");
put("DKA", "dark-ascension");
put("DRB", "from-the-vault-dragons");

View file

@ -45,6 +45,9 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.prefs.Preferences;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.client.MageFrame;
import mage.client.dialog.PreferencesDialog;
import mage.remote.Connection;
@ -332,6 +335,7 @@ public enum WizardCardsImageSource implements CardImageSource {
setsAliases.put("DDQ", "Duel Decks: Blessed vs. Cursed");
setsAliases.put("DDR", "Duel Decks: Nissa vs. Ob Nixilis");
setsAliases.put("DDS", "Duel Decks: Mind vs. Might");
setsAliases.put("DDT", "Duel Decks: Merfolk vs. Goblins");
setsAliases.put("DGM", "Dragon's Maze");
setsAliases.put("DIS", "Dissension");
setsAliases.put("DKA", "Dark Ascension");
@ -473,6 +477,56 @@ public enum WizardCardsImageSource implements CardImageSource {
return null;
}
@Override
public String generateURL(CardDownloadData card) throws Exception {
String collectorId = card.getCollectorId();
String cardSet = card.getSet();
if (collectorId == null || cardSet == null) {
throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet);
}
if (card.isFlippedSide()) { //doesn't support rotated images
return null;
}
String setNames = setsAliases.get(cardSet);
if (setNames != null) {
Map<String, String> setLinks = sets.computeIfAbsent(cardSet, k -> getSetLinks(cardSet));
if (setLinks == null || setLinks.isEmpty()) {
return null;
}
String link = setLinks.get(card.getDownloadName().toLowerCase());
if (link == null) {
int length = collectorId.length();
if (Character.isLetter(collectorId.charAt(length - 1))) {
length -= 1;
}
int number = Integer.parseInt(collectorId.substring(0, length));
if (number > 0) {
String key = card.getDownloadName().toLowerCase() + number;
link = setLinks.get(key);
}
if (link == null) {
List<String> l = new ArrayList<>(setLinks.values());
if (l.size() >= number) {
link = l.get(number - 1);
} else {;
link = l.get(number - 21);
if (link != null) {
link = link.replace(Integer.toString(number - 20), (Integer.toString(number - 20) + 'a'));
}
}
}
}
if (link != null && !link.startsWith("http://")) {
link = "http://gatherer.wizards.com" + link;
}
return link;
}
return null;
}
private Map<String, String> getSetLinks(String cardSet) {
LinkedHashMap<String, String> setLinks = new LinkedHashMap<>();
ExecutorService executor = Executors.newFixedThreadPool(10);
@ -503,8 +557,12 @@ public enum WizardCardsImageSource implements CardImageSource {
}
String cardName = normalizeName(cardsImages.get(i).attr("alt"));
if (cardName != null && !cardName.isEmpty()) {
Runnable task = new GetImageLinkTask(multiverseId, cardName, preferedLanguage, setLinks);
executor.execute(task);
if (cardName.equals("Forest") || cardName.equals("Swamp") || cardName.equals("Mountain") || cardName.equals("Island") || cardName.equals("Plains")) {
getLandVariations(setLinks, cardSet, multiverseId, cardName);
} else {
Integer preferedMultiverseId = getLocalizedMultiverseId(preferedLanguage, multiverseId);
setLinks.put(cardName.toLowerCase(), generateLink(preferedMultiverseId));
}
}
}
page++;
@ -553,23 +611,38 @@ public enum WizardCardsImageSource implements CardImageSource {
return doc;
}
private Map<String, String> getLandVariations(int multiverseId, String cardName) throws IOException, NumberFormatException {
private void getLandVariations(LinkedHashMap<String, String> setLinks, String cardSet, int multiverseId, String cardName) throws IOException, NumberFormatException {
CardCriteria criteria = new CardCriteria();
criteria.name(cardName);
criteria.setCodes(cardSet);
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
String urlLandDocument = "http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=" + multiverseId;
Document landDoc = getDocument(urlLandDocument);
Elements variations = landDoc.select("a.variationlink");
Map<String, String> links = new HashMap<>();
if (!variations.isEmpty()) {
int landNumber = 1;
if (variations.size() > cards.size()) {
logger.warn("More links for lands than cards in DB found for set: " + cardSet + " Name: " + cardName);
}
if (variations.size() < cards.size()) {
logger.warn("Less links for lands than cards in DB found for set: " + cardSet + " Name: " + cardName);
}
int iteration = 0;
for (Element variation : variations) {
String colNumb = String.valueOf(iteration);
if (cards.size() > iteration) {
CardInfo cardInfo = cards.get(iteration);
if (cardInfo != null) {
colNumb = cardInfo.getCardNumber();
}
}
Integer landMultiverseId = Integer.parseInt(variation.attr("href").replaceAll("[^\\d]", ""));
links.put((cardName + landNumber).toLowerCase(), generateLink(landMultiverseId));
landNumber++;
setLinks.put((cardName).toLowerCase() + colNumb, generateLink(landMultiverseId));
iteration++;
}
} else {
links.put(cardName.toLowerCase(), generateLink(multiverseId));
setLinks.put(cardName.toLowerCase(), generateLink(multiverseId));
}
return links;
}
private static String generateLink(int landMultiverseId) {
@ -627,50 +700,6 @@ public enum WizardCardsImageSource implements CardImageSource {
.replace("Hintreland Scourge", "Hinterland Scourge");
}
@Override
public String generateURL(CardDownloadData card) throws Exception {
String collectorId = card.getCollectorId();
String cardSet = card.getSet();
if (collectorId == null || cardSet == null) {
throw new Exception("Wrong parameters for image: collector id: " + collectorId + ",card set: " + cardSet);
}
if (card.isFlippedSide()) { //doesn't support rotated images
return null;
}
String setNames = setsAliases.get(cardSet);
if (setNames != null) {
Map<String, String> setLinks = sets.computeIfAbsent(cardSet, k -> getSetLinks(cardSet));
if (setLinks == null || setLinks.isEmpty()) {
return null;
}
String link = setLinks.get(card.getDownloadName().toLowerCase());
if (link == null) {
int length = collectorId.length();
if (Character.isLetter(collectorId.charAt(length - 1))) {
length -= 1;
}
int number = Integer.parseInt(collectorId.substring(0, length));
List<String> l = new ArrayList<>(setLinks.values());
if (l.size() >= number) {
link = l.get(number - 1);
} else {;
link = l.get(number - 21);
if (link != null) {
link = link.replace(Integer.toString(number - 20), (Integer.toString(number - 20) + 'a'));
}
}
}
if (link != null && !link.startsWith("http://")) {
link = "http://gatherer.wizards.com" + link;
}
return link;
}
return null;
}
@Override
public String generateTokenUrl(CardDownloadData card) {
return null;
@ -681,44 +710,43 @@ public enum WizardCardsImageSource implements CardImageSource {
return 60.0f;
}
private final class GetImageLinkTask implements Runnable {
private int multiverseId;
private String cardName;
private String preferedLanguage;
private LinkedHashMap setLinks;
public GetImageLinkTask(int multiverseId, String cardName, String preferedLanguage, LinkedHashMap setLinks) {
try {
this.multiverseId = multiverseId;
this.cardName = cardName;
this.preferedLanguage = preferedLanguage;
this.setLinks = setLinks;
} catch (Exception ex) {
logger.error(ex.getMessage());
logger.error("multiverseId: " + multiverseId);
logger.error("cardName: " + cardName);
logger.error("preferedLanguage: " + preferedLanguage);
logger.error("setLinks: " + setLinks.toString());
}
}
@Override
public void run() {
try {
if (cardName.equals("Forest") || cardName.equals("Swamp") || cardName.equals("Mountain") || cardName.equals("Island") || cardName.equals("Plains")) {
setLinks.putAll(getLandVariations(multiverseId, cardName));
} else {
Integer preferedMultiverseId = getLocalizedMultiverseId(preferedLanguage, multiverseId);
setLinks.put(cardName.toLowerCase(), generateLink(preferedMultiverseId));
}
} catch (IOException | NumberFormatException ex) {
logger.error("Exception when parsing the wizards page: " + ex.getMessage());
}
}
}
// private final class GetImageLinkTask implements Runnable {
//
// private int multiverseId;
// private String cardName;
// private String preferedLanguage;
// private LinkedHashMap setLinks;
//
// public GetImageLinkTask(int multiverseId, String cardName, String preferedLanguage, LinkedHashMap setLinks) {
// try {
// this.multiverseId = multiverseId;
// this.cardName = cardName;
// this.preferedLanguage = preferedLanguage;
// this.setLinks = setLinks;
// } catch (Exception ex) {
// logger.error(ex.getMessage());
// logger.error("multiverseId: " + multiverseId);
// logger.error("cardName: " + cardName);
// logger.error("preferedLanguage: " + preferedLanguage);
// logger.error("setLinks: " + setLinks.toString());
// }
// }
//
// @Override
// public void run() {
// try {
// if (cardName.equals("Forest") || cardName.equals("Swamp") || cardName.equals("Mountain") || cardName.equals("Island") || cardName.equals("Plains")) {
// setLinks.putAll(getLandVariations(multiverseId, cardName));
// } else {
// Integer preferedMultiverseId = getLocalizedMultiverseId(preferedLanguage, multiverseId);
// setLinks.put(cardName.toLowerCase(), generateLink(preferedMultiverseId));
// }
// } catch (IOException | NumberFormatException ex) {
// logger.error("Exception when parsing the wizards page: " + ex.getMessage());
// }
// }
//
// }
@Override
public int getTotalImages() {
return -1;