-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedditRead.java
52 lines (48 loc) · 2.05 KB
/
RedditRead.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.lang.NullPointerException;
import java.util.Scanner;
public class RedditRead {
public static void main(String []args) {
try {
String commentstring="0";
String url;
Scanner sc = new Scanner(System.in);
System.out.print("Enter URL: ");
url = sc.next();
Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2774.3 Safari/537.36").get();
String title = doc.title();
WriteToHTMLFile hf = new WriteToHTMLFile(title.replace("/"," ")+".html");
hf.clearFile();
hf.insertLine("<!DOCTYPE html>\n<html>\n<head>");
hf.insertLine("<title>"+title+"</title>");
hf.insertLine("<meta author=\"reddit.com\">\n</head>\n<body>");
hf.insertLine("<h1>"+title+"</h1>");
Elements entries = doc.getElementsByClass("entry");
for (Element entry : entries) {
if (!entry.getElementsByClass("author").isEmpty()) {
hf.insertLine("<p><b>"+entry.getElementsByClass("author").text()+"</b></p>");
}
if(!entry.getElementsByClass("md").isEmpty()) {
hf.insertLine("<p>"+entry.getElementsByClass("md").first().toString().replaceAll(" class=\"md\"", "")+"</p>");
}
}
hf.insertLine("</body>\n</html>");
System.out.println("Conversion successful");
}
catch (IOException e) {
//e.printStackTrace();
System.out.println("Oops! Some files are missing.");
}
catch (NullPointerException e) {
e.printStackTrace();
}
catch (IndexOutOfBoundsException e) {
//e.printStackTrace();
System.out.println("Oops! Couldn't parse the URL. Waiting for a fix.");
}
}
}