#37 java: daum book search api
This commit is contained in:
61
java/api/src/main/java/kr/book/search/Book.java
Normal file
61
java/api/src/main/java/kr/book/search/Book.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package kr.book.search;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
private String authors;
|
||||||
|
private String publisher;
|
||||||
|
private String thumbnail;
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String title, String authors, String publisher, String thumbnail) {
|
||||||
|
this.title = title;
|
||||||
|
this.authors = authors;
|
||||||
|
this.publisher = publisher;
|
||||||
|
this.thumbnail = thumbnail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthors() {
|
||||||
|
return authors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthors(String authors) {
|
||||||
|
this.authors = authors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPublisher() {
|
||||||
|
return publisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPublisher(String publisher) {
|
||||||
|
this.publisher = publisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getThumbnail() {
|
||||||
|
return thumbnail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setThumbnail(String thumbnail) {
|
||||||
|
this.thumbnail = thumbnail;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Book{" +
|
||||||
|
"title='" + title + '\'' +
|
||||||
|
", author='" + authors + '\'' +
|
||||||
|
", publisher='" + publisher + '\'' +
|
||||||
|
", thumbnail='" + thumbnail + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
31
java/api/src/main/java/kr/book/search/BookSearchMain.java
Normal file
31
java/api/src/main/java/kr/book/search/BookSearchMain.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package kr.book.search;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class BookSearchMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String apiKey = args[0];
|
||||||
|
KakaoBookApi.setApiKey(apiKey);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
System.out.print("도서 제목을 입력하세요: ");
|
||||||
|
String title = scanner.nextLine();
|
||||||
|
|
||||||
|
List<Book> books = KakaoBookApi.searchBooks(title);
|
||||||
|
|
||||||
|
if (books.isEmpty()) {
|
||||||
|
System.out.println("검색 결과가 없습니다.");
|
||||||
|
} else {
|
||||||
|
String filename = "도서목록.pdf";
|
||||||
|
PdfGenerator.generateBookListPdf(books, filename);
|
||||||
|
System.out.println(filename + " 파일이 생성되었습니다.");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("에러가 발생하였습니다: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
60
java/api/src/main/java/kr/book/search/KakaoBookApi.java
Normal file
60
java/api/src/main/java/kr/book/search/KakaoBookApi.java
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package kr.book.search;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class KakaoBookApi {
|
||||||
|
|
||||||
|
private static String API_KEY = "";
|
||||||
|
|
||||||
|
private static final String API_BASE_URL = "https://dapi.kakao.com/v3/search/book";
|
||||||
|
private static final OkHttpClient client = new OkHttpClient();
|
||||||
|
private static final Gson gson = new Gson();
|
||||||
|
|
||||||
|
public static void setApiKey(String apiKey) {
|
||||||
|
API_KEY = apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Book> searchBooks(String title) throws IOException {
|
||||||
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(API_BASE_URL).newBuilder();
|
||||||
|
urlBuilder.addQueryParameter("query", title);
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(urlBuilder.build())
|
||||||
|
.addHeader("Authorization", "KakaoAK " + API_KEY)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try (Response response = client.newCall(request).execute()) {
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
throw new IOException("Request failed: " + response);
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject jsonResponse = gson.fromJson(response.body().charStream(), JsonObject.class);
|
||||||
|
JsonArray documents = jsonResponse.getAsJsonArray("documents");
|
||||||
|
|
||||||
|
List<Book> books = new ArrayList<>();
|
||||||
|
|
||||||
|
for (JsonElement document : documents) {
|
||||||
|
JsonObject bookJson = document.getAsJsonObject();
|
||||||
|
Book book = new Book(
|
||||||
|
bookJson.get("title").getAsString(),
|
||||||
|
bookJson.get("authors").getAsString(),
|
||||||
|
bookJson.get("publisher").getAsString(),
|
||||||
|
bookJson.get("thumbnail").getAsString()
|
||||||
|
);
|
||||||
|
books.add(book);
|
||||||
|
}
|
||||||
|
return books;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
86
java/api/src/main/java/kr/book/search/PdfGenerator.java
Normal file
86
java/api/src/main/java/kr/book/search/PdfGenerator.java
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
package kr.book.search;
|
||||||
|
|
||||||
|
import com.itextpdf.io.font.PdfEncodings;
|
||||||
|
import com.itextpdf.io.image.ImageData;
|
||||||
|
import com.itextpdf.io.image.ImageDataFactory;
|
||||||
|
import com.itextpdf.kernel.colors.ColorConstants;
|
||||||
|
import com.itextpdf.kernel.font.PdfFont;
|
||||||
|
import com.itextpdf.kernel.font.PdfFontFactory;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||||
|
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||||
|
import com.itextpdf.layout.Document;
|
||||||
|
import com.itextpdf.layout.element.Cell;
|
||||||
|
import com.itextpdf.layout.element.Image;
|
||||||
|
import com.itextpdf.layout.element.Paragraph;
|
||||||
|
import com.itextpdf.layout.element.Table;
|
||||||
|
import com.itextpdf.layout.properties.TextAlignment;
|
||||||
|
import com.itextpdf.layout.properties.UnitValue;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PdfGenerator {
|
||||||
|
|
||||||
|
public static void generateBookListPdf(List<Book> books, String filename) throws FileNotFoundException {
|
||||||
|
PdfWriter pdfWriter = new PdfWriter(filename);
|
||||||
|
PdfDocument pdf = new PdfDocument(pdfWriter);
|
||||||
|
Document document = new Document(pdf);
|
||||||
|
document.setFontSize(12);
|
||||||
|
|
||||||
|
PdfFont font;
|
||||||
|
|
||||||
|
try {
|
||||||
|
font = PdfFontFactory.createFont("NanumGothicLight.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED);
|
||||||
|
document.setFont(font);
|
||||||
|
|
||||||
|
Paragraph titleParagraph = new Paragraph("도서 목록");
|
||||||
|
titleParagraph.setFontSize(24);
|
||||||
|
titleParagraph.setTextAlignment(TextAlignment.CENTER);
|
||||||
|
titleParagraph.setBold();
|
||||||
|
document.add(titleParagraph);
|
||||||
|
|
||||||
|
Table table = new Table(UnitValue.createPercentArray(new float[]{2, 2, 2, 2}));
|
||||||
|
table.setWidth(UnitValue.createPercentValue(100));
|
||||||
|
table.setMarginTop(20);
|
||||||
|
|
||||||
|
table.addHeaderCell(createCell("제목", true));
|
||||||
|
table.addHeaderCell(createCell("저자", true));
|
||||||
|
table.addHeaderCell(createCell("출판사", true));
|
||||||
|
table.addHeaderCell(createCell("이미지", true));
|
||||||
|
|
||||||
|
for (Book book : books) {
|
||||||
|
table.addCell(createCell(book.getTitle(), false));
|
||||||
|
table.addCell(createCell(book.getAuthors(), false));
|
||||||
|
table.addCell(createCell(book.getPublisher(), false));
|
||||||
|
|
||||||
|
try {
|
||||||
|
ImageData imageData = ImageDataFactory.create(book.getThumbnail());
|
||||||
|
Image image = new Image(imageData);
|
||||||
|
image.setAutoScale(true);
|
||||||
|
table.addCell(new Cell().add(image).setPadding(5));
|
||||||
|
} catch (Exception e) {
|
||||||
|
table.addCell(createCell("이미지 불러오기 실패", false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.add(table);
|
||||||
|
document.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Cell createCell(String content, boolean isHeader) {
|
||||||
|
Paragraph paragraph = new Paragraph(content);
|
||||||
|
Cell cell = new Cell().add(paragraph);
|
||||||
|
cell.setPadding(5);
|
||||||
|
|
||||||
|
if (isHeader) {
|
||||||
|
cell.setBackgroundColor(ColorConstants.LIGHT_GRAY);
|
||||||
|
cell.setFontSize(14);
|
||||||
|
cell.setBold();
|
||||||
|
}
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
java/api/도서목록.pdf
Normal file
BIN
java/api/도서목록.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user