Added retries on image and database file download for Android Downloader program.

This commit is contained in:
valfieri
2019-09-02 19:52:46 +02:00
parent 1749330e8a
commit e856538259
2 changed files with 227 additions and 23 deletions
@@ -68,17 +68,23 @@ public class ImgDownloader {
static HashMap<String, HashMap<String, String>> database; static HashMap<String, HashMap<String, String>> database;
public static void loadDatabase(String path) { public static boolean loadDatabase(String path) {
database = new HashMap<String, HashMap<String, String>>(); database = new HashMap<String, HashMap<String, String>>();
try { try {
String databaseurl = "https://github.com/Vitty85/wagic/releases/download/wagic-v0.21.1/CardImageLinks.csv"; String databaseurl = "https://github.com/Vitty85/wagic/releases/download/wagic-v0.21.1/CardImageLinks.csv";
URL url = new URL(databaseurl); URL url = new URL(databaseurl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
if (httpcon == null) { if (httpcon == null) {
System.err.println("Error: Problem downloading or initializing database file, i will use the slow method...");
database = null; database = null;
return; return false;
} }
httpcon.addRequestProperty("User-Agent", "Mozilla/4.76"); httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
httpcon.setConnectTimeout(30000);
httpcon.setReadTimeout(30000);
httpcon.setAllowUserInteraction(false);
httpcon.setDoInput(true);
httpcon.setDoOutput(false);
InputStream in; InputStream in;
try { try {
in = new BufferedInputStream(httpcon.getInputStream()); in = new BufferedInputStream(httpcon.getInputStream());
@@ -89,20 +95,52 @@ public class ImgDownloader {
try { try {
in = new BufferedInputStream(httpcon.getInputStream()); in = new BufferedInputStream(httpcon.getInputStream());
} catch (Exception ex3) { } catch (Exception ex3) {
System.err.println("Error: Problem downloading or initializing database file, i will use the slow method...");
database = null; database = null;
return; return false;
} }
} }
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int n = 0; int n = 0;
while (-1 != (n = in.read(buf))) { long millis = System.currentTimeMillis();
boolean timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n); out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 30000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Timeout downloading database file, i will retry 2 times more...");
buf = new byte[1024];
n = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 30000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Timeout downloading database file, i will retry 1 time more...");
buf = new byte[1024];
n = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 30000)
timeout = true;
}
}
} }
out.close(); out.close();
in.close(); in.close();
if (timeout) {
System.err.println("Error: Timeout downloading database file, i will use the slow method...");
return false;
}
byte[] response = out.toByteArray(); byte[] response = out.toByteArray();
String databasepath = path + File.separator + "CardImageLinks.csv"; String databasepath = path + File.separator + "CardImageLinks.csv";
FileOutputStream fos = new FileOutputStream(databasepath); FileOutputStream fos = new FileOutputStream(databasepath);
@@ -120,8 +158,11 @@ public class ImgDownloader {
File del = new File(databasepath); File del = new File(databasepath);
del.delete(); del.delete();
} catch (Exception e) { } catch (Exception e) {
System.err.println("Error: Problem downloading or initializing database file, i will use the slow method...");
database = null; database = null;
return false;
} }
return true;
} }
public static boolean fastDownloadCard(String set, String id, String name, String imgPath, String thumbPath, int ImgX, int ImgY, int ThumbX, int ThumbY) { public static boolean fastDownloadCard(String set, String id, String name, String imgPath, String thumbPath, int ImgX, int ImgY, int ThumbX, int ThumbY) {
@@ -178,7 +219,7 @@ public class ImgDownloader {
timeout = true; timeout = true;
} }
if (timeout) { if (timeout) {
System.out.println("Warning: Timeout downloading token: " + id + "t.jpg from " + imageurl + ", i will retry 2 times more..."); System.out.println("Warning: Problem downloading card: " + name + " (" + id + ".jpg) from " + imageurl + ", i will retry 2 times more...");
buf = new byte[1024]; buf = new byte[1024];
n = 0; n = 0;
millis = System.currentTimeMillis(); millis = System.currentTimeMillis();
@@ -189,7 +230,7 @@ public class ImgDownloader {
timeout = true; timeout = true;
} }
if (timeout) { if (timeout) {
System.out.println("Warning: Timeout downloading token: " + id + "t.jpg from " + imageurl + ", i will retry 1 time more..."); System.out.println("Warning: Problem downloading card: " + name + " (" + id + ".jpg) from " + imageurl + ", i will retry 1 time more...");
buf = new byte[1024]; buf = new byte[1024];
n = 0; n = 0;
millis = System.currentTimeMillis(); millis = System.currentTimeMillis();
@@ -199,12 +240,12 @@ public class ImgDownloader {
if (System.currentTimeMillis() - millis > 10000) if (System.currentTimeMillis() - millis > 10000)
timeout = true; timeout = true;
} }
if (timeout) {
System.out.println("Warning: Timeout downloading token: " + id + "t.jpg from " + imageurl + ", i will try with slow method...");
return false;
}
} }
} }
if (timeout) {
System.out.println("Warning: Problem downloading card: " + name + " (" + id + ".jpg) from " + imageurl + ", i will try with slow method...");
return false;
}
out.close(); out.close();
in.close(); in.close();
byte[] response = out.toByteArray(); byte[] response = out.toByteArray();
@@ -275,11 +316,43 @@ public class ImgDownloader {
ByteArrayOutputStream outtoken = new ByteArrayOutputStream(); ByteArrayOutputStream outtoken = new ByteArrayOutputStream();
byte[] buftoken = new byte[1024]; byte[] buftoken = new byte[1024];
int ntoken = 0; int ntoken = 0;
while (-1 != (ntoken = intoken.read(buftoken))) { long millis = System.currentTimeMillis();
boolean timeout = false;
while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) {
outtoken.write(buftoken, 0, ntoken); outtoken.write(buftoken, 0, ntoken);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Problem downloading token: " + id + "t.jpg from " + imageurl + ", i will retry 2 times more...");
buftoken = new byte[1024];
ntoken = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) {
outtoken.write(buftoken, 0, ntoken);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Problem downloading token: " + id + "t.jpg from " + imageurl + ", i will retry 1 time more...");
buftoken = new byte[1024];
ntoken = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) {
outtoken.write(buftoken, 0, ntoken);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
}
} }
outtoken.close(); outtoken.close();
intoken.close(); intoken.close();
if (timeout) {
System.out.println("Warning: Problem downloading token: " + id + "t.jpg from " + imageurl + ", i will try with slow method...");
return false;
}
byte[] responsetoken = outtoken.toByteArray(); byte[] responsetoken = outtoken.toByteArray();
String tokenimage = imgPath + File.separator + id + "t.jpg"; String tokenimage = imgPath + File.separator + id + "t.jpg";
String tokenthumbimage = thumbPath + File.separator + id + "t.jpg"; String tokenthumbimage = thumbPath + File.separator + id + "t.jpg";
@@ -1454,11 +1527,43 @@ public class ImgDownloader {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int n = 0; int n = 0;
while (-1 != (n = in.read(buf))) { long millis = System.currentTimeMillis();
boolean timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n); out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg), i will retry 2 times more...");
buf = new byte[1024];
n = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg), i will retry 1 time more...");
buf = new byte[1024];
n = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
}
} }
out.close(); out.close();
in.close(); in.close();
if (timeout) {
System.err.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg) from, i will not retry anymore...");
break;
}
byte[] response = out.toByteArray(); byte[] response = out.toByteArray();
String cardimage = imgPath + File.separator + id + ".jpg"; String cardimage = imgPath + File.separator + id + ".jpg";
String thumbcardimage = thumbPath + File.separator + id + ".jpg"; String thumbcardimage = thumbPath + File.separator + id + ".jpg";
@@ -1471,8 +1576,9 @@ public class ImgDownloader {
FileOutputStream fout = new FileOutputStream(cardimage); FileOutputStream fout = new FileOutputStream(cardimage);
resized.compress(Bitmap.CompressFormat.JPEG, 100, fout); resized.compress(Bitmap.CompressFormat.JPEG, 100, fout);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Warning: Problem resizing card: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); System.err.println("Error: Problem resizing card: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted...");
res = mappa.get(id) + " - " + set + File.separator + id + ".jpg\n" + res; res = mappa.get(id) + " - " + set + File.separator + id + ".jpg\n" + res;
break;
} }
try { try {
Bitmap yourBitmapthumb = BitmapFactory.decodeFile(cardimage); Bitmap yourBitmapthumb = BitmapFactory.decodeFile(cardimage);
@@ -1480,8 +1586,9 @@ public class ImgDownloader {
FileOutputStream fout = new FileOutputStream(thumbcardimage); FileOutputStream fout = new FileOutputStream(thumbcardimage);
resizedThumb.compress(Bitmap.CompressFormat.JPEG, 100, fout); resizedThumb.compress(Bitmap.CompressFormat.JPEG, 100, fout);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Warning: Problem resizing card thumbnail: " + mappa.get(id) + " (" + id + ".jpg, image may be corrupted..."); System.err.println("Error: Problem resizing card thumbnail: " + mappa.get(id) + " (" + id + ".jpg, image may be corrupted...");
res = mappa.get(id) + " - " + set + File.separator + "thumbnails" + File.separator + id + ".jpg\n" + res; res = mappa.get(id) + " - " + set + File.separator + "thumbnails" + File.separator + id + ".jpg\n" + res;
break;
} }
continue; continue;
} }
@@ -1733,11 +1840,43 @@ public class ImgDownloader {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int n = 0; int n = 0;
while (-1 != (n = in.read(buf))) { long millis = System.currentTimeMillis();
boolean timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n); out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg), i will retry 2 times more...");
buf = new byte[1024];
n = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg), i will retry 1 time more...");
buf = new byte[1024];
n = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (n = in.read(buf)) && !timeout) {
out.write(buf, 0, n);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
}
} }
out.close(); out.close();
in.close(); in.close();
if (timeout) {
System.err.println("Error: Problem downloading card: " + mappa.get(id) + " (" + id + ".jpg) from, i will not retry anymore...");
break;
}
byte[] response = out.toByteArray(); byte[] response = out.toByteArray();
String cardimage = imgPath + File.separator + id + ".jpg"; String cardimage = imgPath + File.separator + id + ".jpg";
String thumbcardimage = thumbPath + File.separator + id + ".jpg"; String thumbcardimage = thumbPath + File.separator + id + ".jpg";
@@ -1750,8 +1889,9 @@ public class ImgDownloader {
FileOutputStream fout = new FileOutputStream(cardimage); FileOutputStream fout = new FileOutputStream(cardimage);
resized.compress(Bitmap.CompressFormat.JPEG, 100, fout); resized.compress(Bitmap.CompressFormat.JPEG, 100, fout);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Warning: Problem resizing card: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); System.err.println("Error: Problem resizing card: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted...");
res = mappa.get(id) + " - " + set + File.separator + id + ".jpg\n" + res; res = mappa.get(id) + " - " + set + File.separator + id + ".jpg\n" + res;
break;
} }
try { try {
Bitmap yourBitmapthumb = BitmapFactory.decodeFile(cardimage); Bitmap yourBitmapthumb = BitmapFactory.decodeFile(cardimage);
@@ -1759,8 +1899,9 @@ public class ImgDownloader {
FileOutputStream fout = new FileOutputStream(thumbcardimage); FileOutputStream fout = new FileOutputStream(thumbcardimage);
resizedThumb.compress(Bitmap.CompressFormat.JPEG, 100, fout); resizedThumb.compress(Bitmap.CompressFormat.JPEG, 100, fout);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Warning: Problem resizing card thumbnail: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted..."); System.err.println("Error: Problem resizing card thumbnail: " + mappa.get(id) + " (" + id + ".jpg), image may be corrupted...");
res = mappa.get(id) + " - " + set + File.separator + "thumbnails" + File.separator + id + ".jpg\n" + res; res = mappa.get(id) + " - " + set + File.separator + "thumbnails" + File.separator + id + ".jpg\n" + res;
break;
} }
String text = ""; String text = "";
for (k = 0; k < divs.size(); k++) for (k = 0; k < divs.size(); k++)
@@ -2047,11 +2188,43 @@ public class ImgDownloader {
ByteArrayOutputStream outtoken = new ByteArrayOutputStream(); ByteArrayOutputStream outtoken = new ByteArrayOutputStream();
byte[] buftoken = new byte[1024]; byte[] buftoken = new byte[1024];
int ntoken = 0; int ntoken = 0;
while (-1 != (ntoken = intoken.read(buftoken))) { millis = System.currentTimeMillis();
timeout = false;
while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) {
outtoken.write(buftoken, 0, ntoken); outtoken.write(buftoken, 0, ntoken);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Problem downloading token: " + id + "t.jpg from, i will retry 2 times more...");
buftoken = new byte[1024];
ntoken = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) {
outtoken.write(buftoken, 0, ntoken);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
if (timeout) {
System.out.println("Warning: Problem downloading token: " + id + "t.jpg from, i will retry 1 time more...");
buftoken = new byte[1024];
ntoken = 0;
millis = System.currentTimeMillis();
timeout = false;
while (-1 != (ntoken = intoken.read(buftoken)) && !timeout) {
outtoken.write(buftoken, 0, ntoken);
if (System.currentTimeMillis() - millis > 10000)
timeout = true;
}
}
} }
outtoken.close(); outtoken.close();
intoken.close(); intoken.close();
if (timeout) {
System.err.println("Error: Problem downloading token: " + id + "t.jpg from, i will not retry anymore...");
break;
}
byte[] responsetoken = outtoken.toByteArray(); byte[] responsetoken = outtoken.toByteArray();
String tokenimage = imgPath + File.separator + id + "t.jpg"; String tokenimage = imgPath + File.separator + id + "t.jpg";
String tokenthumbimage = thumbPath + File.separator + id + "t.jpg"; String tokenthumbimage = thumbPath + File.separator + id + "t.jpg";
@@ -2068,8 +2241,9 @@ public class ImgDownloader {
FileOutputStream fout = new FileOutputStream(tokenimage); FileOutputStream fout = new FileOutputStream(tokenimage);
resizedToken.compress(Bitmap.CompressFormat.JPEG, 100, fout); resizedToken.compress(Bitmap.CompressFormat.JPEG, 100, fout);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Warning: Problem resizing token: " + id + "t.jpg, image may be corrupted..."); System.err.println("Error: Problem resizing token: " + id + "t.jpg, image may be corrupted...");
res = nametoken + " - " + set + File.separator + "thumbnails" + File.separator + id + "t.jpg\n" + res; res = nametoken + " - " + set + File.separator + "thumbnails" + File.separator + id + "t.jpg\n" + res;
break;
} }
try { try {
Bitmap yourBitmapTokenthumb = BitmapFactory.decodeFile(tokenimage); Bitmap yourBitmapTokenthumb = BitmapFactory.decodeFile(tokenimage);
@@ -2077,8 +2251,9 @@ public class ImgDownloader {
FileOutputStream fout = new FileOutputStream(tokenthumbimage); FileOutputStream fout = new FileOutputStream(tokenthumbimage);
resizedThumbToken.compress(Bitmap.CompressFormat.JPEG, 100, fout); resizedThumbToken.compress(Bitmap.CompressFormat.JPEG, 100, fout);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Warning: Problem resizing token thumbnail: " + id + "t.jpg, image may be corrupted..."); System.err.println("Error: Problem resizing token thumbnail: " + id + "t.jpg, image may be corrupted...");
res = nametoken + " - " + set + File.separator + "thumbnails" + File.separator + id + "t.jpg\n" + res; res = nametoken + " - " + set + File.separator + "thumbnails" + File.separator + id + "t.jpg\n" + res;
break;
} }
break; break;
} }
@@ -368,6 +368,7 @@ public class SDLActivity extends Activity implements OnKeyListener {
boolean finished2 = false; boolean finished2 = false;
boolean loadResInProgress = false; boolean loadResInProgress = false;
ProgressDialog progressBarDialogRes; ProgressDialog progressBarDialogRes;
boolean fast = false;
private void loadAvailableSets() { private void loadAvailableSets() {
final Handler mHandler = new Handler(); final Handler mHandler = new Handler();
@@ -429,7 +430,12 @@ public class SDLActivity extends Activity implements OnKeyListener {
} }
} }
selectedSets = new ArrayList<String>(); selectedSets = new ArrayList<String>();
downloadCardImages(); if(!fast) {
showWarningFast();
}
else {
downloadCardImages();
}
} }
}); });
} }
@@ -437,14 +443,37 @@ public class SDLActivity extends Activity implements OnKeyListener {
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
ImgDownloader.loadDatabase(getSystemStorageLocation()); fast = ImgDownloader.loadDatabase(getSystemStorageLocation());
finished2 = true; finished2 = true;
} }
}).start(); }).start();
progressBarDialogRes.show(); progressBarDialogRes.show();
}
private void showWarningFast() {
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
infoDialog.setTitle("Problem downloading the images database file");
infoDialog.setMessage("The program will use the slow method (not indexed), so the images download may take really long time...");
infoDialog.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
downloadCardImages();
}
});
infoDialog.setNegativeButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
fast = ImgDownloader.loadDatabase(getSystemStorageLocation());
if(fast){
downloadCardImages();
} else {
showWarningFast();
}
}
});
infoDialog.create().show();
} }
private void downloadCardImages() { private void downloadCardImages() {