From 5d5c724565c6cd6a754043f8cd8b0e8d62a100c3 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Fri, 29 Dec 2017 11:33:56 +0100 Subject: [PATCH] Fixed land download from Gatherer source. --- .../dl/sources/MagicCardsImageSource.java | 1 + .../dl/sources/WizardCardsImageSource.java | 212 ++++++++++-------- 2 files changed, 121 insertions(+), 92 deletions(-) diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java index 0a8b1ba95c..0cfefa736a 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/MagicCardsImageSource.java @@ -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"); diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java index 301edb4757..efc266a49e 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java @@ -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 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 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 getSetLinks(String cardSet) { LinkedHashMap 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 getLandVariations(int multiverseId, String cardName) throws IOException, NumberFormatException { + private void getLandVariations(LinkedHashMap setLinks, String cardSet, int multiverseId, String cardName) throws IOException, NumberFormatException { + CardCriteria criteria = new CardCriteria(); + criteria.name(cardName); + criteria.setCodes(cardSet); + List 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 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 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 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;