forked from mrepol742/mrepol742.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sitemap.java
153 lines (137 loc) · 5.62 KB
/
Sitemap.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
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.jsoup.*;
import org.jsoup.select.*;
import org.jsoup.helper.*;
import org.jsoup.internal.*;
import org.jsoup.nodes.*;
import org.jsoup.parser.*;
class Sitemap {
private static List<Link> links = new ArrayList<>();
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
private static String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\" href=\"/css/sitemap.xsl\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:video=\"http://www.google.com/schemas/sitemap-video/1.1\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\">\n";
private static String footer = "</urlset>";
private static String body = "<url>\n" +
" <loc>%1$s</loc>\n" +
" <lastmod>%2$s</lastmod>\n";
private static StringBuilder sitemap = 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";
find(new File(url), domain);
sitemap.append(header);
for (Link link: links) {
sitemap.append(String.format(body, link.url, link.date));
sitemap.append(link.more);
sitemap.append("</url>\n");
}
sitemap.append(footer);
if (write(new File(url + "/sitemap.xml"), sitemap.toString(), false)) {
System.out.println("\nSitemap generated for " + domain);
} else {
System.out.println("\nFailed to generate sitemap.");
}
}
public static String getImages(File file) {
StringBuilder images = new StringBuilder();
Document doc = Jsoup.parse(read(file, "\n"));
Elements image = doc.getElementsByTag("img");
for (Element el : image) {
String src = el.attr("src");
if (src.startsWith("/images/")) {
images.append(" <image:image>\n <image:loc>" + domain + src + "</image:loc>\n </image:image>");
}
}
return images.toString();
}
public static String getVideos(File file) {
StringBuilder videos = new StringBuilder();
Document doc = Jsoup.parse(read(file, "\n"));
Elements image = doc.getElementsByTag("source");
for (Element el : image) {
String src = el.attr("src");
if (src.startsWith("/videos/")) {
videos.append(" <video:video>\n <video:title>" + (src.replaceAll("%20", " ").replaceAll("/videos/","").replaceAll(".mp4", ""))+ "</video:title>\n <video:content_loc>" + domain + src + "</video:content_loc>\n </video:video>");
}
}
return videos.toString();
}
public static void find(File file, String domain) {
if (file.list() == null) {
System.out.println("no index " + file.toString());
return;
}
if (file.isDirectory() && !isHome) {
File root = new File(url+"/index.html");
System.out.println(format.format(root.lastModified()) + " | " + domain);
links.add(new Link(domain, format.format(root.lastModified()), getImages(root) + "\n" + getVideos(root)));
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, ""));
links.add(new Link(domain + hasIndex.getParentFile().getAbsolutePath().replace(url, "") , format.format(hasIndex.lastModified()), getImages(hasIndex) + "\n" + getVideos(hasIndex)));
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 Link {
public String url;
public String date;
public String more;
public Link(String url, String date, String more) {
this.url = url;
this.date = date;
this.more = more;
}
}