import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; /** * DailyProgrammer Challenge #110 Intermediate - Creepy Crawlies * * Gets the top 100 stories from /r/nosleep, prints them out and saves them to * stories.txt * * @author Quentin/HolocaustHank * */ public class DP110I { public static void main(String[] args) { new DP110I(); } public DP110I() { printStories(); } void printStories() { try { ArrayList topLinks = getTopLinks(); ArrayList stories = new ArrayList(); for (int i = 0; i < topLinks.size(); i++) { URL url = new URL(topLinks.get(i)); HttpURLConnection con = (HttpURLConnection) url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String line; boolean started = false, quote = false; while ((line = br.readLine()) != null) { line = line.replaceAll("", "").replaceAll("", "").replaceAll("'", "'").replaceAll(""", "\"") .replaceAll("&", "&").replaceAll("’", "'").replaceAll("", "").replaceAll("", ""); if (started) { if (line.startsWith("
")) quote = true; if (line.startsWith("
")) quote = false; if (line.startsWith("

")) { String s = ""; if (quote) s += ">"; line = line.replaceAll("

", "").replaceAll("

", ""); s += line; stories.add(s); } if (line.startsWith("")) started = false; } if (line.contains("a class=\"title")) { started = true; stories.add("=== " + getTitle(line) + " ==="); line = line.substring(line.indexOf("class=\"md") + 14); stories.add(line.replaceAll("

", "").replaceAll("

", "")); } } br.close(); stories.add(""); System.out.println("Got story " + (i + 1)); } File file = new File("stories.txt"); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); for (int i = 0; i < stories.size(); i++) { System.out.println(stories.get(i)); bw.write(stories.get(i) + "\n"); } bw.close(); } catch (Exception e) { e.printStackTrace(); } } String getTitle(String s) { String result = s.substring(s.indexOf("a class=\"title")); result = result.substring(result.indexOf(">") + 1, result.indexOf("<")); return result.replaceAll(""", "\"").replaceAll("&", "&"); } ArrayList getTopLinks() throws IOException { ArrayList links = new ArrayList(); String format = "http://www.reddit.com/r/nosleep?count=%s&after=%s", lastLink = ""; for (int i = 0; i < 4; i++) { URL url = new URL(String.format(format, i * 25, lastLink)); HttpURLConnection con = (HttpURLConnection) url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String line; while ((line = br.readLine()) != null) if (line.contains("")) { String[] all = line.split("
"); for (int j = 0; j < 25; j++) { String s = all[j]; s = s.substring(s.indexOf("