Craig in feed form

Google Books android script An orange feed icon

The script below lets you scan multiple book barcodes and add the books directly to your Google Library.

Since getting a Nexus One I’ve been wanting to dabble with programming something for it, Java seemed far too hard. However, I quickly came across Scripting Layer for Android (SL4A) which lets you write quick scripts in a variety of languages; including my favourite, Python. The next challenge was to actually come up with a simple project to get my hands dirty.

Matt Cutts wrote a simple script that scans a barcode and pulls up its Google Books page, from which you can then click the ‘add to my library’ button, but this seemed far too clumsy and slow to me, so I settled on improving it. The script now lets you scan multiple books in series and then add them to your Google library by making use of the Google Books Search API.

See the code (public domain) below:

import android
from gdata.books.service import BookService
import gdata.books

email = 'youremail@gmail.com'
password = 'yourpassword'

droid = android.Android()

def dialog(items):
  title = 'Another?'
  droid.dialogCreateAlert(title)
  droid.dialogSetItems(items)
  droid.dialogSetPositiveButtonText('Add')
  droid.dialogSetNegativeButtonText('Exit')
  droid.dialogSetNeutralButtonText('Upload')
  droid.dialogShow()
  response = droid.dialogGetResponse().result
  return response['which']

def add_from_queue(self):
   for k,b in self.queue.items():
      self.add_item_to_library(b)
   return(True)

def get_by_barcode(self):
   (id, result, error) = droid.scanBarcode()
   if result is not True:
      return(False)
   isbn = int(result['extras']['SCAN_RESULT'])
   q = 'ISBN'+str(isbn)
   b = self.search(q, feed=self.ITEM_FEED).entry[0]
   self.queue[b.dc_title[0].text] = b
   return(True)

gdata.books.service.BookService.get_by_barcode = get_by_barcode
gdata.books.service.BookService.add_from_queue = add_from_queue
gdata.books.service.BookService.queue = {}
gdata.books.service.BookService.ITEM_FEED = gdata.books.service.ITEM_FEED

service = gdata.books.service.BookService()
service.ClientLogin(email, password)

service.get_by_barcode()

i = True
while i:
  response = dialog(service.queue.keys())

  if response == 'positive':
     service.get_by_barcode()

  elif response == 'neutral':
     service.add_from_queue()
     service.queue = {}

  else:
     i = False

droid.exit()

Updated: Thanks to sjb for pointing out a bug in the code; now fixed.

17 Responses to “Google Books android script”

  1. Argon0 Says:

    Have you published, or thought to publish, this as an app?

    At the moment I am looking for an app to be a front end to Google Books on my Android, to enter stuff (as your script seems to do) and to retrieve stuff.

    Ideally to create a catalogue on my Android which syncs with my Google Books Library…

    Argon0

  2. Craig Says:

    Argon0,

    I’m afraid I don’t have time to port this to produce a proper app; if I did I would have already done so ;) .

    As I state in the post, I consider the code to be in public domain, so if it is useful please feel free to take it as your own.

    Craig

  3. Ilan Says:

    This did not work for me,
    in def get_by_barcode(self):

    if result is not True:
    return(False)

    was always returning false, even (and I did not have any other case) when it found the book, so I removed it, and the code started working.

    a question: is there a way to select to which category the books will be added? I have some book shelfs and there are some by default.

    It always adds to favorites and thats not quite right for me.
    Thanks

  4. Phyllis Says:

    Thanks for the script.
    The dialog box, “Another?” keeps coming back up when I press “upload.” Any idea why?
    Thanks again.

  5. Craig Says:

    @Phyllis: This is the correct behaviour. The script is written such that when you select ‘upload’ the books that you’ve scanned are added to Google Books and the list is cleaned out; returning you to the scripts initial state. The script only exits when you select ‘Exit’. If you want it to exit after uploading add ‘i = False’ after ‘service.queue = {}’ in the while loop at the bottom of the script.

    @Ilan: There are various versions of the get_by_barcode() function around on the sl4a site, I basically chose one that worked with the barcode scanner I have installed…

    As for the categories, I agree about the adding to favourites not really being desirable but unfortunately I don’t know of a way of setting categories. There may be a way of doing it using the GBooks concept of a shelf but this isn’t implemented at a high level in the sl4a gbooks api layer; when I looked into it I didn’t have the time to implement it myself.

  6. Mac Says:

    An ISBN is not always an integer, but may have an “X” as a check “digit” in last place (at least if it’s an ISBN-10). Therefore
    isbn = int(result['extras']['SCAN_RESULT'])
    is wrong. You should use a string here.
    http://en.wikipedia.org/wiki/International_Standard_Book_Number

  7. Mike E Says:

    An ISBN is NEVER an integer. The rule is, if you’re not going perform maths on it then it’s a string. Same applies to phone numbers and area codes.

  8. polacy w szkocji Says:

    As the admin of this web site is working, no question very soon it will be famous, due
    to its feature contents.

  9. Effie Says:

    You need to come interact with us on Twitter, you make some
    wonderful factors in your blog so I make certain you
    would fit right in

    Here is my web-site – Effie

  10. Clash of Clans Gems Hack Says:

    Hello! This is my 1st comment here so I just wanted to
    give a quick shout out and tell you I genuinely enjoy reading
    your blog posts. Can you recommend any other blogs/websites/forums that cover the same subjects?

    Thanks a ton!

  11. whole life insurance cash value death benefit Says:

    So you want to know that they are an honest and stable company.
    Each and every insurance policy is not suited to all the people around.
    You’ll have peace of mind when you purchase adequate
    life insurance.

  12. nationwide life insurance quotes Says:

    After a few years, a cash value inside the policy will start
    to build up. If you have health insurance you won’t need to wait; you can access high quality
    treatment quickly and focus on getting better. When it comes to purchasing
    life insurance, price comparisons can save you quite a bit of money.

  13. www.youtube.com Says:

    I’m really enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more enjoyable for me to
    come here and visit more often. Did you hire out a developer to create your theme?

    Fantastic work!

    Check out my weblog … The Venus Factor Review (http://www.youtube.com)

  14. hcg recipes phase 2 apple Says:

    There are already a lot of rumors about our product, the
    HCG Diet, and we all would definitely prefer to finally are able to expand for this
    subject. s and not too long ago, a variety of studies have evolved which generated the availability of the we know as HCG weight-loss diet.

    The dietary plan incorporates the HCG hormone that improves and re-sets
    the metabolic processes for your system therefore someone doesn’t turn out
    having intensive wish to have food.

  15. casino gnomen Says:

    I could not resist commenting. Perfectly written!

    Here is my site: casino gnomen

  16. prlink.no Says:

    Hi there Dear, are you genuinely visiting this site on a regular basis, if so after that you will without doubt obtain nice experience.

    Look into my web-site – prlink.no

  17. GåNedivekt.Com Says:

    Very good information. Lucky me I discovered your blog by accident (stumbleupon).
    I’ve book-marked it for later!

    Here is my homepage GåNedivekt.Com

Leave a Reply

Archives

Categories