Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pi.homestuck.pages;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JOptionPane;
- public class ComicPage {
- public static boolean quiet = true;
- private final URL url;
- private String rawData;
- private String nextComicURL;
- private String nextComic;
- private List<String> dataParts = new ArrayList<String>();
- private String currentComic;
- private String nextComicReplace;
- public ComicPage(URL url) {
- this.url = url;
- }
- public void load() throws IOException {
- URLConnection conn = url.openConnection();
- InputStream in = conn.getInputStream();
- BufferedReader read = new BufferedReader(new InputStreamReader(in));
- rawData = null;
- StringBuilder build = new StringBuilder();
- boolean inComic = false;
- boolean inScript = false;
- while (true) {
- String line = read.readLine();
- if (line == null)
- break;
- if (line.toLowerCase().contains("<!--")) {
- if (line.toLowerCase().contains("begin comic content")) {
- inComic = true;
- } else if (line.toLowerCase().contains("end comic content")) {
- inComic = false;
- rawData = build.toString();
- break;
- }
- } else if (inComic) {
- if (line.contains("<script"))
- inScript = true;
- if (!inScript && !line.contains("noscript>"))
- build.append(line + "\n");
- if (line.contains("</script>"))
- inScript = false;
- }
- }
- read.close();
- in.close();
- }
- public void parse() {
- parseCurrentComic();
- parseNextComic();
- dataParts.clear();
- parseLinks();
- parseSRC();
- }
- public void save(File root) throws IOException {
- File dataDir = new File(root, "data");
- if (!dataDir.exists())
- dataDir.mkdir();
- downloadAndInjectParts(dataDir, "data/");
- File dest = new File(root, getCurrentComic() + ".html");
- if (!dest.exists())
- dest.createNewFile();
- if (nextComic != null)
- rawData = rawData.replace(nextComicReplace, nextComic + ".html");
- BufferedWriter write = new BufferedWriter(new FileWriter(dest));
- write.write("<html><body>");
- write.write(rawData);
- write.write("</body></html>");
- write.close();
- }
- private void downloadAndInjectParts(File destDir, String pathPrefix)
- throws IOException {
- String rootPath = url.getProtocol() + "://" + url.getHost();
- String relPath = rootPath + url.getPath();
- for (String s : dataParts) {
- String url = "";
- if (s.startsWith("/")) {
- url = rootPath + s;
- } else if (s.startsWith("http://")) {
- url = s;
- } else {
- url = relPath + s;
- }
- URL rURL = new URL(url);
- String fPath = rURL.getPath();
- if (fPath.startsWith("/"))
- fPath = fPath.substring(1);
- File dest = new File(destDir, fPath);
- if (!dest.exists()) {
- if (fPath.contains("/")) {
- File parent = new File(dest.getParent());
- parent.mkdirs();
- }
- if (!quiet)
- System.out.println("Downloading: " + rURL.toString()
- + "\t=>\t" + fPath);
- try {
- download(rURL, dest);
- } catch (IOException e) {
- System.err.println("Error on " + rURL.toString());
- continue;
- }
- } else {
- if (!quiet)
- System.out.println("Skipping downloading: "
- + rURL.toString() + "\t=>\t" + fPath);
- }
- rawData = rawData.replace(s, pathPrefix + fPath);
- }
- }
- private void parseCurrentComic() {
- String[] query = url.getQuery().split("&");
- currentComic = null;
- for (String s : query) {
- if (s.startsWith("p=")) {
- currentComic = s.substring(2);
- return;
- }
- }
- }
- private void parseNextComic() {
- int realIndex = rawData.indexOf("<font size=\"5\">> ");
- nextComicURL = nextComicReplace = nextComic = null;
- if (realIndex != -1) {
- int href = rawData.indexOf("href=", realIndex);
- if (href != -1) {
- href += 5;
- if (rawData.charAt(href) == '"') {
- int endQuote = rawData.indexOf('"', href + 1);
- if (endQuote != -1) {
- nextComicURL = nextComicReplace = rawData.substring(
- href + 1, endQuote);
- if (nextComicURL.startsWith("?")) {
- nextComicURL = url.getProtocol() + "://"
- + url.getHost() + url.getPath()
- + nextComicURL;
- }
- String[] query = nextComicURL.split("&");
- for (String s : query) {
- if (s.startsWith("p=")) {
- nextComic = s.substring(2);
- break;
- }
- }
- return;
- }
- }
- }
- }
- System.err.println("Next Comic Parse Error! " + url.toExternalForm());
- }
- private void parseSRC() {
- int currentIndex = 0;
- while ((currentIndex = rawData.indexOf(" src=", currentIndex + 5)) != -1) {
- char quote = rawData.charAt(currentIndex + 5);
- if (quote == '\'' || quote == '"') {
- int quoteEnd = rawData.indexOf(quote, currentIndex + 6);
- if (quoteEnd != -1) {
- String url = rawData.substring(currentIndex + 6, quoteEnd);
- if (!dataParts.contains(url))
- dataParts.add(url);
- }
- }
- }
- }
- private void parseLinks() {
- int currentIndex = 0;
- while ((currentIndex = rawData.indexOf(
- "http://www.mspaintadventures.com/", currentIndex + 30)) != -1) {
- int dblQuoteStart = rawData.lastIndexOf('"', currentIndex);
- int singleQuoteStart = rawData.lastIndexOf('\'', currentIndex);
- int quoteStart = -1;
- char quote;
- if (singleQuoteStart == -1 && dblQuoteStart == -1) {
- continue;
- }
- if (singleQuoteStart == -1) {
- quote = '"';
- quoteStart = singleQuoteStart;
- } else if (dblQuoteStart == -1) {
- quote = '\'';
- quoteStart = dblQuoteStart;
- } else if (dblQuoteStart < singleQuoteStart) {
- quoteStart = singleQuoteStart;
- quote = '\'';
- } else {
- quoteStart = dblQuoteStart;
- quote = '"';
- }
- if (quoteStart != -1) {
- int quoteEnd = rawData.indexOf(quote, currentIndex + 30);
- if (quoteEnd != -1) {
- String url = rawData.substring(quoteStart + 1, quoteEnd);
- if (!dataParts.contains(url))
- dataParts.add(url);
- }
- }
- }
- }
- public String getNextComicURL() {
- return nextComicURL;
- }
- public String getNextComic() {
- return nextComic;
- }
- public String getCurrentComic() {
- return currentComic;
- }
- public static void main(String[] args) throws IOException {
- new File("Homestuck").mkdir();
- String origin = JOptionPane
- .showInputDialog("Starting comic?\nex: http://www.mspaintadventures.com/?s=6&p=001901");
- download(new URL(origin));
- }
- public static void download(URL url) throws IOException {
- ComicPage pg = new ComicPage(url);
- pg.load();
- pg.parse();
- String dest = "Homestuck/" + pg.getCurrentComic() + ".html";
- System.out.println("Downloading: " + pg.getCurrentComic() + "\t=>\t"
- + dest);
- pg.save(new File(dest).getParentFile());
- if (pg.getNextComic() != null)
- download(new URL(pg.getNextComicURL()));
- }
- public static void download(URL url, File dest) throws IOException {
- BufferedInputStream in = null;
- FileOutputStream fout = null;
- File f = new File(dest.getAbsolutePath() + ".part");
- f.createNewFile();
- try {
- in = new BufferedInputStream(url.openStream());
- fout = new FileOutputStream(f);
- byte data[] = new byte[1024];
- int count;
- while ((count = in.read(data, 0, 1024)) != -1) {
- fout.write(data, 0, count);
- }
- } finally {
- if (in != null)
- in.close();
- if (fout != null)
- fout.close();
- }
- dest.delete();
- f.renameTo(dest);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment