Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZed committed Oct 25, 2015
1 parent 9f926ae commit 0e44b67
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 44 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# FakkuDownloader
Downloads galleries from Fakku, only if it's public
<p align="center"><img src="http://puu.sh/kVY41.png"</p>
<p align="center"><img src="http://puu.sh/kWrZi.png"</p>
179 changes: 139 additions & 40 deletions src/main/java/Slayer/DownBot/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,45 @@
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import javafx.event.ActionEvent;
import javafx.scene.control.CheckBox;
import javafx.scene.control.*;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/*
* This program was created by DrZed/KeldonSlayer/LostPotatoFoundation
* Created for lack of a good program to do what I wanted.
* feel free to modify for your own use..
*/
@SuppressWarnings({"ConstantConditions", "unused"})
@SuppressWarnings({"ConstantConditions", "unused", "unchecked"})
public class Controller {
public TextArea input;
public CheckBox zip;
public CheckBox cbz;
public ProgressBar progress = new ProgressBar(0);
public TextField amount;
private File outputDir = new File(System.getProperty("user.dir") + "/FakkuDownloads/");

public void downloadImages(ActionEvent actionEvent) {
private int nl = 0, nc = 0;

public void updateProgress() {
if (nl != 0)
progress.setProgress((double)(nc/nl));
}
public void downloadImages(ActionEvent event) {
//make sure the links provided match requirements
if (input != null && input.getText() != null && !input.getText().isEmpty() && input.getText().contains("http") && input.getText().contains("fakku")) {
if (!outputDir.exists()) {
Expand All @@ -41,52 +52,65 @@ public void downloadImages(ActionEvent actionEvent) {
//get all the links from input
String[] links = input.getText().split(",");
if (links.length == 0) links = new String[]{input.getText()};
nl = links.length;
for (String link : links) {
link = link.replace("https", "http").trim();
if (!link.contains("read")) {
link = link + "/read";
}
System.out.println(link);
link = link.replace("https", "http");
//for each link grab the gallery
try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38)) {
HtmlPage page = webClient.getPage(link);
String galleryName = page.getTitleText();
galleryName = galleryName.substring(galleryName.indexOf("Read") + 4, galleryName.indexOf(" - ")).trim();
String pageAsXML = page.asXml();
//fakku specifically has their gallery thumbnails in an area matching this..
String[] pageFragment = pageAsXML.split("<a href=\"#page=");
List<String> imagesToDL = new ArrayList<>();
for (String fragment : pageFragment) {
//make sure I'm only getting the areas containing an image link
if (fragment.contains("\" title=\"(Page ")) {
if (fragment.contains("</div>")) fragment = fragment.substring(0, fragment.indexOf("</div>"));
fragment = fragment.substring(fragment.indexOf("//t.fakku.net"), fragment.lastIndexOf(".jpg"));

//add each link to be downloaded from
imagesToDL.add(("http:" + fragment + ".jpg").replace(".thumb.jpg", ".jpg").replace("/thumbs/", "/images/"));

if (link.contains("/read")) {
getGallery(page);
updateProgress();
} else if (!link.contains("/read") && pageAsXML.contains("<div class=\"book\">")) {
String[] pageFragments = pageAsXML.split("<div class=\"book\">");
nl += pageFragments.length;
for (String fragment : pageFragments) {
if (fragment.contains("<div class=\"book-cover\">")) {
fragment = fragment.substring(fragment.indexOf("href=") + 6);
fragment = fragment.substring(0, fragment.indexOf("\""));
fragment = "https://www.fakku.net" + fragment;
getGallery(webClient.getPage(fragment + "/read"));
nc++;
updateProgress();
}
}
} else if (!link.contains("/read") && link.contains("tags") && pageAsXML.contains("<div class=\"images four columns\">")) {
String[] pageFragments = pageAsXML.split("<div class=\"content-row doujinshi row\">");
if (pageFragments.length == 0) pageFragments = pageAsXML.split("<div class=\"content-row manga row\">");
if (pageFragments.length == 0) break;
nl += pageFragments.length;
for (String fragment : pageFragments) {
if (fragment.contains("<div class=\"images four columns\">")) {
fragment = fragment.substring(fragment.indexOf("href=") + 6);
fragment = fragment.substring(0, fragment.indexOf("\""));
fragment = "https://www.fakku.net" + fragment;
getGallery(webClient.getPage(fragment + "/read"));
nc++;
updateProgress();
}
}
} else {
getGallery(webClient.getPage(link + "/read"));
updateProgress();
}
System.out.println(galleryName);
File gallery = new File(outputDir, "/" + galleryName + "/");
if (!gallery.exists()) {
gallery.mkdirs();
}
System.out.println(gallery);

imagesToDL.forEach(img -> {
try {
URL url = new URL(img);
Image image = ImageIO.read(url);
File imag = new File(gallery, img.substring(img.lastIndexOf("/")));
ImageIO.write(imageToBufferedImage(image), "JPG", imag);
} catch (Exception ignored) {
nc++;
updateProgress();
if (amount.getText().charAt(0) != '-' && Integer.parseInt(amount.getText()) != 0 && pageAsXML.contains("<div class=\"results\">")) {
String nlink;
for (int i = 0; i < Integer.parseInt(amount.getText()); i++) {
if (!link.contains("/page/")) {
nlink = link + "/page/" + i + 2;
} else {
nlink = link + link.substring(0, link.lastIndexOf("/")) + i + 2 + Integer.parseInt(link.substring(link.lastIndexOf("/")).replace("/", ""));
}
getGals(webClient, webClient.getPage(nlink));
}
});

if (zip.isSelected() && gallery.listFiles() != null && gallery.listFiles().length > 0) {
zipFiles(Arrays.asList(gallery.listFiles()), galleryName);
}
} catch (Exception ignored) {}
} catch(Exception e){
e.printStackTrace();
}
}
}
}
Expand Down Expand Up @@ -135,4 +159,79 @@ public File zipFiles(List<File> files, String filename) {
} catch (Exception ignored) {}
return null;
}

private Random rand = new Random();

public void getGallery(HtmlPage page) {
String pageAsXML = page.asXml();
//fakku specifically has their gallery thumbnails in an area matching this..
String[] pageFragment = pageAsXML.split("<a href=\"#page=");
String galleryName = page.getTitleText().replaceAll("\"", "''");
if (!galleryName.isEmpty() && galleryName.contains("Read")) {
galleryName = galleryName.substring(galleryName.indexOf("Read") + 4, galleryName.indexOf(" - ")).trim();
} else if (galleryName.isEmpty()) {
galleryName = "ERRORED " + rand.nextInt(1000);
}
List<String> imagesToDL = new ArrayList<>();
for (String fragment : pageFragment) {
//make sure I'm only getting the areas containing an image link
if (fragment.contains("\" title=\"(Page ")) {
if (fragment.contains("</div>"))
fragment = fragment.substring(0, fragment.indexOf("</div>"));
fragment = fragment.substring(fragment.indexOf("//t.fakku.net"), fragment.lastIndexOf(".jpg"));

//add each link to be downloaded from
imagesToDL.add(("http:" + fragment + ".jpg").replace(".thumb.jpg", ".jpg").replace("/thumbs/", "/images/"));
}
}
File gallery = new File(outputDir, "/" + galleryName + "/");
if (!gallery.exists()) {
gallery.mkdirs();
}

imagesToDL.forEach(img -> {
try {
URL url = new URL(img);
Image image = ImageIO.read(url);
File imag = new File(gallery, img.substring(img.lastIndexOf("/")));
ImageIO.write(imageToBufferedImage(image), "JPG", imag);
} catch (Exception ignored) {
}
});

if (zip.isSelected() && gallery.listFiles() != null && gallery.listFiles().length > 0) {
zipFiles(Arrays.asList(gallery.listFiles()), galleryName);
}
updateProgress();
}

public void getGals(WebClient webClient, HtmlPage page) {
String pageAsXML = page.asXml();
//fakku specifically has their gallery thumbnails in an area matching this..
String[] pageFragment = pageAsXML.split("<a href=\"#page=");
String galleryName = page.getTitleText().replaceAll("\"", "''");
if (!galleryName.isEmpty() && galleryName.contains("Read")) {
galleryName = galleryName.substring(galleryName.indexOf("Read") + 4, galleryName.indexOf(" - ")).trim();
} else if (galleryName.isEmpty()) {
galleryName = "ERRORED " + rand.nextInt(1000);
}
String[] pageFragments = pageAsXML.split("<div class=\"content-row doujinshi row\">");
if (pageFragments.length == 0) pageFragments = pageAsXML.split("<div class=\"content-row manga row\">");
if (pageFragments.length == 0) return;
nl += pageFragments.length;
for (String fragment : pageFragments) {
if (fragment.contains("<div class=\"images four columns\">")) {
fragment = fragment.substring(fragment.indexOf("href=") + 6);
fragment = fragment.substring(0, fragment.indexOf("\""));
fragment = "https://www.fakku.net" + fragment;
try {
getGallery(webClient.getPage(fragment + "/read"));
} catch (IOException e) {
e.printStackTrace();
}
nc++;
updateProgress();
}
}
}
}
10 changes: 6 additions & 4 deletions src/main/java/Slayer/DownBot/botGUI.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="375.0" prefWidth="270.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Slayer.DownBot.Controller">
<children>
<TextArea fx:id="input" layoutX="14.0" layoutY="14.0" prefHeight="294.0" prefWidth="241.0" promptText="Put fakku /read links here" />
<Button layoutX="79.0" layoutY="338.0" mnemonicParsing="false" onAction="#downloadImages" text="Download Images" />
<CheckBox fx:id="zip" layoutX="14.0" layoutY="312.0" mnemonicParsing="false" text="ZipImages" />
<CheckBox fx:id="cbz" layoutX="171.0" layoutY="312.0" mnemonicParsing="false" text="Output CBZ" />
<TextArea fx:id="input" layoutX="14.0" layoutY="47.0" prefHeight="242.0" prefWidth="241.0" promptText="Put fakku /read links here" />
<Button layoutX="80.0" layoutY="318.0" mnemonicParsing="false" onAction="#downloadImages" text="Download Images" />
<CheckBox fx:id="zip" layoutX="14.0" layoutY="289.0" mnemonicParsing="false" text="ZipImages" />
<TextField fx:id="amount" layoutX="60.0" layoutY="14.0" promptText="Number of Pages" text="-1" />
<CheckBox fx:id="cbz" layoutX="168.0" layoutY="289.0" mnemonicParsing="false" text="Output CBZ" />
<ProgressBar fx:id="progress" layoutX="35.0" layoutY="343.0" prefWidth="200.0" progress="0.0" />
</children>
</AnchorPane>

0 comments on commit 0e44b67

Please sign in to comment.