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.

August 23rd, 2010 at 11:00
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
August 23rd, 2010 at 15:31
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
September 14th, 2010 at 21:48
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
October 5th, 2010 at 05:16
Thanks for the script.
The dialog box, “Another?” keeps coming back up when I press “upload.” Any idea why?
Thanks again.
October 5th, 2010 at 15:25
@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.
July 12th, 2011 at 20:26
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
October 15th, 2012 at 19:49
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.