forked from eugenejade/JavaTPCProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject03_D.java
More file actions
89 lines (81 loc) · 2.89 KB
/
Project03_D.java
File metadata and controls
89 lines (81 loc) · 2.89 KB
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import kr.inflearn.ExcelVO;
public class Project03_D {
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("책제목:");
String title=br.readLine();
System.out.print("책저자:");
String author=br.readLine();
System.out.print("출판사:");
String company=br.readLine();
ExcelVO vo=new ExcelVO(title, author, company);
getIsbnImage(vo);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void getIsbnImage(ExcelVO vo) {
try {
String openApi="https://openapi.naver.com/v1/search/book_adv.xml?d_titl="
+ URLEncoder.encode(vo.getTitle(), "UTF-8")
+ "&d_auth="+URLEncoder.encode(vo.getAuthor(), "UTF-8")
+ "&d_publ="+URLEncoder.encode(vo.getCompany(), "UTF-8");
URL url=new URL(openApi);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("X-Naver-Client-Id", "hQLwYhKiYEGobEsmc8fU");
con.setRequestProperty("X-Naver-Client-Secret", "CndvMziCkZ");
int responseCode=con.getResponseCode();
BufferedReader br1=null;
if(responseCode==200) {
br1=new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
}else {
br1=new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String inputLine;
StringBuffer response=new StringBuffer();
while((inputLine=br1.readLine())!=null) {
response.append(inputLine);
}
br1.close();
//System.out.println(response.toString());
// isbn, image
Document doc=Jsoup.parse(response.toString());
//System.out.println(doc.toString());
Element total=doc.select("total").first();
//System.out.println(total.text());
if(!(total.text().equals("0"))) {
Element isbn=doc.select("isbn").first();
String isbnStr=isbn.text();
//System.out.println(isbnStr);
String isbn_find=isbnStr.split(" ")[1];
vo.setIsbn(isbn_find);
//-------------------------------------
//Element img=doc.select("img").first();
//System.out.println(img.toString());
String imgDoc=doc.toString();
String imgTag=imgDoc.substring(imgDoc.indexOf("<img>")+5);
//System.out.println(imgTag); // http://~~~~~~~~~~~~~~
String imgURL=imgTag.substring(0, imgTag.indexOf("?"));
System.out.println(imgURL);
String fileName=imgURL.substring(imgURL.lastIndexOf("/")+1);
System.out.println(fileName);
vo.setImgurl(fileName);
System.out.println(vo);
}else {
System.out.println("검색데이터가 없습니다.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}