-
Notifications
You must be signed in to change notification settings - Fork 975
Add TappedOut and MTGGoldfish deck URL loading #11045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Madwand99
wants to merge
1
commit into
Card-Forge:master
Choose a base branch
from
Madwand99:AddMoreDeckImportSites
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+307
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
forge-gui/src/main/java/forge/deck/MtgGoldfishDeckUrlProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| package forge.deck; | ||
|
|
||
| import forge.util.Localizer; | ||
| import org.apache.commons.text.StringEscapeUtils; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Set; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| final class MtgGoldfishDeckUrlProvider implements DeckUrlProvider { | ||
| private static final Pattern DECK_URL = Pattern.compile("(?i)(?:^|/)deck/(\\d+)(?:[/?#]|$)"); | ||
| private static final Pattern CARD_LINE = Pattern.compile("^(\\d+)\\s+(.+)$"); | ||
| private static final Pattern TITLE = Pattern.compile("(?is)<title>\\s*(.*?)\\s*(?:-\\s*Original Deck)?\\s*</title>"); | ||
| private static final Pattern FORMAT = inputValuePattern("deck_input_format"); | ||
| private static final Pattern COMMANDER = inputValuePattern("deck_input_commander"); | ||
| private static final Pattern COMMANDER_ALT = inputValuePattern("deck_input_commander_alt"); | ||
| private static final String PROVIDER_NAME = "MTGGoldfish"; | ||
| private static final Localizer localizer = Localizer.getInstance(); | ||
|
|
||
| @Override | ||
| public RemoteDeck load(final String normalizedUrl, final Iterable<Deck> savedDecks) throws IOException { | ||
| final String deckId = getDeckId(normalizedUrl); | ||
| final String html = DeckUrlLoader.readText("https://www.mtggoldfish.com/deck/" + deckId, PROVIDER_NAME); | ||
| final String text = DeckUrlLoader.readText("https://www.mtggoldfish.com/deck/download/" + deckId, PROVIDER_NAME); | ||
| final String deckName = getDeckName(html); | ||
|
|
||
| return new RemoteDeck( | ||
| DeckUrlLoader.getDeckName(deckName, deckId, normalizedUrl, savedDecks), | ||
| getDeckFormat(html), | ||
| normalizedUrl, | ||
| toSectionedImportText(text, getCommanders(html)), | ||
| PROVIDER_NAME); | ||
| } | ||
|
|
||
| static String getDeckId(final String deckUrl) throws IOException { | ||
| final Matcher matcher = DECK_URL.matcher(deckUrl); | ||
| if (matcher.find()) { | ||
| return matcher.group(1); | ||
| } | ||
| throw new IOException(localizer.getMessage("lblCouldNotFindDeckUrlId", PROVIDER_NAME)); | ||
| } | ||
|
|
||
| static String toSectionedImportText(final String text) { | ||
| return toSectionedImportText(text, Collections.emptySet()); | ||
| } | ||
|
|
||
| static String toSectionedImportText(final String text, final Set<String> commanders) { | ||
| final StringBuilder commander = new StringBuilder(); | ||
| final StringBuilder main = new StringBuilder(); | ||
| final StringBuilder sideboard = new StringBuilder(); | ||
| boolean wroteMain = false; | ||
| boolean afterBlankLine = false; | ||
| boolean inSideboard = false; | ||
|
|
||
| for (final String line : text.split("\\R")) { | ||
| final String trimmed = line.trim(); | ||
| if (trimmed.isEmpty()) { | ||
| afterBlankLine = true; | ||
| continue; | ||
| } | ||
| final Matcher matcher = CARD_LINE.matcher(trimmed); | ||
| if (!matcher.matches()) { | ||
| afterBlankLine = false; | ||
| continue; | ||
| } | ||
| if (!inSideboard && afterBlankLine && wroteMain) { | ||
| inSideboard = true; | ||
| } | ||
| final String cardName = matcher.group(2).trim(); | ||
| if (!inSideboard && commanders.contains(cardName)) { | ||
| commander.append(matcher.group(1)).append(' ').append(cardName).append('\n'); | ||
| } else { | ||
| (inSideboard ? sideboard : main).append(trimmed).append('\n'); | ||
| } | ||
| wroteMain = true; | ||
| afterBlankLine = false; | ||
| } | ||
|
|
||
| final StringBuilder out = new StringBuilder(); | ||
| DeckUrlProvider.appendSection(out, DeckSection.Commander, commander); | ||
| DeckUrlProvider.appendSection(out, DeckSection.Main, main); | ||
| DeckUrlProvider.appendSection(out, DeckSection.Sideboard, sideboard); | ||
| return out.toString(); | ||
| } | ||
|
|
||
| static Set<String> getCommanders(final String html) { | ||
| final Set<String> commanders = new LinkedHashSet<>(); | ||
| addInputValue(commanders, html, COMMANDER); | ||
| addInputValue(commanders, html, COMMANDER_ALT); | ||
| return commanders; | ||
| } | ||
|
|
||
| static String getDeckName(final String html) { | ||
| final Matcher matcher = TITLE.matcher(html); | ||
| if (!matcher.find()) { | ||
| return localizer.getMessage("lblDeckUrlDefaultDeckName", PROVIDER_NAME); | ||
| } | ||
| final String title = StringEscapeUtils.unescapeHtml4(matcher.group(1)).trim(); | ||
| return title.isBlank() ? localizer.getMessage("lblDeckUrlDefaultDeckName", PROVIDER_NAME) : title; | ||
| } | ||
|
|
||
| private static DeckFormat getDeckFormat(final String html) { | ||
| final String format = getInputValue(html, FORMAT); | ||
| return "commander".equalsIgnoreCase(format) | ||
| ? DeckFormat.Commander | ||
| : DeckFormat.Constructed; | ||
| } | ||
|
|
||
| private static void addInputValue(final Set<String> values, final String html, final Pattern inputPattern) { | ||
| final String value = getInputValue(html, inputPattern); | ||
| if (value != null && !value.isBlank()) { | ||
| values.add(value); | ||
| } | ||
| } | ||
|
|
||
| private static String getInputValue(final String html, final Pattern inputPattern) { | ||
| final Matcher matcher = inputPattern.matcher(html); | ||
| if (!matcher.find()) { | ||
| return null; | ||
| } | ||
| return StringEscapeUtils.unescapeHtml4(matcher.group(1)).trim(); | ||
| } | ||
|
|
||
| private static Pattern inputValuePattern(final String inputId) { | ||
| return Pattern.compile("(?is)<input\\b[^>]*\\bid=\"" + Pattern.quote(inputId) + "\"[^>]*\\bvalue=\"([^\"]*)\"[^>]*>"); | ||
| } | ||
| } |
129 changes: 129 additions & 0 deletions
129
forge-gui/src/main/java/forge/deck/TappedOutDeckUrlProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package forge.deck; | ||
|
|
||
| import forge.util.Localizer; | ||
| import org.apache.commons.text.StringEscapeUtils; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Locale; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| final class TappedOutDeckUrlProvider implements DeckUrlProvider { | ||
| private static final Pattern DECK_URL = Pattern.compile("(?i)(?:^|/)mtg-decks/([^/?#]+)/?"); | ||
| private static final Pattern CARD_LINE = Pattern.compile("^(\\d+)x?\\s+(.+?)(?:\\s+\\(([A-Z0-9_]{2,7})\\)\\s+\\S+)?$"); | ||
| private static final Pattern TITLE = Pattern.compile("(?is)<title>\\s*(.*?)\\s*(?:\\([^<]*MTG Deck\\))?\\s*</title>"); | ||
| private static final Pattern OG_TITLE = Pattern.compile("(?is)<meta\\s+property=\"og:title\"\\s+content=\"(?:MTG Deck:\\s*)?(.*?)\"\\s*/?>"); | ||
| private static final Pattern MTGA_EXPORT = Pattern.compile("(?is)<textarea\\b[^>]*id=\"mtga-textarea\"[^>]*>(.*?)</textarea>"); | ||
| private static final String PROVIDER_NAME = "TappedOut"; | ||
| private static final Localizer localizer = Localizer.getInstance(); | ||
|
|
||
| @Override | ||
| public RemoteDeck load(final String normalizedUrl, final Iterable<Deck> savedDecks) throws IOException { | ||
| final String deckSlug = getDeckSlug(normalizedUrl); | ||
| final String deckPage = "https://tappedout.net/mtg-decks/" + deckSlug + "/"; | ||
| final String html = DeckUrlLoader.readText(deckPage, PROVIDER_NAME); | ||
| final String deckName = getDeckName(html, deckSlug); | ||
|
|
||
| return new RemoteDeck( | ||
| DeckUrlLoader.getDeckName(deckName, deckSlug, normalizedUrl, savedDecks), | ||
| isCommanderPage(html) ? DeckFormat.Commander : DeckFormat.Constructed, | ||
| normalizedUrl, | ||
| toImportText(html), | ||
| PROVIDER_NAME); | ||
| } | ||
|
|
||
| static String getDeckSlug(final String deckUrl) throws IOException { | ||
| final Matcher matcher = DECK_URL.matcher(deckUrl); | ||
| if (matcher.find() && !matcher.group(1).isBlank()) { | ||
| return matcher.group(1); | ||
| } | ||
| throw new IOException(localizer.getMessage("lblCouldNotFindDeckUrlId", PROVIDER_NAME)); | ||
| } | ||
|
|
||
| static String toImportText(final String html) throws IOException { | ||
| final Matcher matcher = MTGA_EXPORT.matcher(html); | ||
| if (!matcher.find()) { | ||
| throw new IOException(localizer.getMessage("lblDeckUrlUnexpectedResponse", PROVIDER_NAME)); | ||
| } | ||
|
|
||
| final StringBuilder main = new StringBuilder(); | ||
| final StringBuilder commanders = new StringBuilder(); | ||
| final StringBuilder sideboard = new StringBuilder(); | ||
| StringBuilder currentSection = null; | ||
| for (final String line : StringEscapeUtils.unescapeHtml4(matcher.group(1)).split("\\R")) { | ||
| final String trimmed = line.trim(); | ||
| if ("Commander".equalsIgnoreCase(trimmed)) { | ||
| currentSection = commanders; | ||
| continue; | ||
| } | ||
| if ("Deck".equalsIgnoreCase(trimmed)) { | ||
| currentSection = main; | ||
| continue; | ||
| } | ||
| if ("Sideboard".equalsIgnoreCase(trimmed)) { | ||
| currentSection = sideboard; | ||
| continue; | ||
| } | ||
| if (trimmed.isBlank() || trimmed.equalsIgnoreCase("About") || trimmed.startsWith("Name ")) { | ||
| continue; | ||
| } | ||
| if (currentSection != null) { | ||
| appendCardLine(currentSection, trimmed); | ||
| } | ||
| } | ||
| if (commanders.isEmpty() && main.isEmpty() && sideboard.isEmpty()) { | ||
| throw new IOException(localizer.getMessage("lblNoPlayableCardsInDeckUrl", PROVIDER_NAME)); | ||
| } | ||
| final StringBuilder out = new StringBuilder(); | ||
| DeckUrlProvider.appendSection(out, DeckSection.Commander, commanders); | ||
| DeckUrlProvider.appendSection(out, DeckSection.Main, main); | ||
| DeckUrlProvider.appendSection(out, DeckSection.Sideboard, sideboard); | ||
| return out.toString(); | ||
| } | ||
|
|
||
| static String getDeckName(final String html, final String deckSlug) { | ||
| final String title = getFirstMatch(OG_TITLE, html); | ||
| if (title != null) { | ||
| return title; | ||
| } | ||
| final String pageTitle = getFirstMatch(TITLE, html); | ||
| if (pageTitle != null) { | ||
| return pageTitle; | ||
| } | ||
| return deckSlug.replace('-', ' ').trim(); | ||
| } | ||
|
|
||
| private static void appendCardLine(final StringBuilder out, final String line) { | ||
| final Matcher matcher = CARD_LINE.matcher(line.trim()); | ||
| if (!matcher.matches()) { | ||
| return; | ||
| } | ||
| if ("SUNF".equalsIgnoreCase(matcher.group(3))) { | ||
| return; | ||
| } | ||
| String cardName = matcher.group(2).trim(); | ||
| cardName = stripAfter(cardName, '#'); | ||
| cardName = stripAfter(cardName, '*'); | ||
| if (!cardName.isBlank()) { | ||
| out.append(matcher.group(1)).append(' ').append(cardName).append('\n'); | ||
| } | ||
| } | ||
|
|
||
| private static String getFirstMatch(final Pattern pattern, final String html) { | ||
| final Matcher matcher = pattern.matcher(html); | ||
| if (!matcher.find()) { | ||
| return null; | ||
| } | ||
| final String value = StringEscapeUtils.unescapeHtml4(matcher.group(1)).trim(); | ||
| return value.isBlank() ? null : value; | ||
| } | ||
|
|
||
| private static boolean isCommanderPage(final String html) { | ||
| return html.toLowerCase(Locale.ROOT).contains("commander / edh"); | ||
| } | ||
|
|
||
| private static String stripAfter(final String value, final char marker) { | ||
| final int markerIndex = value.indexOf(marker); | ||
| return markerIndex < 0 ? value : value.substring(0, markerIndex).trim(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you silently deleting decks user might have modified further locally?