forked from mrepol742/mrepol742.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSS.java
188 lines (170 loc) · 6.96 KB
/
RSS.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.text.SimpleDateFormat;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.json.*;
import org.jsoup.*;
import org.jsoup.select.*;
import org.jsoup.helper.*;
import org.jsoup.internal.*;
import org.jsoup.nodes.*;
import org.jsoup.parser.*;
class RSS {
private static List<Item> items = new ArrayList<>();
private static JSONObject obj = new JSONObject();
private static SimpleDateFormat format = new SimpleDateFormat("E, dd MMMM yyyy k:m:s z");
private static String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rss xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss/\">\n<channel>\n";
private static String footer = "</channel>\n</rss>";
private static String body = "<item>\n" +
" <title>%1$s</title>\n" +
" <description>%2$s</description>\n" +
" <link>%3$s</link>\n" +
" <creator>%4$s</creator>\n" +
" <content medium=\"image\" url=\"%5$s\"/>\n";
private static StringBuilder rss = new StringBuilder();
private static String url;
private static String domain;
private static boolean isHome = false;
public static void main(String[] args) {
//String domain = args[0];
//String url = args[1];
url = "/home/alexaguno/Documents/mrepol742.github.io";
domain = "https://mrepol742.github.io";
rss.append(header);
find(new File(url), domain);
for (Item item: items) {
rss.append(String.format(body, item.title, item.description, item.link, "<![CDATA[ Melvin Jones Repol ]]>", item.medium_url));
rss.append("</item>\n");
}
rss.append(footer);
if (write(new File(url + "/rss.xml"), rss.toString(), false)) {
System.out.println("\nRSS generated for " + domain);
} else {
System.out.println("\nFailed to generate rss.");
}
}
public static String getTitle(File file) {
StringBuilder str = new StringBuilder();
Document doc = Jsoup.parse(read(file, "\n"));
Elements title = doc.getElementsByTag("title");
return title.get(0).toString().replace("<title>", "").replace("</title>", "");
}
public static String[] getMeta(File file) {
String[] data = {"",""};
Document doc = Jsoup.parse(read(file, "\n"));
Elements metas = doc.getElementsByTag("meta");
for (Element meta : metas) {
String content = meta.attr("content");
String name = meta.attr("name");
String prop = meta.attr("property");
if (name.equals("description")) {
data[0] = content;
}
if (prop.equals("og:image")) {
data[1] = domain + content;
}
}
return data;
}
public static void find(File file, String domain) {
if (file.list() == null) {
System.out.println("no index " + file.toString());
return;
}
if (file.isDirectory() && !isHome) {
String[] metas = getMeta(new File(url+"/index.html"));
rss.append(" <title>");
rss.append("<![CDATA[");
rss.append(getTitle(new File(url+"/index.html")));
rss.append("]]>");
rss.append("</title>\n");
rss.append(" <description>");
rss.append("<![CDATA[");
rss.append( metas[0]);
rss.append("]]>");
rss.append("</description>\n");
rss.append(" <link>");
rss.append(domain);
rss.append(" </link>\n");
rss.append(" <image>\n");
rss.append(" <url>");
rss.append(metas[1]);
rss.append("</url>\n");
rss.append(" <title>");
rss.append(getTitle(new File(url+"/index.html")));
rss.append("</title>\n");
rss.append(" <link>");
rss.append(domain);
rss.append("</link>\n");
rss.append(" </image>\n <generator> RSS for Melvin Jones Repol </generator>\n");
rss.append(" <lastBuildDate>");
rss.append(format.format(new Date()));
rss.append("</lastBuildDate>\n");
rss.append(" <link href=\"https://" + domain + "/rss.xml\"" + " rel=\"self\" type=\"application/rss+xml\"/>\n");
rss.append(" <language><![CDATA[ en ]]></language>\n");
isHome = true;
}
String[] listFiles = file.list();
for (String str: listFiles) {
File folder = new File(file.getAbsolutePath() + "/" + str);
if (folder.isDirectory()) {
File hasIndex = new File(folder.getAbsolutePath() + "/index.html");
if (hasIndex.isFile()) {
System.out.println(format.format(hasIndex.lastModified()) + " | " + domain + hasIndex.getParentFile().getAbsolutePath().replace(url, ""));
String[] metas = getMeta(new File(hasIndex.getParentFile().getAbsolutePath() + "/index.html"));
items.add(new Item("<![CDATA[" + getTitle(new File(hasIndex.getParentFile().getAbsolutePath() + "/index.html")) + "]]>", "<![CDATA[" + metas[0] + "]]>", domain + hasIndex.getParentFile().getAbsolutePath().replace(url, ""), metas[1]));
find(new File (file.getAbsolutePath() + "/" + str), domain);
}
}
}
}
public static boolean write(File location, String data, boolean readOnly) {
try {
FileWriter fw = new FileWriter(location, false);
fw.write(data);
fw.close();
if (readOnly) {
boolean bn = location.setReadOnly();
}
return true;
} catch (Exception exception) {
exception.printStackTrace();
}
return false;
}
public static String read(java.io.File fe, String line) {
try {
if (!fe.exists()) {
return null;
}
FileReader fr = new FileReader(fe);
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
String ln;
while ((ln = br.readLine()) != null) {
sb.append(ln);
sb.append(line);
}
fr.close();
br.close();
return sb.toString();
} catch (Exception exception) {
exception.printStackTrace();
}
return null;
}
}
class Item {
public String title;
public String description;
public String link;
public String medium_url;
public Item(String title, String description, String link, String medium_url) {
this.title = title;
this.description = description;
this.link = link;
this.medium_url = medium_url;
}
}