Equinox

HomestuckDL

May 30th, 2012
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.65 KB | None | 0 0
  1. package com.pi.homestuck.pages;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.net.URL;
  13. import java.net.URLConnection;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16.  
  17. import javax.swing.JOptionPane;
  18.  
  19. public class ComicPage {
  20.     public static boolean quiet = true;
  21.  
  22.     private final URL url;
  23.     private String rawData;
  24.     private String nextComicURL;
  25.     private String nextComic;
  26.     private List<String> dataParts = new ArrayList<String>();
  27.     private String currentComic;
  28.     private String nextComicReplace;
  29.  
  30.     public ComicPage(URL url) {
  31.         this.url = url;
  32.     }
  33.  
  34.     public void load() throws IOException {
  35.         URLConnection conn = url.openConnection();
  36.         InputStream in = conn.getInputStream();
  37.         BufferedReader read = new BufferedReader(new InputStreamReader(in));
  38.         rawData = null;
  39.         StringBuilder build = new StringBuilder();
  40.         boolean inComic = false;
  41.         boolean inScript = false;
  42.         while (true) {
  43.             String line = read.readLine();
  44.             if (line == null)
  45.                 break;
  46.             if (line.toLowerCase().contains("<!--")) {
  47.                 if (line.toLowerCase().contains("begin comic content")) {
  48.                     inComic = true;
  49.                 } else if (line.toLowerCase().contains("end comic content")) {
  50.                     inComic = false;
  51.                     rawData = build.toString();
  52.                     break;
  53.                 }
  54.             } else if (inComic) {
  55.                 if (line.contains("<script"))
  56.                     inScript = true;
  57.                 if (!inScript && !line.contains("noscript>"))
  58.                     build.append(line + "\n");
  59.                 if (line.contains("</script>"))
  60.                     inScript = false;
  61.             }
  62.         }
  63.         read.close();
  64.         in.close();
  65.     }
  66.  
  67.     public void parse() {
  68.         parseCurrentComic();
  69.         parseNextComic();
  70.  
  71.         dataParts.clear();
  72.         parseLinks();
  73.         parseSRC();
  74.     }
  75.  
  76.     public void save(File root) throws IOException {
  77.         File dataDir = new File(root, "data");
  78.         if (!dataDir.exists())
  79.             dataDir.mkdir();
  80.         downloadAndInjectParts(dataDir, "data/");
  81.  
  82.         File dest = new File(root, getCurrentComic() + ".html");
  83.         if (!dest.exists())
  84.             dest.createNewFile();
  85.  
  86.         if (nextComic != null)
  87.             rawData = rawData.replace(nextComicReplace, nextComic + ".html");
  88.  
  89.         BufferedWriter write = new BufferedWriter(new FileWriter(dest));
  90.         write.write("<html><body>");
  91.         write.write(rawData);
  92.         write.write("</body></html>");
  93.         write.close();
  94.     }
  95.  
  96.     private void downloadAndInjectParts(File destDir, String pathPrefix)
  97.             throws IOException {
  98.         String rootPath = url.getProtocol() + "://" + url.getHost();
  99.         String relPath = rootPath + url.getPath();
  100.         for (String s : dataParts) {
  101.             String url = "";
  102.             if (s.startsWith("/")) {
  103.                 url = rootPath + s;
  104.             } else if (s.startsWith("http://")) {
  105.                 url = s;
  106.             } else {
  107.                 url = relPath + s;
  108.             }
  109.  
  110.             URL rURL = new URL(url);
  111.             String fPath = rURL.getPath();
  112.             if (fPath.startsWith("/"))
  113.                 fPath = fPath.substring(1);
  114.             File dest = new File(destDir, fPath);
  115.             if (!dest.exists()) {
  116.                 if (fPath.contains("/")) {
  117.                     File parent = new File(dest.getParent());
  118.                     parent.mkdirs();
  119.                 }
  120.                 if (!quiet)
  121.                     System.out.println("Downloading: " + rURL.toString()
  122.                             + "\t=>\t" + fPath);
  123.                 try {
  124.                     download(rURL, dest);
  125.                 } catch (IOException e) {
  126.                     System.err.println("Error on " + rURL.toString());
  127.                     continue;
  128.                 }
  129.             } else {
  130.                 if (!quiet)
  131.                     System.out.println("Skipping downloading: "
  132.                             + rURL.toString() + "\t=>\t" + fPath);
  133.             }
  134.             rawData = rawData.replace(s, pathPrefix + fPath);
  135.         }
  136.     }
  137.  
  138.     private void parseCurrentComic() {
  139.         String[] query = url.getQuery().split("&");
  140.         currentComic = null;
  141.         for (String s : query) {
  142.             if (s.startsWith("p=")) {
  143.                 currentComic = s.substring(2);
  144.                 return;
  145.             }
  146.         }
  147.     }
  148.  
  149.     private void parseNextComic() {
  150.         int realIndex = rawData.indexOf("<font size=\"5\">&gt; ");
  151.         nextComicURL = nextComicReplace = nextComic = null;
  152.         if (realIndex != -1) {
  153.             int href = rawData.indexOf("href=", realIndex);
  154.             if (href != -1) {
  155.                 href += 5;
  156.                 if (rawData.charAt(href) == '"') {
  157.                     int endQuote = rawData.indexOf('"', href + 1);
  158.                     if (endQuote != -1) {
  159.                         nextComicURL = nextComicReplace = rawData.substring(
  160.                                 href + 1, endQuote);
  161.                         if (nextComicURL.startsWith("?")) {
  162.                             nextComicURL = url.getProtocol() + "://"
  163.                                     + url.getHost() + url.getPath()
  164.                                     + nextComicURL;
  165.                         }
  166.  
  167.                         String[] query = nextComicURL.split("&");
  168.                         for (String s : query) {
  169.                             if (s.startsWith("p=")) {
  170.                                 nextComic = s.substring(2);
  171.                                 break;
  172.                             }
  173.                         }
  174.  
  175.                         return;
  176.                     }
  177.                 }
  178.             }
  179.         }
  180.         System.err.println("Next Comic Parse Error! " + url.toExternalForm());
  181.     }
  182.  
  183.     private void parseSRC() {
  184.         int currentIndex = 0;
  185.         while ((currentIndex = rawData.indexOf(" src=", currentIndex + 5)) != -1) {
  186.             char quote = rawData.charAt(currentIndex + 5);
  187.             if (quote == '\'' || quote == '"') {
  188.                 int quoteEnd = rawData.indexOf(quote, currentIndex + 6);
  189.                 if (quoteEnd != -1) {
  190.                     String url = rawData.substring(currentIndex + 6, quoteEnd);
  191.                     if (!dataParts.contains(url))
  192.                         dataParts.add(url);
  193.                 }
  194.             }
  195.         }
  196.     }
  197.  
  198.     private void parseLinks() {
  199.         int currentIndex = 0;
  200.         while ((currentIndex = rawData.indexOf(
  201.                 "http://www.mspaintadventures.com/", currentIndex + 30)) != -1) {
  202.             int dblQuoteStart = rawData.lastIndexOf('"', currentIndex);
  203.             int singleQuoteStart = rawData.lastIndexOf('\'', currentIndex);
  204.             int quoteStart = -1;
  205.             char quote;
  206.             if (singleQuoteStart == -1 && dblQuoteStart == -1) {
  207.                 continue;
  208.             }
  209.             if (singleQuoteStart == -1) {
  210.                 quote = '"';
  211.                 quoteStart = singleQuoteStart;
  212.             } else if (dblQuoteStart == -1) {
  213.                 quote = '\'';
  214.                 quoteStart = dblQuoteStart;
  215.             } else if (dblQuoteStart < singleQuoteStart) {
  216.                 quoteStart = singleQuoteStart;
  217.                 quote = '\'';
  218.             } else {
  219.                 quoteStart = dblQuoteStart;
  220.                 quote = '"';
  221.             }
  222.             if (quoteStart != -1) {
  223.                 int quoteEnd = rawData.indexOf(quote, currentIndex + 30);
  224.                 if (quoteEnd != -1) {
  225.                     String url = rawData.substring(quoteStart + 1, quoteEnd);
  226.                     if (!dataParts.contains(url))
  227.                         dataParts.add(url);
  228.                 }
  229.             }
  230.  
  231.         }
  232.     }
  233.  
  234.     public String getNextComicURL() {
  235.         return nextComicURL;
  236.     }
  237.  
  238.     public String getNextComic() {
  239.         return nextComic;
  240.     }
  241.  
  242.     public String getCurrentComic() {
  243.         return currentComic;
  244.     }
  245.  
  246.     public static void main(String[] args) throws IOException {
  247.         new File("Homestuck").mkdir();
  248.  
  249.         String origin = JOptionPane
  250.                 .showInputDialog("Starting comic?\nex: http://www.mspaintadventures.com/?s=6&p=001901");
  251.         download(new URL(origin));
  252.     }
  253.  
  254.     public static void download(URL url) throws IOException {
  255.         ComicPage pg = new ComicPage(url);
  256.         pg.load();
  257.         pg.parse();
  258.  
  259.         String dest = "Homestuck/" + pg.getCurrentComic() + ".html";
  260.         System.out.println("Downloading: " + pg.getCurrentComic() + "\t=>\t"
  261.                 + dest);
  262.  
  263.         pg.save(new File(dest).getParentFile());
  264.  
  265.         if (pg.getNextComic() != null)
  266.             download(new URL(pg.getNextComicURL()));
  267.     }
  268.  
  269.     public static void download(URL url, File dest) throws IOException {
  270.         BufferedInputStream in = null;
  271.         FileOutputStream fout = null;
  272.         File f = new File(dest.getAbsolutePath() + ".part");
  273.         f.createNewFile();
  274.         try {
  275.             in = new BufferedInputStream(url.openStream());
  276.             fout = new FileOutputStream(f);
  277.             byte data[] = new byte[1024];
  278.             int count;
  279.             while ((count = in.read(data, 0, 1024)) != -1) {
  280.                 fout.write(data, 0, count);
  281.             }
  282.         } finally {
  283.             if (in != null)
  284.                 in.close();
  285.             if (fout != null)
  286.                 fout.close();
  287.         }
  288.         dest.delete();
  289.         f.renameTo(dest);
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment