Downloader for Card Images now working on Android version with OptionMenuButton (no zip)

This commit is contained in:
valfieri
2019-08-20 00:49:38 +02:00
parent 7cb0d8b843
commit 73138d2176
6 changed files with 218 additions and 56 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -54,9 +54,10 @@ public class ImgDownloader {
return contentBuilder.toString();
}
public static void DownloadCardImages(String set, String[] availableSets, String targetres, String basePath, String destinationPath) throws IOException {
String baseurl = "https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=";
public static String DownloadCardImages(String set, String[] availableSets, String targetres, String basePath, String destinationPath) throws IOException {
String res = "";
String baseurl = "https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=";
String imageurl = "https://scryfall.com/sets/";
Integer ImgX = 0;
@@ -78,8 +79,12 @@ public class ImgDownloader {
File baseFolder = new File(basePath);
File[] listOfFiles = baseFolder.listFiles();
String currentSet = "";
for (int f = 1; f < availableSets.length; f++) {
if(set.equalsIgnoreCase("*.*"))
currentSet = availableSets[f];
else
currentSet = set;
Map<String, String> mappa = new HashMap<String, String>();
ZipFile zipFile = null;
InputStream stream = null;
@@ -93,50 +98,45 @@ public class ImgDownloader {
if(entryName.contains("sets/")){
if(entryName.contains("_cards.dat")){
String[] names = entryName.split("/");
if(availableSets[f].equalsIgnoreCase(names[1])){
if(set.equalsIgnoreCase(names[1])){
stream = zipFile.getInputStream(entry);
byte[] buffer = new byte[2048];
java.nio.file.Path outDir = Paths.get(basePath);
filePath = outDir.resolve("_cards.dat");
try {
FileOutputStream fos = new FileOutputStream(filePath.toFile());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
int len;
while ((len = stream.read(buffer)) > 0) {
bos.write(buffer, 0, len);
}
fos.close();
bos.close();
} catch (Exception ex) {}
if(currentSet.equalsIgnoreCase(names[1])){
stream = zipFile.getInputStream(entry);
byte[] buffer = new byte[1];
java.nio.file.Path outDir = Paths.get(basePath);
filePath = outDir.resolve("_cards.dat");
try {
FileOutputStream fos = new FileOutputStream(filePath.toFile());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
int len;
while ((len = stream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
fos.close();
bos.close();
} catch (Exception ex) {
System.out.println("Error extracting zip file" + ex);
}
if(!set.equalsIgnoreCase("*.*"))
f = availableSets.length;
break;
} else if(set.equalsIgnoreCase("*.*")){
stream = zipFile.getInputStream(entry);
break;
}
}
break;
}
}
}
}
}
catch (IOException ioe) {
} catch (IOException ioe){
System.out.println("Error opening zip file" + ioe);
}
finally {
} finally {
try {
if (zipFile!=null) {
zipFile.close();
}
}
catch (IOException ioe) {
} catch (IOException ioe) {
System.out.println("Error while closing zip file" + ioe);
}
}
//File folder = new File(basePath + set + "\\");
//String filePath = folder.getAbsolutePath() + "\\_cards.dat";
String lines = readLineByLineJava8(filePath.toString());
File del = new File(filePath.toString());
del.delete();
int totalcards = 0;
String findStr = "total=";
int lastIndex = lines.indexOf(findStr);
@@ -196,11 +196,54 @@ public class ImgDownloader {
String id = mappa.keySet().toArray()[y].toString();
Document doc = Jsoup.connect(baseurl + id).get();
Elements divs = doc.select("body div");
try {
doc = Jsoup.connect(imageurl + set.toLowerCase()).get();
String scryset = currentSet;
if(scryset.equalsIgnoreCase("MRQ"))
scryset = "MMQ";
else if(scryset.equalsIgnoreCase("AVN"))
scryset = "DDH";
else if(scryset.equalsIgnoreCase("BVC"))
scryset = "DDQ";
else if(scryset.equalsIgnoreCase("CFX"))
scryset = "CON";
else if(scryset.equalsIgnoreCase("DM"))
scryset = "DKM";
else if(scryset.equalsIgnoreCase("EVK"))
scryset = "DDO";
else if(scryset.equalsIgnoreCase("EVT"))
scryset = "DDF";
else if(scryset.equalsIgnoreCase("FVD"))
scryset = "DRB";
else if(scryset.equalsIgnoreCase("FVE"))
scryset = "V09";
else if(scryset.equalsIgnoreCase("FVL"))
scryset = "V11";
else if(scryset.equalsIgnoreCase("FVR"))
scryset = "V10";
else if(scryset.equalsIgnoreCase("HVM"))
scryset = "DDL";
else if(scryset.equalsIgnoreCase("IVG"))
scryset = "DDJ";
else if(scryset.equalsIgnoreCase("JVV"))
scryset = "DDM";
else if(scryset.equalsIgnoreCase("KVD"))
scryset = "DDG";
else if(scryset.equalsIgnoreCase("PDS"))
scryset = "H09";
else if(scryset.equalsIgnoreCase("PVC"))
scryset = "DDE";
else if(scryset.equalsIgnoreCase("RV"))
scryset = "3ED";
else if(scryset.equalsIgnoreCase("SVT"))
scryset = "DDK";
else if(scryset.equalsIgnoreCase("VVK"))
scryset = "DDI";
else if(scryset.equalsIgnoreCase("ZVE"))
scryset = "DDP";
try {
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();
} catch (Exception e) {
System.err.println("The SET: " + set + " has not been found on ScryFall!");
y = mappa.size();
System.err.println("Problem downloading card: " + mappa.get(id) + " (" + id + ") from " + scryset + " on ScryFall");
res = mappa.get(id) + "-" + currentSet + "/" + id + ".jpg\n" + res;
continue;
}
Elements imgs = doc.select("body img");
@@ -229,8 +272,8 @@ public class ImgDownloader {
out.close();
in.close();
byte[] response = out.toByteArray();
String cardimage = imgPath + "\\" + id + ".jpg";
String thumbcardimage = thumbPath + "\\" + id + ".jpg";
String cardimage = imgPath + "/" + id + ".jpg";
String thumbcardimage = thumbPath + "/" + id + ".jpg";
FileOutputStream fos = new FileOutputStream(cardimage);
fos.write(response);
fos.close();
@@ -316,10 +359,10 @@ public class ImgDownloader {
if (nametoken.isEmpty()) {
tokenfound = false;
nametoken = mappa.get(id);
doc = Jsoup.connect(imageurl + set.toLowerCase()).get();
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();
} else {
try {
doc = Jsoup.connect(imageurl + "t" + set.toLowerCase()).get();
doc = Jsoup.connect(imageurl + "t" + scryset.toLowerCase()).get();
tokenfound = true;
} catch(Exception e) {
tokenfound = false;
@@ -346,11 +389,13 @@ public class ImgDownloader {
String tokenimage = "";
String tokenthumbimage = "";
if (tokenfound) {
tokenimage = imgPath + "\\" + id + "t.jpg";
tokenthumbimage = thumbPath + "\\" + id + "t.jpg";
tokenimage = imgPath + "/" + id + "t.jpg";
tokenthumbimage = thumbPath + "/" + id + "t.jpg";
} else {
tokenimage = imgPath + "\\" + id + "_tocheck_t.jpg";
tokenthumbimage = thumbPath + "\\" + id + "_tocheck_t.jpg";
tokenimage = imgPath + "/" + id + "_tocheck_t.jpg";
tokenthumbimage = thumbPath + "/" + id + "_tocheck_t.jpg";
System.err.println("Problem downloading token: " + nametoken + " (" + id + "t) from T" + scryset + " on ScryFall");
res = cardname + "-" + currentSet + "/" + id + "t.jpg\n" + res;
}
FileOutputStream fos2 = new FileOutputStream(tokenimage);
fos2.write(responsetoken);
@@ -381,6 +426,14 @@ public class ImgDownloader {
}
}
}
}
/*try {
Zipper appZip = new Zipper(destinationPath + set + "/");
appZip.generateFileList(new File(destinationPath + set + "/"));
appZip.zipIt(destinationPath + set + ".zip");
} catch (Exception e) {
e.printStackTrace();
}*/
}
return res;
}
}

View File

@@ -0,0 +1,80 @@
package net.wagic.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zipper {
private List <String> fileList;
private String SOURCE_FOLDER;
public Zipper(String source) {
fileList = new ArrayList < String > ();
SOURCE_FOLDER = source;
}
public void zipIt(String zipFile) {
byte[] buffer = new byte[1024];
String source = new File(SOURCE_FOLDER).getName();
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
zos.setLevel(ZipOutputStream.STORED);
System.out.println("Output to Zip : " + zipFile);
FileInputStream in = null;
for (String file: this.fileList) {
System.out.println("File Added : " + file);
ZipEntry ze = new ZipEntry(file);
zos.putNextEntry(ze);
try {
in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in .read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
} finally {
in.close();
}
}
zos.closeEntry();
System.out.println("Folder successfully compressed");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void generateFileList(File node) {
// add file only
if (node.isFile()) {
fileList.add(generateZipEntry(node.toString()));
}
if (node.isDirectory()) {
String[] subNote = node.list();
for (String filename: subNote) {
generateFileList(new File(node, filename));
}
}
}
private String generateZipEntry(String file) {
return file.substring(SOURCE_FOLDER.length(), file.length());
}
}

View File

@@ -3,6 +3,7 @@ package org.libsdl.app;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.Enumeration;
import android.os.StrictMode;
import java.io.BufferedInputStream;
import java.io.File;
@@ -401,7 +402,7 @@ public class SDLActivity extends Activity implements OnKeyListener
String set = "";
String [] availableSets;
Integer totalset = 0;
private void downloadCardImages()
{
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
@@ -449,6 +450,7 @@ public class SDLActivity extends Activity implements OnKeyListener
public void onClick(DialogInterface dialog, int item)
{
set = availableSets[item];
downloadCardStarting(set);
}
});
@@ -456,25 +458,51 @@ public class SDLActivity extends Activity implements OnKeyListener
{
public void onClick(DialogInterface dialog, int which)
{
try{
ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "/sets/");
boolean error = false;
String res = "";
try{
res = ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "sets/");
} catch(Exception e) {
e.printStackTrace();
res = e.getMessage();
error = true;
}
downloadCardCompleted(error, res, set);
}
});
cardDownloader.create().show();
}
private void downloadCardStarting(String set){
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
infoDialog.setTitle("You choose to Download: " + set);
infoDialog.setMessage("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 (NOTE: if you choose *.* you will have to wait several hours!!!)");
infoDialog.create().show();
}
private void downloadCardCompleted(boolean error, String res, String set){
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
if(!error){
infoDialog.setTitle("Download Completed: " + set);
if(!res.isEmpty())
infoDialog.setMessage("Following cards could not be downloaded:\n" + res);
else
infoDialog.setMessage("All the cards have been successfully downloaded");
} else {
infoDialog.setTitle("Error downloading: " + set);
infoDialog.setMessage(res);
}
infoDialog.create().show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
SubMenu settingsMenu = menu.addSubMenu(Menu.NONE, 1, 1, "Settings");
menu.add(Menu.NONE, 2, 2, "Import Decks");
menu.add(Menu.NONE, 3, 3, "About");
//menu.add(Menu.NONE, 4, 4, "Download Cards");
menu.add(Menu.NONE, 3, 3, "Download Cards");
menu.add(Menu.NONE, 4, 4, "About");
settingsMenu.add(kStorageDataOptionsMenuId, kStorageDataOptionsMenuId, Menu.NONE, "Storage Data Options");
// buildStorageOptionsMenu(settingsMenu);
return true;
@@ -491,10 +519,10 @@ public class SDLActivity extends Activity implements OnKeyListener
} else if (itemId == 2)
{
importDeckOptions();
} else if (itemId == 4)
} else if (itemId == 3)
{
downloadCardImages();
} else if (itemId == 3)
} else if (itemId == 4)
{
// display some info about the app
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
@@ -575,7 +603,8 @@ public class SDLActivity extends Activity implements OnKeyListener
{
//Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// So we can call stuff from static callbacks
mSingleton = this;
mContext = this.getApplicationContext();