Improved Dowloader: Set Async thread and displayed a progress bar for resource loading and card image downloading.

This commit is contained in:
valfieri
2019-08-25 22:29:24 +02:00
parent a5c9f877f7
commit 39884d9711
2 changed files with 323 additions and 156 deletions

View File

@@ -27,6 +27,7 @@ import java.util.HashMap;
import java.util.stream.Stream;
import android.graphics.*;
import android.app.ProgressDialog;
public class ImgDownloader {
@@ -141,104 +142,185 @@ public class ImgDownloader {
return name + " (" + totalcards + " cards)";
}
public static String getSpecialCardUrl(String id){
public static String getSpecialCardUrl(String id) {
String cardurl = "";
if(id.equals("15208711"))
if (id.equals("15208711"))
cardurl = "https://img.scryfall.com/cards/large/front/9/c/9c138bf9-8be6-4f1a-a82c-a84938ab84f5.jpg?1562279137";
else if(id.equals("15208712"))
else if (id.equals("15208712"))
cardurl = "https://img.scryfall.com/cards/normal/front/d/4/d453ee89-6122-4d51-989c-e78b046a9de3.jpg?1561758141";
else if(id.equals("2050321"))
else if (id.equals("2050321"))
cardurl = "https://img.scryfall.com/cards/large/front/1/8/18b9c83d-4422-4b95-9fc2-070ed6b5bdf6.jpg?1562701921";
else if(id.equals("2050322"))
else if (id.equals("2050322"))
cardurl = "https://crystal-cdn4.crystalcommerce.com/photos/504053/ooze_token_b.jpg";
else if(id.equals("22010012"))
else if (id.equals("22010012"))
cardurl = "https://img.scryfall.com/cards/normal/front/8/4/84dc847c-7a37-4c7f-b02c-30b3e4c91fb6.jpg?1561757490";
else if (id.equals("8759611"))
cardurl = "https://img.scryfall.com/cards/large/front/4/1/41004bdf-8e09-4b2c-9e9c-26c25eac9854.jpg?1562493483";
else if (id.equals("8759911"))
cardurl = "https://img.scryfall.com/cards/large/front/0/b/0b61d772-2d8b-4acf-9dd2-b2e8b03538c8.jpg?1562492461";
else if (id.equals("8759511"))
cardurl = "https://img.scryfall.com/cards/large/front/d/2/d224c50f-8146-4c91-9401-04e5bd306d02.jpg?1562496100";
else if (id.equals("8471611"))
cardurl = "https://img.scryfall.com/cards/png/front/8/4/84920a21-ee2a-41ac-a369-347633d10371.png?1562494702";
else if (id.equals("8760011"))
cardurl = "https://img.scryfall.com/cards/large/front/4/2/42ba0e13-d20f-47f9-9c86-2b0b13c39ada.jpg?1562493487";
else if (id.equals("401721"))
cardurl = "https://deckmaster.info/images/cards/DDP/401721-hr.jpg";
else if (id.equals("401722"))
cardurl = "https://deckmaster.info/images/cards/DDP/401722-hr.jpg";
else if (id.equals("19784311"))
cardurl = "https://deckmaster.info/images/cards/AKH/-4173-hr.jpg";
else if (id.equals("19784312"))
cardurl = "https://deckmaster.info/images/cards/BNG/-10-hr.jpg";
else if (id.equals("19784313"))
cardurl = "https://deckmaster.info/images/cards/DDD/201843-hr.jpg";
else if (id.equals("20787512"))
cardurl = "https://deckmaster.info/images/cards/SOM/-227-hr.jpg";
else if (id.equals("20787511"))
cardurl = "https://deckmaster.info/images/cards/SOM/-226-hr.jpg";
else if (id.equals("11492111"))
cardurl = "https://deckmaster.info/images/cards/TSP/-2841-hr.jpg";
else if (id.equals("11492112"))
cardurl = "https://deckmaster.info/images/cards/TSP/-2840-hr.jpg";
else if (id.equals("11492113"))
cardurl = "https://img.scryfall.com/cards/large/front/5/b/5b9f471a-1822-4981-95a9-8923d83ddcbf.jpg?1562702075";
else if (id.equals("11492114"))
cardurl = "https://deckmaster.info/images/cards/DDN/386322-hr.jpg";
else if (id.equals("11492115"))
cardurl = "https://deckmaster.info/images/cards/DDE/209162-hr.jpg";
return cardurl;
}
public static String getSpecialTokenUrl(String id){
public static String getSpecialTokenUrl(String id) {
String tokenurl = "";
if(id.equals("75291t"))
if (id.equals("75291t"))
tokenurl = "http://4.bp.blogspot.com/-y5Fanm3qvrU/Vmd4gGnl2DI/AAAAAAAAAWY/FCrS9FTgOJk/s1600/Tatsumasa%2BToken.jpg";
else if(id.equals("202474t"))
else if (id.equals("435411t") || id.equals("435410t"))
tokenurl = "https://deckmaster.info/images/cards/XLN/-5173-hr.jpg";
else if (id.equals("202474t") || id.equals("1098t"))
tokenurl = "https://deckmaster.info/images/cards/AST/-884-hr.jpg";
else if(id.equals("202590t"))
else if (id.equals("202590t"))
tokenurl = "https://deckmaster.info/images/cards/AST/-892-hr.jpg";
else if(id.equals("201124t"))
else if (id.equals("201124t"))
tokenurl = "http://i1013.photobucket.com/albums/af260/lovesoldier99/STARFISHTOKEN.jpg";
else if(id.equals("184735"))
else if (id.equals("184735"))
tokenurl = "https://i.pinimg.com/originals/a9/fb/37/a9fb37bdfa8f8013b7eb854d155838e2.jpg";
else if(id.equals("184598t"))
else if (id.equals("184598t"))
tokenurl = "https://deckmaster.info/images/cards/HM/-2070-hr.jpg";
else if(id.equals("184589t"))
else if (id.equals("184589t"))
tokenurl = "http://d1f83aa4yffcdn.cloudfront.net/TOKEN/2%202%20Black%20Zombie.jpg";
else if(id.equals("184730t"))
tokenurl="https://www.mtg.onl/static/c88f42f8bd5a7c25aa36902546b690f5/4d406/PROXY_Knight_W_1_1.jpg";
else if(id.equals("1649t") || id.equals("201182t"))
else if (id.equals("184730t"))
tokenurl = "https://www.mtg.onl/static/c88f42f8bd5a7c25aa36902546b690f5/4d406/PROXY_Knight_W_1_1.jpg";
else if (id.equals("1649t") || id.equals("201182t"))
tokenurl = "https://pbs.twimg.com/media/DH9n-2JVwAA0o8z.jpg";
else if(id.equals("140233t") || id.equals("191239t") || id.equals("205957t"))
else if (id.equals("140233t") || id.equals("191239t") || id.equals("205957t"))
tokenurl = "https://i860.photobucket.com/albums/ab170/mistergreen527/White%20Tokens/WAvatarX-X1.jpg";
else if(id.equals("1686t") || id.equals("2881t") || id.equals("201231t"))
else if (id.equals("1686t") || id.equals("2881t") || id.equals("201231t"))
tokenurl = "https://deckmaster.info/images/cards/A25/-5648-hr.jpg";
else if(id.equals("121261t"))
else if (id.equals("121261t"))
tokenurl = "https://i.pinimg.com/originals/a9/fb/37/a9fb37bdfa8f8013b7eb854d155838e2.jpg";
else if(id.equals("368951t"))
else if (id.equals("368951t"))
tokenurl = "https://d1rw89lz12ur5s.cloudfront.net/photo/facetofacegames/file/36262794e9f37368e7872326715ac806/eletok.jpg";
else if(id.equals("46168t"))
else if (id.equals("46168t"))
tokenurl = "https://deckmaster.info/images/cards/KLD/-3287-hr.jpg";
else if(id.equals("49026t"))
else if (id.equals("49026t"))
tokenurl = "https://www.mtg.onl/static/a9d81341e62e39e75075b573739f39d6/4d406/PROXY_Wirefly_2_2.jpg";
else if(id.equals("414506t"))
else if (id.equals("414506t"))
tokenurl = "https://poromagia.com/media/cache/25/f0/25f0cd307adc18d7655c465408267469.jpg";
else if(id.equals("6142t"))
else if (id.equals("6142t"))
tokenurl = "https://cdn.staticneo.com/w/mtg/c/cd/Beast5.jpg";
else if(id.equals("126166t"))
else if (id.equals("126166t"))
tokenurl = "https://deckmaster.info/images/cards/C14/-487-hr.jpg";
else if(id.equals("136155t"))
else if (id.equals("136155t"))
tokenurl = "http://static1.squarespace.com/static/583dca25ff7c5080991b2c87/583de52de6f2e18631eb2b32/58405d0dbe6594762f5bd8e6/1565969982322/wurm-white.jpg";
else if(id.equals("107091t"))
else if (id.equals("107091t"))
tokenurl = "https://media.mtgsalvation.com/attachments/13/534/635032476540667501.jpg";
else if(id.equals("452760t"))
else if (id.equals("452760t"))
tokenurl = "https://deckmaster.info/images/cards/M19/-6036.jpg";
else if(id.equals("2959t"))
else if (id.equals("2959t"))
tokenurl = "https://deckmaster.info/images/cards/HM/-2070-hr.jpg";
else if(id.equals("380486t"))
else if (id.equals("380486t"))
tokenurl = "https://deckmaster.info/images/cards/BNG/-5-hr.jpg";
else if(id.equals("380487t"))
else if (id.equals("380487t"))
tokenurl = "https://poromagia.com/media/cache/25/f0/25f0cd307adc18d7655c465408267469.jpg";
else if(id.equals("234849t"))
else if (id.equals("234849t"))
tokenurl = "https://deckmaster.info/images/cards/RTR/-61-hr.jpg";
else if(id.equals("23319t"))
else if (id.equals("23319t"))
tokenurl = "https://i860.photobucket.com/albums/ab170/mistergreen527/White%20Tokens/WReflectionX-X1.jpg";
else if(id.equals("205297t") || id.equals("50104t"))
else if (id.equals("205297t") || id.equals("50104t"))
tokenurl = "https://www.mtg.onl/static/df30395b530524a3988428d4c0b37161/4d406/PROXY_Pest_0_1.jpg";
else if(id.equals("3449t"))
else if (id.equals("3449t"))
tokenurl = "https://www.mtg.onl/static/8c7fed1a0b8edd97c0fb0ceab24a654f/4d406/PROXY_Goblin_Scout_R_1_1.jpg";
else if(id.equals("3392t"))
else if (id.equals("3392t"))
tokenurl = "https://deckmaster.info/images/cards/DDR/417498-hr.jpg";
else if(id.equals("3280t"))
else if (id.equals("3280t"))
tokenurl = "https://media.mtgsalvation.com/attachments/54/421/635032484680831888.jpg";
else if(id.equals("3242t"))
else if (id.equals("3242t"))
tokenurl = "https://deckmaster.info/images/cards/MI/-2828-hr.jpg";
else if(id.equals("426025t"))
else if (id.equals("426025t"))
tokenurl = "https://cdn.shopify.com/s/files/1/0790/8591/products/Grnelementalfinal_800x800.jpg?v=1476398274";
else if(id.equals("19878t"))
else if (id.equals("19878t"))
tokenurl = "https://www.cardkingdom.com/images/magic-the-gathering/commander-2014/ape-token-zombie-token-blue-65252-medium.jpg";
else if(id.equals("21381t") || id.equals("40198t"))
else if (id.equals("21381t") || id.equals("40198t"))
tokenurl = "https://img.scryfall.com/cards/large/back/8/c/8ce60642-e207-46e6-b198-d803ff3b47f4.jpg?1562921132";
else if(id.equals("265141t"))
else if (id.equals("265141t"))
tokenurl = "https://media.mtgsalvation.com/attachments/102/31/635032498723573408.jpg";
else if(id.equals("24624t"))
else if (id.equals("24624t"))
tokenurl = "https://www.mtg.onl/static/6d717cba653ea9e3f6bd1419741671cb/4d406/PROXY_Minion_B_1_1.jpg";
else if (id.equals("409810t") || id.equals("409805t") || id.equals("409953t") || id.equals("409997t") || id.equals("410032t"))
tokenurl = "https://deckmaster.info/images/cards/SOI/-2404-hr.jpg";
else if (id.equals("74492t"))
tokenurl = "https://media.mtgsalvation.com/attachments/94/295/635032496473215708.jpg";
else if (id.equals("88973t"))
tokenurl = "https://deckmaster.info/images/cards/DDQ/409655-hr.jpg";
else if (id.equals("89051t"))
tokenurl = "https://www.mtg.onl/static/b7625a256e10bcec251a1a0abbf17bd4/4d406/PROXY_Horror_B_4_4.jpg";
else if (id.equals("5261t"))
tokenurl = "https://static.cardmarket.com/img/5a0199344cad68eebeefca6fa24e52c3/items/1/MH1/376905.jpg";
else if (id.equals("116384t"))
tokenurl = "https://deckmaster.info/images/cards/TSP/-114916-hr.jpg";
else if (id.equals("116383t"))
tokenurl = "https://i.imgur.com/wRMebWg.jpg";
else if (id.equals("114917t"))
tokenurl = "https://deckmaster.info/images/cards/JOU/-43-hr.jpg";
else if (id.equals("5610t"))
tokenurl = "https://deckmaster.info/images/cards/DDE/207998-hr.jpg";
else if (id.equals("185704t"))
tokenurl = "https://deckmaster.info/images/cards/ZEN/-277-hr.jpg";
else if (id.equals("461099t"))
tokenurl = "https://img.scryfall.com/cards/large/front/d/e/de7ba875-f77b-404f-8b75-4ba6f81da410.jpg?1557575978";
else if (id.equals("9667t"))
tokenurl = "https://www.mtg.onl/static/abe5178af8ebbe84f5504493a1b5f154/4d406/PROXY_Giant_Chicken_R_4_4.jpg";
else if (id.equals("368549t"))
tokenurl = "https://deckmaster.info/images/cards/DDQ/409655-hr.jpg";
else if (id.equals("73953t"))
tokenurl = "https://www.mtg.onl/static/98da67c454fe6cde61ae45af4f9c3150/4d406/PROXY_Giant_Teddy_Bear_5_5.jpg";
else if (id.equals("74265t"))
tokenurl = "https://deckmaster.info/images/cards/UNH/-2064-hr.jpg";
else if (id.equals("27634t"))
tokenurl = "https://www.mtg.onl/static/8b684bdea239d594e296a134f5ec1783/4d406/PROXY_Hippo_G_1_1.jpg";
else if (id.equals("111046t"))
tokenurl = "https://media.mtgsalvation.com/attachments/32/354/635032480299772645.jpg";
else if (id.equals("4771t"))
tokenurl = "https://www.mtg.onl/static/b8060dffbaf67ef987c6324c1523d3e4/4d406/PROXY_Hound_G_1_1.jpg";
else if (id.equals("3591t"))
tokenurl = "https://i.pinimg.com/564x/6e/8d/fe/6e8dfeee2919a3efff210df56ab7b85d.jpg";
return tokenurl;
}
public static boolean hasToken(String id){
if(id.equals("456378") || id.equals("2912") || id.equals("1514") || id.equals("364") || id.equals("69") || id.equals("369012") ||
public static boolean hasToken(String id) {
if (id.equals("456378") || id.equals("2912") || id.equals("1514") || id.equals("364") || id.equals("69") || id.equals("369012") ||
id.equals("417759") || id.equals("386476") || id.equals("456371") || id.equals("456360") || id.equals("391958") || id.equals("466959") ||
id.equals("466813") || id.equals("201176") || id.equals("202483") || id.equals("3546") || id.equals("425949") || id.equals("426027") ||
id.equals("425853") || id.equals("425846") || id.equals("426036") || id.equals("370387") || id.equals("29955") || id.equals("29989") ||
id.equals("19741") || id.equals("19722") || id.equals("19706") || id.equals("24597") || id.equals("24617") || id.equals("24563"))
id.equals("466813") || id.equals("201176") || id.equals("202483") || id.equals("3546") || id.equals("425949") || id.equals("426027") ||
id.equals("425853") || id.equals("425846") || id.equals("426036") || id.equals("370387") || id.equals("29955") || id.equals("29989") ||
id.equals("19741") || id.equals("19722") || id.equals("19706") || id.equals("24597") || id.equals("24617") || id.equals("24563") ||
id.equals("253539") || id.equals("277995") || id.equals("265415") || id.equals("289225") || id.equals("289215") || id.equals("253529") ||
id.equals("253641") || id.equals("270957") || id.equals("401685") || id.equals("89116") || id.equals("5183") || id.equals("5177") ||
id.equals("209289") || id.equals("198171") || id.equals("10419") || id.equals("470542") || id.equals("29992"))
return false;
return true;
}
@@ -248,7 +330,7 @@ public class ImgDownloader {
Elements outlinks = null;
try {
doc = Jsoup.connect(imageurl + "t" + set.toLowerCase()).get();
if(doc != null) {
if (doc != null) {
outlinks = doc.select("body a");
if (outlinks != null) {
for (int k = 0; k < outlinks.size(); k++) {
@@ -256,10 +338,10 @@ public class ImgDownloader {
if (linktoken != null && !linktoken.isEmpty()) {
try {
Document tokendoc = Jsoup.connect(linktoken).get();
if(tokendoc == null)
if (tokendoc == null)
continue;
Elements stats = tokendoc.select("head meta");
if(stats != null) {
if (stats != null) {
for (int j = 0; j < stats.size(); j++) {
String a = stats.get(j).attributes().get("content");
if (stats.get(j).attributes().get("content").contains(tokenstats) &&
@@ -281,10 +363,10 @@ public class ImgDownloader {
try {
URL url = new URL("https://deckmaster.info/includes/ajax.php?action=cardSearch&searchString=" + name);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
if(httpcon != null) {
if (httpcon != null) {
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
InputStream stream = httpcon.getInputStream();
if(stream != null) {
if (stream != null) {
int i;
while ((i = stream.read()) != -1) {
json = json + ((char) i);
@@ -304,10 +386,10 @@ public class ImgDownloader {
for (int i = 0; i < urls.size(); i++) {
try {
Document tokendoc = Jsoup.connect("https://deckmaster.info/card.php?multiverseid=" + urls.get(i)).get();
if(tokendoc == null)
if (tokendoc == null)
continue;
Elements stats = tokendoc.select("head meta");
if(stats != null) {
if (stats != null) {
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", "★/★")))
@@ -336,18 +418,18 @@ public class ImgDownloader {
if (!currentSet.equalsIgnoreCase(set)) {
try {
doc = Jsoup.connect(imageurl + "t" + currentSet).get();
if(doc == null)
if (doc == null)
continue;
outlinks = doc.select("body a");
if(outlinks != null) {
if (outlinks != null) {
for (int k = 0; k < outlinks.size(); k++) {
String linktoken = outlinks.get(k).attributes().get("href");
try {
Document tokendoc = Jsoup.connect(linktoken).get();
if(tokendoc == null)
if (tokendoc == null)
continue;
Elements stats = tokendoc.select("head meta");
if(stats != null) {
if (stats != null) {
for (int j = 0; j < stats.size(); j++) {
String a = stats.get(j).attributes().get("content");
if (stats.get(j).attributes().get("content").contains(tokenstats) && stats.get(j).attributes().get("content").toLowerCase().contains(name.toLowerCase())) {
@@ -368,7 +450,7 @@ public class ImgDownloader {
throw new Exception();
}
public static String DownloadCardImages(String set, String[] availableSets, String targetres, String basePath, String destinationPath) throws IOException {
public static String DownloadCardImages(String set, String[] availableSets, String targetres, String basePath, String destinationPath, ProgressDialog progressBarDialog) throws IOException {
String res = "";
String baseurl = "https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=";
@@ -472,8 +554,19 @@ public class ImgDownloader {
lines = lines.substring(c + 8);
if (primitive != null && id != null && !id.equalsIgnoreCase("null"))
mappa.put(id, primitive);
if (id.equals("114921")) {
mappa.put("11492111", "Citizen");
mappa.put("11492112", "Camarid");
mappa.put("11492113", "Thrull");
mappa.put("11492114", "Goblin");
mappa.put("11492115", "Saproling");
}
}
progressBarDialog.setProgress(0);
progressBarDialog.setMax(totalcards);
progressBarDialog.setTitle("Downloading set: " + set);
File imgPath = new File(destinationPath + set + "/");
if (!imgPath.exists()) {
System.out.println("creating directory: " + imgPath.getName());
@@ -509,29 +602,29 @@ public class ImgDownloader {
for (int y = 0; y < mappa.size(); y++) {
String id = mappa.keySet().toArray()[y].toString();
Document doc = null;
try{
try {
doc = Jsoup.connect(baseurl + id).get();
} catch(Exception e) {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will retry 2 times more...");
try{
} catch (Exception e) {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will retry 2 times more...");
try {
doc = Jsoup.connect(baseurl + id).get();
} catch(Exception e2) {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will retry 1 time more...");
try{
} catch (Exception e2) {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will retry 1 time more...");
try {
doc = Jsoup.connect(baseurl + id).get();
} catch(Exception e3) {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will not retry anymore...");
} catch (Exception e3) {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will not retry anymore...");
continue;
}
}
}
if(doc == null){
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will not retry anymore...");
if (doc == null) {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will not retry anymore...");
continue;
}
Elements divs = doc.select("body div");
if(divs == null){
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will not retry anymore...");
if (divs == null) {
System.err.println("Error: Problem reading card (" + mappa.get(id) + ") infos from: " + baseurl + id + ", i will not retry anymore...");
continue;
}
String scryset = currentSet;
@@ -595,12 +688,12 @@ public class ImgDownloader {
}
}
String specialcardurl = getSpecialCardUrl(id);
if(!specialcardurl.isEmpty()){
if (!specialcardurl.isEmpty()) {
URL url = new URL(specialcardurl);
InputStream in;
try{
try {
in = new BufferedInputStream(url.openStream());
}catch(Exception ex){
} catch (Exception 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());
@@ -625,6 +718,11 @@ public class ImgDownloader {
byte[] response = out.toByteArray();
String cardimage = imgPath + "/" + id + ".jpg";
String thumbcardimage = thumbPath + "/" + id + ".jpg";
if (id.equals("11492111") || id.equals("11492112") || id.equals("11492113") ||
id.equals("11492114") || id.equals("11492115")) {
cardimage = imgPath + File.separator + id + "t.jpg";
thumbcardimage = thumbPath + File.separator + id + "t.jpg";
}
FileOutputStream fos = new FileOutputStream(cardimage);
fos.write(response);
fos.close();
@@ -644,15 +742,15 @@ public class ImgDownloader {
} catch (IOException e) {
e.printStackTrace();
}
progressBarDialog.incrementProgressBy((int) (1));
continue;
}
if(doc == null){
if (doc == null) {
System.err.println("Error: Problem fetching card: " + mappa.get(id) + "-" + id + " from " + scryset + " on ScryFall, i will not download it...");
continue;
}
Elements imgs = doc.select("body img");
if(imgs == null){
if (imgs == null) {
System.err.println("Error: Problem fetching card: " + mappa.get(id) + "-" + id + " from " + scryset + " on ScryFall, i will not download it...");
continue;
}
@@ -770,7 +868,7 @@ public class ImgDownloader {
}
}
}
if (hasToken(id) && ((text.trim().toLowerCase().contains("create") && text.trim().toLowerCase().contains("creature token")) || (text.trim().toLowerCase().contains("put") && text.trim().toLowerCase().contains("token")))) {
if (hasToken(id) && ((text.trim().toLowerCase().contains("create") && text.trim().toLowerCase().contains("creature token")) || (text.trim().toLowerCase().contains("put") && text.trim().toLowerCase().contains("token")))) {
boolean tokenfound = false;
String arrays[] = text.trim().split(" ");
String nametoken = "";
@@ -833,17 +931,17 @@ public class ImgDownloader {
}
String specialtokenurl = getSpecialTokenUrl(id + "t");
Elements imgstoken;
if(!specialtokenurl.isEmpty()) {
try{
if (!specialtokenurl.isEmpty()) {
try {
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();
} catch (Exception ex) {
System.err.println("Error: Problem occurring while searching for token: " + nametoken + "-" + id + "t, i will not download it...");
break;
}
if(doc == null)
if (doc == null)
break;
imgstoken = doc.select("body img");
if(imgstoken == null)
if (imgstoken == null)
break;
tokenfound = true;
} else {
@@ -864,10 +962,10 @@ public class ImgDownloader {
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();
}
}
if(doc == null)
if (doc == null)
break;
imgstoken = doc.select("body img");
if(imgstoken == null)
if (imgstoken == null)
break;
}
for (int p = 0; p < imgstoken.size(); p++) {
@@ -879,10 +977,10 @@ public class ImgDownloader {
if (CardImageToken.isEmpty())
CardImageToken = imgstoken.get(p).attributes().get("data-src");
URL urltoken = new URL(CardImageToken);
if(!specialtokenurl.isEmpty())
if (!specialtokenurl.isEmpty())
urltoken = new URL(specialtokenurl);
HttpURLConnection httpcontoken = (HttpURLConnection) urltoken.openConnection();
if(httpcontoken == null) {
if (httpcontoken == null) {
System.err.println("Error: Problem downloading token: " + nametoken + "-" + id + "t, i will not download it...");
break;
}
@@ -942,7 +1040,7 @@ public class ImgDownloader {
}
}
}
progressBarDialog.incrementProgressBy((int) (1));
break;
}
}

View File

@@ -358,64 +358,87 @@ public class SDLActivity extends Activity implements OnKeyListener {
}
}
private void downloadCardInfo() {
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
cardDownloader.setTitle("Image Downloader");
cardDownloader.setMessage("IMPORTANT: After you press OK you may have to wait a bit while Downloader will evaluate all the sets available in this game version.");
cardDownloader.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
downloadCardImages();
}
});
cardDownloader.create().show();
}
String set = "";
String[] availableSets;
Integer totalset = 0;
boolean finished = false;
boolean loadResInProgress = false;
ProgressDialog progressBarDialogRes;
private void loadAvailableSets() {
final Handler mHandler = new Handler();
progressBarDialogRes = new ProgressDialog(this);
progressBarDialogRes.setTitle("Loading all available sets...");
progressBarDialogRes.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBarDialogRes.setProgress(0);
new Thread(new Runnable() {
public void run() {
ArrayList<String> sets = new ArrayList<String>();
if (availableSets == null) {
loadResInProgress = true;
File baseFolder = new File(getSystemStorageLocation());
File[] listOfFiles = baseFolder.listFiles();
ZipFile zipFile = null;
try {
zipFile = new ZipFile(baseFolder + "/" + listOfFiles[0].getName());
Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement();
String entryName = entry.getName();
if (entryName != null && entryName.contains("sets/")) {
if (!entryName.equalsIgnoreCase("sets/") && !entryName.contains("primitives") && !entryName.contains(".")) {
String[] names = entryName.split("/");
sets.add(names[1]);
}
}
}
} catch (IOException ioe) {
System.out.println("Error opening zip file" + ioe);
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (IOException ioe) {
System.out.println("Error while closing zip file" + ioe);
}
}
availableSets = new String[sets.size() + 1];
availableSets[0] = "*.* - All Wagic sets (thousands of cards)";
progressBarDialogRes.setMax(sets.size());
for (int i = 1; i < availableSets.length; i++) {
availableSets[i] = sets.get(i - 1) + " - " + ImgDownloader.getSetInfo(sets.get(i - 1), true, getSystemStorageLocation());
progressBarDialogRes.incrementProgressBy((int) (1));
}
}
finished = true;
loadResInProgress = false;
progressBarDialogRes.dismiss();
mHandler.post(new Runnable() {
public void run() {
while (!finished) {
try {
Thread.sleep(5000);
} catch (Exception e) {
}
}
downloadCardImages();
}
});
}
}).start();
progressBarDialogRes.show();
}
private void downloadCardImages() {
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
cardDownloader.setTitle("Which Set would you like to download?");
ArrayList<String> sets = new ArrayList<String>();
if (availableSets == null) {
File baseFolder = new File(getSystemStorageLocation());
File[] listOfFiles = baseFolder.listFiles();
ZipFile zipFile = null;
try {
zipFile = new ZipFile(baseFolder + "/" + listOfFiles[0].getName());
Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement();
String entryName = entry.getName();
if (entryName != null && entryName.contains("sets/")) {
if (!entryName.equalsIgnoreCase("sets/") && !entryName.contains("primitives") && !entryName.contains(".")) {
String[] names = entryName.split("/");
sets.add(names[1]);
}
}
}
} catch (IOException ioe) {
System.out.println("Error opening zip file" + ioe);
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (IOException ioe) {
System.out.println("Error while closing zip file" + ioe);
}
}
availableSets = new String[sets.size() + 1];
availableSets[0] = "*.* - All Wagic sets (thousands of cards)";
for (int i = 1; i < availableSets.length; i++) {
availableSets[i] = sets.get(i - 1) + " - " + ImgDownloader.getSetInfo(sets.get(i - 1), true, getSystemStorageLocation());
}
}
cardDownloader.setTitle("Which Set would you like to download?");
cardDownloader.setSingleChoiceItems(availableSets, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
set = availableSets[item].split(" - ")[0];
@@ -431,33 +454,71 @@ public class SDLActivity extends Activity implements OnKeyListener {
cardDownloader.create().show();
}
boolean error = false;
String res = "";
boolean dowloadInProgress = false;
ProgressDialog cardDownloader;
private void downloadCardImagesStart() {
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
cardDownloader.setTitle("Download of: " + set);
final Handler mHandler = new Handler();
cardDownloader = new ProgressDialog(this);
cardDownloader.setTitle("Downloading set: " + set);
cardDownloader.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
cardDownloader.setProgress(0);
cardDownloader.setMessage("Don't turn off phone or wi-fi/data connection and don't quit Wagic. The download could take several minutes, but you can hide this window and continue to play, a pop-up will notify the completion of process.");
cardDownloader.setMessage("IMPORTANT: After you press OK don't turn off phone or wi-fi/data connection and don't close Wagic.\nThe download process can take several minutes according to the number of images contained in the selected set.\nNOTE: if you choose *.* you may have to wait several hours!!!");
cardDownloader.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean error = false;
String res = "";
new Thread(new Runnable() {
public void run() {
try {
res = ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "sets/");
dowloadInProgress = true;
res = ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "sets/", cardDownloader);
} catch (Exception e) {
res = e.getMessage();
error = true;
}
downloadCardCompleted(error, res, set);
mHandler.post(new Runnable() {
public void run() {
downloadCardCompleted(error, res, set);
dowloadInProgress = false;
cardDownloader.dismiss();
}
});
}
}).start();
cardDownloader.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
cardDownloader.hide();
}
});
cardDownloader.setNegativeButton("Back", new DialogInterface.OnClickListener() {
cardDownloader.setButton(DialogInterface.BUTTON_NEGATIVE, "Stop Download", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mHandler.post(new Runnable() {
public void run() {
downloadCardInterruped(set, cardDownloader.getProgress(), cardDownloader.getMax());
dowloadInProgress = false;
cardDownloader.dismiss();
}
});
}
});
cardDownloader.show();
}
private void downloadCardInterruped(String set, int cardsDownloaded, int total) {
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
infoDialog.setTitle("Download of " + set + " has been interrupted!");
infoDialog.setMessage("WARNING: Only " + cardsDownloaded + " of " + total + " total cards have been downloaded and zip archive (" + set + ".zip) has not been created. You have to start the download again in order to complete the entire set.");
infoDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
downloadCardImages();
}
});
cardDownloader.create().show();
infoDialog.create().show();
}
private void downloadCardCompleted(boolean error, String res, String set) {
@@ -496,9 +557,17 @@ public class SDLActivity extends Activity implements OnKeyListener {
importDeckOptions();
} else if (itemId == 3) {
if (availableSets == null)
downloadCardInfo();
else
downloadCardImages();
loadAvailableSets();
else {
if (loadResInProgress) {
progressBarDialogRes.show();
progressBarDialogRes.show();
} else if (dowloadInProgress) {
cardDownloader.show();
cardDownloader.show();
} else
downloadCardImages();
}
} else if (itemId == 4) {
// display some info about the app
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);