Downloader for Card Images now working on Android version with OptionMenuButton (no zip)
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -54,9 +54,10 @@ public class ImgDownloader {
|
|||||||
return contentBuilder.toString();
|
return contentBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void 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) throws IOException {
|
||||||
|
String res = "";
|
||||||
String baseurl = "https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=";
|
|
||||||
|
String baseurl = "https://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=";
|
||||||
String imageurl = "https://scryfall.com/sets/";
|
String imageurl = "https://scryfall.com/sets/";
|
||||||
|
|
||||||
Integer ImgX = 0;
|
Integer ImgX = 0;
|
||||||
@@ -78,8 +79,12 @@ public class ImgDownloader {
|
|||||||
|
|
||||||
File baseFolder = new File(basePath);
|
File baseFolder = new File(basePath);
|
||||||
File[] listOfFiles = baseFolder.listFiles();
|
File[] listOfFiles = baseFolder.listFiles();
|
||||||
|
String currentSet = "";
|
||||||
for (int f = 1; f < availableSets.length; f++) {
|
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>();
|
Map<String, String> mappa = new HashMap<String, String>();
|
||||||
ZipFile zipFile = null;
|
ZipFile zipFile = null;
|
||||||
InputStream stream = null;
|
InputStream stream = null;
|
||||||
@@ -93,50 +98,45 @@ public class ImgDownloader {
|
|||||||
if(entryName.contains("sets/")){
|
if(entryName.contains("sets/")){
|
||||||
if(entryName.contains("_cards.dat")){
|
if(entryName.contains("_cards.dat")){
|
||||||
String[] names = entryName.split("/");
|
String[] names = entryName.split("/");
|
||||||
if(availableSets[f].equalsIgnoreCase(names[1])){
|
if(currentSet.equalsIgnoreCase(names[1])){
|
||||||
if(set.equalsIgnoreCase(names[1])){
|
stream = zipFile.getInputStream(entry);
|
||||||
stream = zipFile.getInputStream(entry);
|
byte[] buffer = new byte[1];
|
||||||
byte[] buffer = new byte[2048];
|
java.nio.file.Path outDir = Paths.get(basePath);
|
||||||
java.nio.file.Path outDir = Paths.get(basePath);
|
filePath = outDir.resolve("_cards.dat");
|
||||||
filePath = outDir.resolve("_cards.dat");
|
try {
|
||||||
try {
|
FileOutputStream fos = new FileOutputStream(filePath.toFile());
|
||||||
FileOutputStream fos = new FileOutputStream(filePath.toFile());
|
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
|
||||||
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
|
int len;
|
||||||
int len;
|
while ((len = stream.read(buffer)) != -1) {
|
||||||
while ((len = stream.read(buffer)) > 0) {
|
bos.write(buffer, 0, len);
|
||||||
bos.write(buffer, 0, len);
|
}
|
||||||
}
|
fos.close();
|
||||||
fos.close();
|
bos.close();
|
||||||
bos.close();
|
} catch (Exception ex) {
|
||||||
} catch (Exception ex) {}
|
System.out.println("Error extracting zip file" + ex);
|
||||||
|
}
|
||||||
|
if(!set.equalsIgnoreCase("*.*"))
|
||||||
f = availableSets.length;
|
f = availableSets.length;
|
||||||
break;
|
break;
|
||||||
} else if(set.equalsIgnoreCase("*.*")){
|
}
|
||||||
stream = zipFile.getInputStream(entry);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException ioe){
|
||||||
catch (IOException ioe) {
|
|
||||||
System.out.println("Error opening zip file" + ioe);
|
System.out.println("Error opening zip file" + ioe);
|
||||||
}
|
} finally {
|
||||||
finally {
|
|
||||||
try {
|
try {
|
||||||
if (zipFile!=null) {
|
if (zipFile!=null) {
|
||||||
zipFile.close();
|
zipFile.close();
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException ioe) {
|
||||||
catch (IOException ioe) {
|
|
||||||
System.out.println("Error while closing zip file" + 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());
|
String lines = readLineByLineJava8(filePath.toString());
|
||||||
|
File del = new File(filePath.toString());
|
||||||
|
del.delete();
|
||||||
int totalcards = 0;
|
int totalcards = 0;
|
||||||
String findStr = "total=";
|
String findStr = "total=";
|
||||||
int lastIndex = lines.indexOf(findStr);
|
int lastIndex = lines.indexOf(findStr);
|
||||||
@@ -196,11 +196,54 @@ public class ImgDownloader {
|
|||||||
String id = mappa.keySet().toArray()[y].toString();
|
String id = mappa.keySet().toArray()[y].toString();
|
||||||
Document doc = Jsoup.connect(baseurl + id).get();
|
Document doc = Jsoup.connect(baseurl + id).get();
|
||||||
Elements divs = doc.select("body div");
|
Elements divs = doc.select("body div");
|
||||||
try {
|
String scryset = currentSet;
|
||||||
doc = Jsoup.connect(imageurl + set.toLowerCase()).get();
|
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) {
|
} catch (Exception e) {
|
||||||
System.err.println("The SET: " + set + " has not been found on ScryFall!");
|
System.err.println("Problem downloading card: " + mappa.get(id) + " (" + id + ") from " + scryset + " on ScryFall");
|
||||||
y = mappa.size();
|
res = mappa.get(id) + "-" + currentSet + "/" + id + ".jpg\n" + res;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Elements imgs = doc.select("body img");
|
Elements imgs = doc.select("body img");
|
||||||
@@ -229,8 +272,8 @@ public class ImgDownloader {
|
|||||||
out.close();
|
out.close();
|
||||||
in.close();
|
in.close();
|
||||||
byte[] response = out.toByteArray();
|
byte[] response = out.toByteArray();
|
||||||
String cardimage = imgPath + "\\" + id + ".jpg";
|
String cardimage = imgPath + "/" + id + ".jpg";
|
||||||
String thumbcardimage = thumbPath + "\\" + id + ".jpg";
|
String thumbcardimage = thumbPath + "/" + id + ".jpg";
|
||||||
FileOutputStream fos = new FileOutputStream(cardimage);
|
FileOutputStream fos = new FileOutputStream(cardimage);
|
||||||
fos.write(response);
|
fos.write(response);
|
||||||
fos.close();
|
fos.close();
|
||||||
@@ -316,10 +359,10 @@ public class ImgDownloader {
|
|||||||
if (nametoken.isEmpty()) {
|
if (nametoken.isEmpty()) {
|
||||||
tokenfound = false;
|
tokenfound = false;
|
||||||
nametoken = mappa.get(id);
|
nametoken = mappa.get(id);
|
||||||
doc = Jsoup.connect(imageurl + set.toLowerCase()).get();
|
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
doc = Jsoup.connect(imageurl + "t" + set.toLowerCase()).get();
|
doc = Jsoup.connect(imageurl + "t" + scryset.toLowerCase()).get();
|
||||||
tokenfound = true;
|
tokenfound = true;
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
tokenfound = false;
|
tokenfound = false;
|
||||||
@@ -346,11 +389,13 @@ public class ImgDownloader {
|
|||||||
String tokenimage = "";
|
String tokenimage = "";
|
||||||
String tokenthumbimage = "";
|
String tokenthumbimage = "";
|
||||||
if (tokenfound) {
|
if (tokenfound) {
|
||||||
tokenimage = imgPath + "\\" + id + "t.jpg";
|
tokenimage = imgPath + "/" + id + "t.jpg";
|
||||||
tokenthumbimage = thumbPath + "\\" + id + "t.jpg";
|
tokenthumbimage = thumbPath + "/" + id + "t.jpg";
|
||||||
} else {
|
} else {
|
||||||
tokenimage = imgPath + "\\" + id + "_tocheck_t.jpg";
|
tokenimage = imgPath + "/" + id + "_tocheck_t.jpg";
|
||||||
tokenthumbimage = thumbPath + "\\" + 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);
|
FileOutputStream fos2 = new FileOutputStream(tokenimage);
|
||||||
fos2.write(responsetoken);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package org.libsdl.app;
|
|||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import android.os.StrictMode;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -401,7 +402,7 @@ public class SDLActivity extends Activity implements OnKeyListener
|
|||||||
String set = "";
|
String set = "";
|
||||||
String [] availableSets;
|
String [] availableSets;
|
||||||
Integer totalset = 0;
|
Integer totalset = 0;
|
||||||
|
|
||||||
private void downloadCardImages()
|
private void downloadCardImages()
|
||||||
{
|
{
|
||||||
AlertDialog.Builder cardDownloader = new AlertDialog.Builder(this);
|
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)
|
public void onClick(DialogInterface dialog, int item)
|
||||||
{
|
{
|
||||||
set = availableSets[item];
|
set = availableSets[item];
|
||||||
|
downloadCardStarting(set);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -456,25 +458,51 @@ public class SDLActivity extends Activity implements OnKeyListener
|
|||||||
{
|
{
|
||||||
public void onClick(DialogInterface dialog, int which)
|
public void onClick(DialogInterface dialog, int which)
|
||||||
{
|
{
|
||||||
try{
|
boolean error = false;
|
||||||
ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "/sets/");
|
String res = "";
|
||||||
|
try{
|
||||||
|
res = ImgDownloader.DownloadCardImages(set, availableSets, "HI", getSystemStorageLocation(), getUserStorageLocation() + "sets/");
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
e.printStackTrace();
|
res = e.getMessage();
|
||||||
|
error = true;
|
||||||
}
|
}
|
||||||
|
downloadCardCompleted(error, res, set);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
cardDownloader.create().show();
|
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
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu)
|
public boolean onCreateOptionsMenu(Menu menu)
|
||||||
{
|
{
|
||||||
SubMenu settingsMenu = menu.addSubMenu(Menu.NONE, 1, 1, "Settings");
|
SubMenu settingsMenu = menu.addSubMenu(Menu.NONE, 1, 1, "Settings");
|
||||||
menu.add(Menu.NONE, 2, 2, "Import Decks");
|
menu.add(Menu.NONE, 2, 2, "Import Decks");
|
||||||
menu.add(Menu.NONE, 3, 3, "About");
|
menu.add(Menu.NONE, 3, 3, "Download Cards");
|
||||||
//menu.add(Menu.NONE, 4, 4, "Download Cards");
|
menu.add(Menu.NONE, 4, 4, "About");
|
||||||
settingsMenu.add(kStorageDataOptionsMenuId, kStorageDataOptionsMenuId, Menu.NONE, "Storage Data Options");
|
settingsMenu.add(kStorageDataOptionsMenuId, kStorageDataOptionsMenuId, Menu.NONE, "Storage Data Options");
|
||||||
// buildStorageOptionsMenu(settingsMenu);
|
// buildStorageOptionsMenu(settingsMenu);
|
||||||
return true;
|
return true;
|
||||||
@@ -491,10 +519,10 @@ public class SDLActivity extends Activity implements OnKeyListener
|
|||||||
} else if (itemId == 2)
|
} else if (itemId == 2)
|
||||||
{
|
{
|
||||||
importDeckOptions();
|
importDeckOptions();
|
||||||
} else if (itemId == 4)
|
} else if (itemId == 3)
|
||||||
{
|
{
|
||||||
downloadCardImages();
|
downloadCardImages();
|
||||||
} else if (itemId == 3)
|
} else if (itemId == 4)
|
||||||
{
|
{
|
||||||
// display some info about the app
|
// display some info about the app
|
||||||
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
|
AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);
|
||||||
@@ -575,7 +603,8 @@ public class SDLActivity extends Activity implements OnKeyListener
|
|||||||
{
|
{
|
||||||
//Log.d(TAG, "onCreate()");
|
//Log.d(TAG, "onCreate()");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
||||||
|
StrictMode.setThreadPolicy(policy);
|
||||||
// So we can call stuff from static callbacks
|
// So we can call stuff from static callbacks
|
||||||
mSingleton = this;
|
mSingleton = this;
|
||||||
mContext = this.getApplicationContext();
|
mContext = this.getApplicationContext();
|
||||||
|
|||||||
Reference in New Issue
Block a user