This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

DailyProgrammer 110 Intermediate

By: a guest on Nov 3rd, 2012  |  syntax: Java  |  size: 4.16 KB  |  views: 145  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.util.ArrayList;
  10.  
  11. /**
  12.  * DailyProgrammer Challenge #110 Intermediate - Creepy Crawlies
  13.  *
  14.  * Gets the top 100 stories from /r/nosleep, prints them out and saves them to
  15.  * stories.txt
  16.  *
  17.  * @author Quentin/HolocaustHank
  18.  *
  19.  */
  20.  
  21. public class DP110I {
  22.  
  23.   public static void main(String[] args) {
  24.     new DP110I();
  25.   }
  26.  
  27.   public DP110I() {
  28.     printStories();
  29.   }
  30.  
  31.   void printStories() {
  32.     try {
  33.       ArrayList<String> topLinks = getTopLinks();
  34.       ArrayList<String> stories = new ArrayList<String>();
  35.       for (int i = 0; i < topLinks.size(); i++) {
  36.         URL url = new URL(topLinks.get(i));
  37.         HttpURLConnection con = (HttpURLConnection) url.openConnection();
  38.         BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
  39.         String line;
  40.         boolean started = false, quote = false;
  41.         while ((line = br.readLine()) != null) {
  42.           line = line.replaceAll("<strong>", "").replaceAll("</strong>", "").replaceAll("&#39;", "'").replaceAll("&quot;", "\"")
  43.               .replaceAll("&amp;", "&").replaceAll("’", "'").replaceAll("<em>", "").replaceAll("</em>", "");
  44.           if (started) {
  45.             if (line.startsWith("<blockquote>"))
  46.               quote = true;
  47.             if (line.startsWith("</blockquote>"))
  48.               quote = false;
  49.             if (line.startsWith("<p>")) {
  50.               String s = "";
  51.               if (quote)
  52.                 s += ">";
  53.               line = line.replaceAll("<p>", "").replaceAll("</p>", "");
  54.               s += line;
  55.               stories.add(s);
  56.             }
  57.             if (line.startsWith("</div>"))
  58.               started = false;
  59.           }
  60.           if (line.contains("a class=\"title")) {
  61.             started = true;
  62.             stories.add("=== " + getTitle(line) + " ===");
  63.             line = line.substring(line.indexOf("class=\"md") + 14);
  64.             stories.add(line.replaceAll("<p>", "").replaceAll("</p>", ""));
  65.           }
  66.         }
  67.         br.close();
  68.         stories.add("");
  69.         System.out.println("Got story " + (i + 1));
  70.       }
  71.       File file = new File("stories.txt");
  72.       BufferedWriter bw = new BufferedWriter(new FileWriter(file));
  73.       for (int i = 0; i < stories.size(); i++) {
  74.         System.out.println(stories.get(i));
  75.         bw.write(stories.get(i) + "\n");
  76.       }
  77.       bw.close();
  78.     } catch (Exception e) {
  79.       e.printStackTrace();
  80.     }
  81.   }
  82.  
  83.   String getTitle(String s) {
  84.     String result = s.substring(s.indexOf("a class=\"title"));
  85.     result = result.substring(result.indexOf(">") + 1, result.indexOf("<"));
  86.     return result.replaceAll("&quot;", "\"").replaceAll("&amp;", "&");
  87.   }
  88.  
  89.   ArrayList<String> getTopLinks() throws IOException {
  90.     ArrayList<String> links = new ArrayList<String>();
  91.     String format = "http://www.reddit.com/r/nosleep?count=%s&after=%s", lastLink = "";
  92.     for (int i = 0; i < 4; i++) {
  93.       URL url = new URL(String.format(format, i * 25, lastLink));
  94.       HttpURLConnection con = (HttpURLConnection) url.openConnection();
  95.       BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
  96.       String line;
  97.       while ((line = br.readLine()) != null)
  98.         if (line.contains("<!--IE6sux-->")) {
  99.           String[] all = line.split("<div class=\"clearleft\"><!--IE6sux--></div></div><div class=\"clearleft\"><!--IE6sux--></div>");
  100.           for (int j = 0; j < 25; j++) {
  101.             String s = all[j];
  102.             s = s.substring(s.indexOf("<a class=\"title"));
  103.             s = s.substring(s.indexOf("href") + 6);
  104.             s = s.substring(0, s.indexOf("\""));
  105.             links.add("http://www.reddit.com" + s);
  106.             if (j == 24) {
  107.               s = s.substring(s.indexOf("comments/") + 9);
  108.               s = s.substring(0, s.indexOf("/"));
  109.               lastLink = "t3_" + s;
  110.             }
  111.           }
  112.         }
  113.       br.close();
  114.     }
  115.     return links;
  116.   }
  117.  
  118. }
clone this paste RAW Paste Data