How to index your Book Library

Hello guys! I have pretty big book library and people tend to lend books and never return them. So I asked myself: What would be the best way to track my books ? Then it hit me. Smartphones can read barcodes and Google can recognize books by its barcode. I downloaded app for android called BookShelf. I created new shelf for every shelf I have and scanned all those barcodes. Then I created app in Java which reads xmls exported by BookShelf and uses Google API to recognize books. The I used my own API to manage database.

package sk.drndos.kniznica.ui;</pre>
import sk.drndos.kniznica.tables.Book;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author DeathX
 */
public class Kniznica {

public static Database db;

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 db = new Database("localhost", "books", "root", "");
 Kniznica k = new Kniznica();
 }

public Kniznica() {
 //readFromXML();
 /* Create and display the form */
 java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
 new MainInterface().setVisible(true);
 }
 });
 }

public static void readFromXML() {
 File d = new File("xmls/");
 File[] files = d.listFiles();
 for (int i = 0; i < files.length; i++) {
 try {
 parseXML(files[i]);
 } catch (XMLStreamException ex) {
 Logger.getLogger(Kniznica.class.getName()).log(Level.SEVERE, null, ex);
 } catch (UnsupportedEncodingException ex) {
 Logger.getLogger(Kniznica.class.getName()).log(Level.SEVERE, null, ex);
 } catch (FileNotFoundException ex) {
 Logger.getLogger(Kniznica.class.getName()).log(Level.SEVERE, null, ex);
 }
 }
 }

public static void parseXML(File xml)
 throws XMLStreamException, UnsupportedEncodingException, FileNotFoundException {
 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
 XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(xml));
 String currentParsing = "";

while (reader.hasNext()) {
 XMLEvent event = reader.nextEvent();

if (event.getEventType() == XMLEvent.START_ELEMENT) {
 StartElement startElement = event.asStartElement();
 String element = startElement.getName().getLocalPart();
 if ("field".equals(element)) {
 event = (XMLEvent) reader.next();
 if (event.isCharacters()) {
 String elementTwo = event.asCharacters().getData();
 currentParsing = elementTwo;
 }
 } else if ("value".equals(element)) {
 if (currentParsing.equals("11")) {
 event = (XMLEvent) reader.next();
 if (event.isCharacters()) {
 String elementTwo = event.asCharacters().getData();
 Book b = fromIsbn(elementTwo);
 System.out.println(b);
 if (b != null) {
 db.queryInsert("INSERT INTO book (title,publishedDate,pagecount,language,isbn,authors,location) VALUES(?,?,?,?,?,?,?)", b.getTitle(), b.getPublishedDate(), b.getPageCount(), b.getLanguage(), b.getIsbn(), b.getAuthors(), xml.getName());
 }
 }
 }
 }
 }
 }
 reader.close();
 }

private static Book fromIsbn(String isbn) {
 Book b = null;
 URLConnection is = null;
 try {
 URL url = new URL("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn + "&key=AIzaSyDtMtNvV1nG1NiBRqYAxR6OCm1_dkc4kOk");
 is = url.openConnection();
 Reader r = new InputStreamReader(is.getInputStream(), "UTF-8");
 StringBuilder buf = new StringBuilder();
 while (true) {
 int ch = r.read();
 if (ch < 0) {
 break;
 }
 buf.append((char) ch);
 }
 r.close();
 String str = buf.toString();
 try {
 JSONObject json = (JSONObject) new JSONParser().parse(str);
 if ((JSONArray) json.get("items") == null) {
 System.out.println(json.toString());
 System.out.println(isbn);
 } else {
 for (Object one : (JSONArray) json.get("items")) {
 String title = "";
 String publishedDate = "";
 String language = "";
 Integer pageCount = 0;
 if (((JSONObject) ((JSONObject) one).get("volumeInfo")).get("title") != null) {
 title = ((JSONObject) ((JSONObject) one).get("volumeInfo")).get("title").toString();
 }
 if (((JSONObject) ((JSONObject) one).get("volumeInfo")).get("publishedDate") != null) {
 publishedDate = ((JSONObject) ((JSONObject) one).get("volumeInfo")).get("publishedDate").toString();
 }
 if (((JSONObject) ((JSONObject) one).get("volumeInfo")).get("language") != null) {
 language = ((JSONObject) ((JSONObject) one).get("volumeInfo")).get("language").toString();
 }
 if (((JSONObject) ((JSONObject) one).get("volumeInfo")).get("pageCount") != null) {
 pageCount = Integer.parseInt(((JSONObject) ((JSONObject) one).get("volumeInfo")).get("pageCount").toString());
 }
 List<Object> authors = ((JSONArray) ((JSONObject) ((JSONObject) one).get("volumeInfo")).get("authors"));
 String authorss = "";
 if (authors != null) {
 int i = 1;
 for (Object author : authors) {
 if (authors.size() == i) {
 authorss += author.toString();
 } else {
 authorss += author.toString() + ", ";
 }
 i++;
 }
 }
 b = new Book(title, authorss, publishedDate, pageCount, language, isbn);
 }
 }
 } catch (ParseException ex) {
 Logger.getLogger(Kniznica.class.getName()).log(Level.SEVERE, null, ex);
 }
 } catch (IOException ex) {
 Logger.getLogger(Kniznica.class.getName()).log(Level.SEVERE, null, ex);
 }
 return b;
 }
}

Leave a Comment

Your email address will not be published. Required fields are marked *