Fix on Android downloader and code indent

This commit is contained in:
valfieri
2019-08-23 12:58:10 +02:00
parent 30622010df
commit 9dfd513ad3
2 changed files with 550 additions and 712 deletions
@@ -4,9 +4,11 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
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 net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionMethod; import net.lingala.zip4j.model.enums.CompressionMethod;
@@ -35,7 +37,8 @@ public class ImgDownloader {
break; break;
out.append(buffer, 0, rsz); out.append(buffer, 0, rsz);
} }
} catch (Exception e) {} } catch (Exception e) {
}
return out.toString(); return out.toString();
} }
@@ -56,32 +59,32 @@ public class ImgDownloader {
return contentBuilder.toString(); return contentBuilder.toString();
} }
public static String getSetInfo(String setName, boolean zipped, String path){ public static String getSetInfo(String setName, boolean zipped, String path) {
String cardsfilepath = ""; String cardsfilepath = "";
boolean todelete = false; boolean todelete = false;
if(zipped){ if (zipped) {
File resFolder = new File(path + File.separator); File resFolder = new File(path + File.separator);
File [] listOfFile = resFolder.listFiles(); File[] listOfFile = resFolder.listFiles();
ZipFile zipFile = null; ZipFile zipFile = null;
InputStream stream = null; InputStream stream = null;
java.nio.file.Path filePath = null; java.nio.file.Path filePath = null;
try { try {
for (int i = 0; i < listOfFile.length; i++){ for (int i = 0; i < listOfFile.length; i++) {
if (listOfFile[i].getName().contains(".zip")){ if (listOfFile[i].getName().contains(".zip")) {
zipFile = new ZipFile(path + File.separator + listOfFile[i].getName()); zipFile = new ZipFile(path + File.separator + listOfFile[i].getName());
break; break;
} }
} }
if(zipFile == null) if (zipFile == null)
return ""; return "";
Enumeration<? extends ZipEntry> e = zipFile.entries(); Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement(); ZipEntry entry = e.nextElement();
String entryName = entry.getName(); String entryName = entry.getName();
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(setName.equalsIgnoreCase(names[1])){ if (setName.equalsIgnoreCase(names[1])) {
stream = zipFile.getInputStream(entry); stream = zipFile.getInputStream(entry);
byte[] buffer = new byte[1]; byte[] buffer = new byte[1];
java.nio.file.Path outDir = Paths.get(path + File.separator); java.nio.file.Path outDir = Paths.get(path + File.separator);
@@ -97,26 +100,28 @@ public class ImgDownloader {
bos.close(); bos.close();
cardsfilepath = filePath.toString(); cardsfilepath = filePath.toString();
todelete = true; todelete = true;
} catch (Exception ex) {} } catch (Exception ex) {
}
break; break;
} }
} }
} }
} }
} catch (IOException ioe){ } } catch (IOException ioe) {
finally { } finally {
try { try {
if (zipFile!=null) { if (zipFile != null) {
zipFile.close(); zipFile.close();
} }
} catch (IOException ioe) {} } catch (IOException ioe) {
}
} }
} else { } else {
File setFolder = new File(path + File.separator + "sets" + File.separator + setName + File.separator); File setFolder = new File(path + File.separator + "sets" + File.separator + setName + File.separator);
cardsfilepath = setFolder.getAbsolutePath() + File.separator + "_cards.dat"; cardsfilepath = setFolder.getAbsolutePath() + File.separator + "_cards.dat";
} }
String lines = readLineByLineJava8(cardsfilepath); String lines = readLineByLineJava8(cardsfilepath);
if(todelete) { if (todelete) {
File del = new File(cardsfilepath); File del = new File(cardsfilepath);
del.delete(); del.delete();
} }
@@ -131,48 +136,52 @@ public class ImgDownloader {
return name + " (" + totalcards + " cards)"; return name + " (" + totalcards + " cards)";
} }
public static Document findTokenPage(String imageurl, String name, String set, String [] availableSets, String tokenstats) throws Exception { public static Document findTokenPage(String imageurl, String name, String set, String[] availableSets, String tokenstats) throws Exception {
Document doc = null; Document doc = null;
Elements outlinks = null; Elements outlinks = null;
try { try {
doc = Jsoup.connect(imageurl + "t" + set.toLowerCase()).get(); doc = Jsoup.connect(imageurl + "t" + set.toLowerCase()).get();
outlinks = doc.select("body a"); outlinks = doc.select("body a");
for (int k = 0; k < outlinks.size(); k++){ for (int k = 0; k < outlinks.size(); k++) {
String linktoken = outlinks.get(k).attributes().get("href"); String linktoken = outlinks.get(k).attributes().get("href");
try { try {
Document tokendoc = Jsoup.connect(linktoken).get(); Document tokendoc = Jsoup.connect(linktoken).get();
Elements stats = tokendoc.select("head meta"); Elements stats = tokendoc.select("head meta");
for (int j = 0; j < stats.size(); j++){ for (int j = 0; j < stats.size(); j++) {
String a = stats.get(j).attributes().get("content"); 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())){ if (stats.get(j).attributes().get("content").contains(tokenstats) && stats.get(j).attributes().get("content").toLowerCase().contains(name.toLowerCase())) {
return tokendoc; return tokendoc;
} }
} }
} catch (Exception e) {} } catch (Exception e) {
}
}
} catch (Exception e) {
} }
} catch (Exception e){}
System.out.println("Warning: Token " + name + " has not been found between " + set + " tokens, i will search for it between any other set..."); System.out.println("Warning: Token " + name + " has not been found between " + set + " tokens, i will search for it between any other set...");
for (int i = 1; i < availableSets.length; i++){ for (int i = 1; i < availableSets.length; i++) {
String currentSet = availableSets[i].toLowerCase().split(" - ")[0]; String currentSet = availableSets[i].toLowerCase().split(" - ")[0];
if(!currentSet.equalsIgnoreCase(set)){ if (!currentSet.equalsIgnoreCase(set)) {
try { try {
doc = Jsoup.connect(imageurl + "t" + currentSet).get(); doc = Jsoup.connect(imageurl + "t" + currentSet).get();
outlinks = doc.select("body a"); outlinks = doc.select("body a");
for (int k = 0; k < outlinks.size(); k++){ for (int k = 0; k < outlinks.size(); k++) {
String linktoken = outlinks.get(k).attributes().get("href"); String linktoken = outlinks.get(k).attributes().get("href");
try { try {
Document tokendoc = Jsoup.connect(linktoken).get(); Document tokendoc = Jsoup.connect(linktoken).get();
Elements stats = tokendoc.select("head meta"); Elements stats = tokendoc.select("head meta");
for (int j = 0; j < stats.size(); j++){ for (int j = 0; j < stats.size(); j++) {
String a = stats.get(j).attributes().get("content"); 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())){ if (stats.get(j).attributes().get("content").contains(tokenstats) && stats.get(j).attributes().get("content").toLowerCase().contains(name.toLowerCase())) {
System.out.println("Token " + name + " has been found between " + currentSet.toUpperCase() + " tokens, i will use this one"); System.out.println("Token " + name + " has been found between " + currentSet.toUpperCase() + " tokens, i will use this one");
return tokendoc; return tokendoc;
} }
} }
} catch (Exception e) {} } catch (Exception e) {
}
}
} catch (Exception e) {
} }
} catch (Exception e) {}
} }
} }
System.err.println("Error: Token " + name + " has not been found between any set of " + imageurl); System.err.println("Error: Token " + name + " has not been found between any set of " + imageurl);
@@ -206,7 +215,7 @@ public class ImgDownloader {
File[] listOfFiles = baseFolder.listFiles(); File[] listOfFiles = baseFolder.listFiles();
String currentSet = ""; String currentSet = "";
for (int f = 1; f < availableSets.length; f++) { for (int f = 1; f < availableSets.length; f++) {
if(set.equalsIgnoreCase("*.*")) if (set.equalsIgnoreCase("*.*"))
currentSet = availableSets[f]; currentSet = availableSets[f];
else else
currentSet = set; currentSet = set;
@@ -220,10 +229,10 @@ public class ImgDownloader {
while (e.hasMoreElements()) { while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement(); ZipEntry entry = e.nextElement();
String entryName = entry.getName(); String entryName = entry.getName();
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(currentSet.equalsIgnoreCase(names[1])){ if (currentSet.equalsIgnoreCase(names[1])) {
stream = zipFile.getInputStream(entry); stream = zipFile.getInputStream(entry);
byte[] buffer = new byte[1]; byte[] buffer = new byte[1];
java.nio.file.Path outDir = Paths.get(basePath); java.nio.file.Path outDir = Paths.get(basePath);
@@ -240,18 +249,18 @@ public class ImgDownloader {
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Error extracting zip file" + ex); System.out.println("Error extracting zip file" + ex);
} }
if(!set.equalsIgnoreCase("*.*")) if (!set.equalsIgnoreCase("*.*"))
f = availableSets.length; f = availableSets.length;
break; 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) {
@@ -267,19 +276,19 @@ public class ImgDownloader {
int lastIndex = lines.indexOf(findStr); int lastIndex = lines.indexOf(findStr);
String totals = lines.substring(lastIndex, lines.indexOf("\n", lastIndex)); String totals = lines.substring(lastIndex, lines.indexOf("\n", lastIndex));
totalcards = Integer.parseInt(totals.split("=")[1]); totalcards = Integer.parseInt(totals.split("=")[1]);
for (int i = 0; i < totalcards; i++) { while (lines.contains("[card]")) {
findStr = "[card]"; findStr = "[card]";
lastIndex = lines.indexOf(findStr); lastIndex = lines.indexOf(findStr);
String id = null; String id = null;
String primitive = null; String primitive = null;
int a = lines.indexOf("primitive=",lastIndex); int a = lines.indexOf("primitive=", lastIndex);
if(a > 0) if (a > 0)
primitive = lines.substring(a, lines.indexOf("\n",a)).replace("//", "-").split("=")[1]; primitive = lines.substring(a, lines.indexOf("\n", a)).replace("//", "-").split("=")[1];
int b = lines.indexOf("id=",lastIndex); int b = lines.indexOf("id=", lastIndex);
if(b > 0) if (b > 0)
id = lines.substring(b, lines.indexOf("\n",b)).replace("-", "").split("=")[1]; id = lines.substring(b, lines.indexOf("\n", b)).replace("-", "").split("=")[1];
int c = lines.indexOf("[/card]",lastIndex); int c = lines.indexOf("[/card]", lastIndex);
if(c > 0) if (c > 0)
lines = lines.substring(c + 8); lines = lines.substring(c + 8);
if (primitive != null && id != null && !id.equalsIgnoreCase("null")) if (primitive != null && id != null && !id.equalsIgnoreCase("null"))
mappa.put(id, primitive); mappa.put(id, primitive);
@@ -322,47 +331,47 @@ public class ImgDownloader {
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");
String scryset = currentSet; String scryset = currentSet;
if(scryset.equalsIgnoreCase("MRQ")) if (scryset.equalsIgnoreCase("MRQ"))
scryset = "MMQ"; scryset = "MMQ";
else if(scryset.equalsIgnoreCase("AVN")) else if (scryset.equalsIgnoreCase("AVN"))
scryset = "DDH"; scryset = "DDH";
else if(scryset.equalsIgnoreCase("BVC")) else if (scryset.equalsIgnoreCase("BVC"))
scryset = "DDQ"; scryset = "DDQ";
else if(scryset.equalsIgnoreCase("CFX")) else if (scryset.equalsIgnoreCase("CFX"))
scryset = "CON"; scryset = "CON";
else if(scryset.equalsIgnoreCase("DM")) else if (scryset.equalsIgnoreCase("DM"))
scryset = "DKM"; scryset = "DKM";
else if(scryset.equalsIgnoreCase("EVK")) else if (scryset.equalsIgnoreCase("EVK"))
scryset = "DDO"; scryset = "DDO";
else if(scryset.equalsIgnoreCase("EVT")) else if (scryset.equalsIgnoreCase("EVT"))
scryset = "DDF"; scryset = "DDF";
else if(scryset.equalsIgnoreCase("FVD")) else if (scryset.equalsIgnoreCase("FVD"))
scryset = "DRB"; scryset = "DRB";
else if(scryset.equalsIgnoreCase("FVE")) else if (scryset.equalsIgnoreCase("FVE"))
scryset = "V09"; scryset = "V09";
else if(scryset.equalsIgnoreCase("FVL")) else if (scryset.equalsIgnoreCase("FVL"))
scryset = "V11"; scryset = "V11";
else if(scryset.equalsIgnoreCase("FVR")) else if (scryset.equalsIgnoreCase("FVR"))
scryset = "V10"; scryset = "V10";
else if(scryset.equalsIgnoreCase("HVM")) else if (scryset.equalsIgnoreCase("HVM"))
scryset = "DDL"; scryset = "DDL";
else if(scryset.equalsIgnoreCase("IVG")) else if (scryset.equalsIgnoreCase("IVG"))
scryset = "DDJ"; scryset = "DDJ";
else if(scryset.equalsIgnoreCase("JVV")) else if (scryset.equalsIgnoreCase("JVV"))
scryset = "DDM"; scryset = "DDM";
else if(scryset.equalsIgnoreCase("KVD")) else if (scryset.equalsIgnoreCase("KVD"))
scryset = "DDG"; scryset = "DDG";
else if(scryset.equalsIgnoreCase("PDS")) else if (scryset.equalsIgnoreCase("PDS"))
scryset = "H09"; scryset = "H09";
else if(scryset.equalsIgnoreCase("PVC")) else if (scryset.equalsIgnoreCase("PVC"))
scryset = "DDE"; scryset = "DDE";
else if(scryset.equalsIgnoreCase("RV")) else if (scryset.equalsIgnoreCase("RV"))
scryset = "3ED"; scryset = "3ED";
else if(scryset.equalsIgnoreCase("SVT")) else if (scryset.equalsIgnoreCase("SVT"))
scryset = "DDK"; scryset = "DDK";
else if(scryset.equalsIgnoreCase("VVK")) else if (scryset.equalsIgnoreCase("VVK"))
scryset = "DDI"; scryset = "DDI";
else if(scryset.equalsIgnoreCase("ZVE")) else if (scryset.equalsIgnoreCase("ZVE"))
scryset = "DDP"; scryset = "DDP";
try { try {
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get(); doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();
@@ -497,12 +506,12 @@ public class ImgDownloader {
for (int l = 1; l < arrays.length - 1; l++) { for (int l = 1; l < arrays.length - 1; l++) {
if (arrays[l].equalsIgnoreCase("creature") && arrays[l + 1].toLowerCase().contains("token")) { if (arrays[l].equalsIgnoreCase("creature") && arrays[l + 1].toLowerCase().contains("token")) {
nametoken = arrays[l - 1]; nametoken = arrays[l - 1];
if(l - 3 > 0) if (l - 3 > 0)
tokenstats = arrays[l - 3]; tokenstats = arrays[l - 3];
if(nametoken.equalsIgnoreCase("artifact")){ if (nametoken.equalsIgnoreCase("artifact")) {
if(l - 2 > 0) if (l - 2 > 0)
nametoken = arrays[l - 2]; nametoken = arrays[l - 2];
if(l - 4 > 0) if (l - 4 > 0)
tokenstats = arrays[l - 4]; tokenstats = arrays[l - 4];
} }
break; break;
@@ -525,7 +534,7 @@ public class ImgDownloader {
doc = findTokenPage(imageurl, nametoken, scryset, availableSets, tokenstats); doc = findTokenPage(imageurl, nametoken, scryset, availableSets, tokenstats);
tokenfound = true; tokenfound = true;
nametocheck = nametoken; nametocheck = nametoken;
} catch(Exception e) { } catch (Exception e) {
tokenfound = false; tokenfound = false;
nametocheck = mappa.get(id); nametocheck = mappa.get(id);
doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get(); doc = Jsoup.connect(imageurl + scryset.toLowerCase()).get();
@@ -593,26 +602,27 @@ public class ImgDownloader {
try { try {
File oldzip = new File(destinationPath + "/" + set + "/" + set + ".zip"); File oldzip = new File(destinationPath + "/" + set + "/" + set + ".zip");
oldzip.delete(); oldzip.delete();
} catch (Exception e) {} } catch (Exception e) {
}
ZipParameters zipParameters = new ZipParameters(); ZipParameters zipParameters = new ZipParameters();
zipParameters.setCompressionMethod(CompressionMethod.STORE); zipParameters.setCompressionMethod(CompressionMethod.STORE);
File folder = new File(destinationPath + set + "/"); File folder = new File(destinationPath + set + "/");
File[] listOfFile = folder.listFiles(); File[] listOfFile = folder.listFiles();
net.lingala.zip4j.ZipFile zipped = new net.lingala.zip4j.ZipFile(destinationPath + "/" + set + "/" + set + ".zip"); net.lingala.zip4j.ZipFile zipped = new net.lingala.zip4j.ZipFile(destinationPath + "/" + set + "/" + set + ".zip");
for (int i = 0 ; i < listOfFile.length; i++){ for (int i = 0; i < listOfFile.length; i++) {
if(listOfFile[i].isDirectory()){ if (listOfFile[i].isDirectory()) {
zipped.addFolder(listOfFile[i],zipParameters); zipped.addFolder(listOfFile[i], zipParameters);
} else { } else {
zipped.addFile(listOfFile[i], zipParameters); zipped.addFile(listOfFile[i], zipParameters);
} }
} }
File destFolder = new File(destinationPath + set + "/"); File destFolder = new File(destinationPath + set + "/");
listOfFiles = destFolder.listFiles(); listOfFiles = destFolder.listFiles();
for(int u = 0; u < listOfFiles.length; u++){ for (int u = 0; u < listOfFiles.length; u++) {
if (!listOfFiles[u].getName().contains(".zip")){ if (!listOfFiles[u].getName().contains(".zip")) {
if(listOfFiles[u].isDirectory()){ if (listOfFiles[u].isDirectory()) {
File[] listOfSubFiles = listOfFiles[u].listFiles(); File[] listOfSubFiles = listOfFiles[u].listFiles();
for(int j = 0; j < listOfSubFiles.length; j++) for (int j = 0; j < listOfSubFiles.length; j++)
listOfSubFiles[j].delete(); listOfSubFiles[j].delete();
} }
listOfFiles[u].delete(); listOfFiles[u].delete();
File diff suppressed because it is too large Load Diff