-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
130 lines (109 loc) · 4.4 KB
/
Main.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
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
import java.net.URL;
public class Main{
public static void main(String[] args) throws Exception {
//题目标号的范围
Integer num = 1099;
Integer MaxNum = 6543;
for(int i=num;i<= MaxNum;i++){
StringBuffer sb = null;
try {
sb = generateMarkdownContent(i);
} catch (Exception e) {
e.printStackTrace();
}
String filePath = "D:\\tmp\\HDU\\HDU-"+i+".md";
toFile(sb,filePath);
}
}
public static void toFile(StringBuffer sb,String filePath) throws Exception {
File markdownFile = new File(filePath);
if(markdownFile.exists()){
if(!markdownFile.delete()){
System.err.println(filePath +"失败...");
}
}
if(markdownFile.createNewFile()){
FileWriter fw=new FileWriter(filePath);
fw.write(sb.toString());
fw.close();
}else{
System.err.println(filePath+ "失败...");
}
System.out.println(filePath + "成功...");
}
public static StringBuffer generateMarkdownContent(Integer num) throws Exception {
String title = null;
String limits = null;
StringBuffer sb = new StringBuffer();
Document document = Jsoup.connect("http://acm.hdu.edu.cn/showproblem.php?pid=" + num)
.userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '\n" +
" 'Chrome/51.0.2704.63 Safari/537.36")
.get();
Elements elementList= document.getElementsByTag("h1");
/**
* 每个问题的标题
*/
if(elementList.size() == 1){
title = elementList.get(0).html();
}
sb.append("## "+title + " " + num + "\n");
/**
* Limits
*
*/
Elements limites = document.getElementsByTag("span");
limits = limites.get(0).html();
sb.append( limits + "\n");
/**
*
*/
Elements elements = document.getElementsByClass("panel_title");
Elements elements1 = document.getElementsByClass("panel_content");
if( elements.size() == elements1.size() ){
for(int i=0;i<elements.size();i++){
//System.out.println("=====================");
sb.append( "## " + elements.get(i).html() + "\n");
//System.out.println("====================");
Element contentElement = elements1.get(i);
if(contentElement.select("img[src$=.gif]") != null){
//说明这个tag中有img标签
Elements images = contentElement.select("img[src$=.gif]");
for(Element e : images){
String tmp = e.attr("src");
String imgPath = "http://acm.hdu.edu.cn" + tmp;
//System.out.println(imgPath);
//String localPath = downloadPicture(imgPath, tmp.substring(tmp.lastIndexOf("/"),tmp.length()));
e.attr("src",imgPath);
}
//tag处理完毕
}
sb.append(contentElement.html() + "\n");
}
}
return sb;
}
//有考虑过离线图片,后来觉得并不是多现实,而且图片下载到的话会很混乱,所以就改成了链接的方式。
//链接url下载图片
private static String downloadPicture(String urlList,String path) throws Exception {
URL url = null;
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
File file = new File(path);
FileOutputStream fileOutputStream = new FileOutputStream(file);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
return file.getAbsolutePath();
}
}