Android Open Source - Scanner-For-Zotero Google Books A P I Client






From Project

Back to project page Scanner-For-Zotero.

License

The source code is released under:

GNU General Public License

If you think the Android project Scanner-For-Zotero listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/** 
 * Copyright 2011 John M. Schanck//  w ww.  ja v  a2  s  .  co  m
 * 
 * ScannerForZotero is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ScannerForZotero is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ScannerForZotero.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ale.scanner.zotero.web.googlebooks;

import java.net.URI;

import org.ale.scanner.zotero.Util;
import org.ale.scanner.zotero.data.CreatorType;
import org.ale.scanner.zotero.data.ItemField;
import org.ale.scanner.zotero.data.ItemType;
import org.ale.scanner.zotero.web.APIHandler;
import org.ale.scanner.zotero.web.APIRequest;
import org.ale.scanner.zotero.web.HttpsClient;
import org.ale.scanner.zotero.web.RequestQueue;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;

public class GoogleBooksAPIClient {

    public static final String BOOK_SEARCH_ISBN = "https://www.googleapis.com/books/v1/volumes?prettyPrint=flase&q=isbn:";

    public static final String EXTRA_ISBN = "ISBN";

    private DefaultHttpClient mHttpsClient;

    private RequestQueue mRequestQueue;

    private APIHandler mHandler;
    
    public GoogleBooksAPIClient() {
        mHandler = GoogleBooksHandler.getInstance();
        mHttpsClient = HttpsClient.getInstance();
        mRequestQueue = RequestQueue.getInstance();
    }

    private APIRequest newRequest(){
        return new APIRequest(mHandler, mHttpsClient);
    }

    public void isbnLookup(String isbn) {
        APIRequest r = newRequest();
        r.setHttpMethod(APIRequest.GET);
        r.setURI(URI.create(BOOK_SEARCH_ISBN+isbn));
        Bundle extra = new Bundle();
        extra.putString(GoogleBooksAPIClient.EXTRA_ISBN, isbn);
        r.setExtra(extra);

        mRequestQueue.enqueue(r);
    }

    public static JSONObject translateJsonResponse(String isbn, String resp){
        // Returns empty JSONObject on failure.
        JSONObject translation = new JSONObject();
        JSONObject jsonResp = null;
        try {
            jsonResp = new JSONObject(resp);

            // Google search always returns "books#volumes"
            String kind = jsonResp.optString("kind");
            JSONArray respItems = jsonResp.optJSONArray("items");
            if(kind == null || !kind.equals("books#volumes") || respItems == null) {
                return null;
            }

            translation.put("items", new JSONArray());
            for(int i=0; i < respItems.length(); i++){
                // oItem is google's result, tItem is our translation of it
                JSONObject orig = respItems.getJSONObject(i);
                JSONObject volInfo = orig.optJSONObject("volumeInfo");
                if(volInfo == null){
                    continue;
                }
                JSONObject trans = new JSONObject();

                /* Set the itemType XXX: Always 'book' */
                trans.put(ItemType.type, ItemType.book);

                /* Get ISBN/ISSN info */
                String bestId = isbn;
                String bestType = ItemField.ISBN;
                JSONArray identifiers = volInfo.optJSONArray("industryIdentifiers");
                for(int j=0; identifiers != null && j<identifiers.length(); j++){
                    JSONObject identifier = identifiers.getJSONObject(j);
                    String idType = identifier.getString("type");

                    String id = identifier.getString("identifier");
                    if(bestId == null){
                        bestId = id;
                        if(idType.equals("ISSN")) bestType = ItemField.ISSN;
                    }
                    if(Util.isbnMatch(id, isbn)){
                        if(bestId != id && bestId.length() < id.length()){
                            bestId = id;
                            if(idType.equals("ISSN")) bestType = ItemField.ISSN;
                        }
                        break;
                    }
                }
                trans.put(bestType, bestId);

                /* Get title  */
                String subtitle = volInfo.optString("subtitle");
                if(!TextUtils.isEmpty(subtitle)){
                    trans.put(ItemField.title, 
                            volInfo.optString("title") + ": " + subtitle);
                }else{
                    trans.put(ItemField.title, volInfo.optString("title"));
                }

                /* Get Creators  */
                JSONArray creators = new JSONArray();
                JSONArray authors = volInfo.optJSONArray("authors");
                for(int j=0; authors != null && j<authors.length(); j++){
                    JSONObject author = new JSONObject();
                    author.put(CreatorType.type, CreatorType.Book.get(0));
                    author.put(ItemField.Creator.name, authors.get(j));
                    creators.put(author);
                }
                if(creators.length() > 0)
                    trans.put(ItemField.creators, creators);

                /* Get Other info  */
                trans.put(ItemField.publisher, volInfo.optString("publisher"))
                     .put(ItemField.date, volInfo.optString("publishedDate"))
                     .put(ItemField.numPages, volInfo.optString("pageCount"))
                     .put(ItemField.language, volInfo.optString("language"));

                translation.accumulate("items", trans);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return translation;
    }
}




Java Source Code List

org.ale.scanner.zotero.BibDetailJSONAdapter.java
org.ale.scanner.zotero.BibItemListAdapter.java
org.ale.scanner.zotero.Dialogs.java
org.ale.scanner.zotero.EditItemActivity.java
org.ale.scanner.zotero.LoginActivity.java
org.ale.scanner.zotero.MainActivity.java
org.ale.scanner.zotero.ManageAccountsActivity.java
org.ale.scanner.zotero.PString.java
org.ale.scanner.zotero.PendingListAdapter.java
org.ale.scanner.zotero.SafeViewFlipper.java
org.ale.scanner.zotero.Util.java
org.ale.scanner.zotero.data.Access.java
org.ale.scanner.zotero.data.Account.java
org.ale.scanner.zotero.data.BibItemDBHandler.java
org.ale.scanner.zotero.data.BibItem.java
org.ale.scanner.zotero.data.Collection.java
org.ale.scanner.zotero.data.CreatorType.java
org.ale.scanner.zotero.data.Database.java
org.ale.scanner.zotero.data.Group.java
org.ale.scanner.zotero.data.ItemField.java
org.ale.scanner.zotero.data.ItemType.java
org.ale.scanner.zotero.web.APIHandler.java
org.ale.scanner.zotero.web.APIRequest.java
org.ale.scanner.zotero.web.HttpsClient.java
org.ale.scanner.zotero.web.RequestQueue.java
org.ale.scanner.zotero.web.googlebooks.GoogleBooksAPIClient.java
org.ale.scanner.zotero.web.googlebooks.GoogleBooksHandler.java
org.ale.scanner.zotero.web.worldcat.WorldCatAPIClient.java
org.ale.scanner.zotero.web.worldcat.WorldCatHandler.java
org.ale.scanner.zotero.web.zotero.ZoteroAPIClient.java
org.ale.scanner.zotero.web.zotero.ZoteroHandler.java