package Com.Vn.screenmatch.main;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Exercise {
static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
System.out.println("What do you wanna read? ");
var reading = sc.nextLine().replace(" ", "+");
String address = "https://www.googleapis.com/books/v1/volumes?q=" + reading;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(address))
.build();
HttpResponse<String> response = client
.send(request, HttpResponse.BodyHandlers.ofString());
String json = response.body();
JsonObject root = JsonParser.parseString(json).getAsJsonObject();
JsonObject firstBook = root
.getAsJsonArray("items")
.get(0)
.getAsJsonObject()
.getAsJsonObject("volumeInfo");
String title = firstBook.get("title").getAsString();
String author = firstBook.getAsJsonArray("authors").get(0).getAsString();
String publisher = firstBook.get("publisher").getAsString();
String published = firstBook.get("publishedDate").getAsString();
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Publisher: " + publisher);
System.out.println("Published: " + published);
}
}