Skip to content

Commit

Permalink
Improvement on token search algorithm, not it even uses deckmaster we…
Browse files Browse the repository at this point in the history
…bsite.
  • Loading branch information
valfieri committed Aug 23, 2019
1 parent 4c6f512 commit e0a2a45
Showing 1 changed file with 87 additions and 4 deletions.
91 changes: 87 additions & 4 deletions projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Node;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand All @@ -14,11 +15,15 @@

import java.io.*;
import java.net.URL;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.stream.Stream;

import android.graphics.*;
Expand Down Expand Up @@ -158,7 +163,49 @@ public static Document findTokenPage(String imageurl, String name, String set, S
}
} catch (Exception e) {
}
System.out.println("Warning: Token " + name + " has not been found between " + set + " tokens, i will search for it between any other set...");
System.out.println("Warning: Token " + name + " has not been found between " + set + " tokens, i will search for it in https://deckmaster.info");
String json = "";
try {
URL url = new URL("https://deckmaster.info/includes/ajax.php?action=cardSearch&searchString=" + name);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
InputStream stream = httpcon.getInputStream();
int i;
while ((i = stream.read()) != -1) {
json = json + ((char) i);
}
} catch (Exception e) {
}
List<String> urls = new ArrayList<String>();
String[] tok = json.split(",");
for (int i = 0; i < tok.length; i++) {
if (tok[i].contains("multiverseid")) {
String id = tok[i].split(":")[1].replace("\"", "");
urls.add(id);
}
}
for (int i = 0; i < urls.size(); i++) {
try {
Document tokendoc = Jsoup.connect("https://deckmaster.info/card.php?multiverseid=" + urls.get(i)).get();
Elements stats = tokendoc.select("head meta");
for (int j = 0; j < stats.size(); j++) {
if (stats.get(j).attributes().get("content").contains("Token Creature") && stats.get(j).attributes().get("content").toLowerCase().contains(name.toLowerCase())) {
if (stats.get(j).attributes().get("content").contains(tokenstats.replace("X/X", "*/*")))
return tokendoc;
stats = tokendoc.select("body textarea");
for (int y = 0; y < stats.size(); y++) {
List<Node> nodes = stats.get(y).childNodes();
for (int p = 0; p < nodes.size(); p++) {
if (stats.get(y).childNode(p).attributes().get("#text").contains(tokenstats))
return tokendoc;
}
}
}
}
} catch (Exception e) {
}
}
System.out.println("Warning: Token " + name + " has not been found in https://deckmaster.info so i will search for it between any other set in " + imageurl + " (it can take long time)");
for (int i = 1; i < availableSets.length; i++) {
String currentSet = availableSets[i].toLowerCase().split(" - ")[0];
if (!currentSet.equalsIgnoreCase(set)) {
Expand Down Expand Up @@ -413,7 +460,23 @@ else if (scryset.equalsIgnoreCase("ZVE"))
if (CardImage.isEmpty())
CardImage = imgs.get(i).attributes().get("data-src");
URL url = new URL(CardImage);
InputStream in = new BufferedInputStream(url.openStream());
InputStream in = null;
try {
in = new BufferedInputStream(url.openStream());
} catch (IOException ex) {
System.err.println("Error: Problem downloading card: " + mappa.get(id) + "-" + id + " from " + scryset + " on ScryFall, i will retry 2 times more...");
try {
in = new BufferedInputStream(url.openStream());
} catch (IOException ex2) {
System.err.println("Error: Problem downloading card: " + mappa.get(id) + "-" + id + " from " + scryset + " on ScryFall, i will retry 1 time more...");
try {
in = new BufferedInputStream(url.openStream());
} catch (IOException ex3) {
System.err.println("Error: Problem downloading card: " + mappa.get(id) + "-" + id + " from " + scryset + " on ScryFall, i will not retry anymore...");
break;
}
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
Expand Down Expand Up @@ -577,13 +640,33 @@ else if (scryset.equalsIgnoreCase("ZVE"))
}
Elements imgstoken = doc.select("body img");
for (int p = 0; p < imgstoken.size(); p++) {
String titletoken = imgstoken.get(p).attributes().get("title");
String titletoken = imgstoken.get(p).attributes().get("alt");
if (titletoken.isEmpty())
titletoken = imgstoken.get(p).attributes().get("title");
if (titletoken.toLowerCase().contains(nametocheck.toLowerCase())) {
String CardImageToken = imgstoken.get(p).attributes().get("src");
if (CardImageToken.isEmpty())
CardImageToken = imgstoken.get(p).attributes().get("data-src");
URL urltoken = new URL(CardImageToken);
InputStream intoken = new BufferedInputStream(urltoken.openStream());
HttpURLConnection httpcontoken = (HttpURLConnection) urltoken.openConnection();
httpcontoken.addRequestProperty("User-Agent", "Mozilla/4.76");
InputStream intoken = null;
try {
intoken = new BufferedInputStream(httpcontoken.getInputStream());
} catch (IOException ex) {
System.err.println("Error: Problem downloading token: " + nametoken + "-" + id + "t, i will retry 2 times more...");
try {
intoken = new BufferedInputStream(httpcontoken.getInputStream());
} catch (IOException ex2) {
System.err.println("Error: Problem downloading token: " + nametoken + "-" + id + "t, i will retry 1 time more...");
try {
intoken = new BufferedInputStream(httpcontoken.getInputStream());
} catch (IOException ex3) {
System.err.println("Error: Problem downloading token: " + nametoken + "-" + id + "t, i will not retry anymore...");
break;
}
}
}
ByteArrayOutputStream outtoken = new ByteArrayOutputStream();
byte[] buftoken = new byte[1024];
int ntoken = 0;
Expand Down

0 comments on commit e0a2a45

Please sign in to comment.