Skip to content

Commit

Permalink
Downloader for Card Images now working on Android version with Option…
Browse files Browse the repository at this point in the history
…MenuButton (no zip)
  • Loading branch information
valfieri committed Aug 19, 2019
1 parent 7cb0d8b commit 73138d2
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 56 deletions.
Binary file added projects/mtg/Android/libs/armeabi-v7a/libSDL.so
Binary file not shown.
Binary file added projects/mtg/Android/libs/armeabi-v7a/libmain.so
Binary file not shown.
Binary file added projects/mtg/Android/libs/jsoup-1.11.3.jar
Binary file not shown.
147 changes: 100 additions & 47 deletions projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ private static String readLineByLineJava8(String filePath) {
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;
Expand All @@ -78,8 +79,12 @@ public static void DownloadCardImages(String set, String[] availableSets, String

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;
Expand All @@ -93,50 +98,45 @@ public static void DownloadCardImages(String set, String[] availableSets, String
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);
Expand Down Expand Up @@ -196,11 +196,54 @@ public static void DownloadCardImages(String set, String[] availableSets, String
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");
Expand Down Expand Up @@ -229,8 +272,8 @@ public static void DownloadCardImages(String set, String[] availableSets, String
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();
Expand Down Expand Up @@ -316,10 +359,10 @@ public static void DownloadCardImages(String set, String[] availableSets, String
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;
Expand All @@ -346,11 +389,13 @@ public static void DownloadCardImages(String set, String[] availableSets, String
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);
Expand Down Expand Up @@ -381,6 +426,14 @@ public static void DownloadCardImages(String set, String[] availableSets, String
}
}
}
}
/*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;
}
}
80 changes: 80 additions & 0 deletions projects/mtg/Android/src/net/wagic/utils/Zipper.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading

0 comments on commit 73138d2

Please sign in to comment.