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.
Posted in Books, Distractions, Scripts | 2 Comments »
I changed the look of the website a little while back, as you may have noticed. It is quite a significant departure from the previous style and layout. The two main changes are the new colour scheme and the inclusion of a number of feeds on the index page.
Colour scheme
I was inspired to change to a much darker scheme having read an article on how the colours effect the energy required to display a website. Essentially, bright and light colours require more energy from your monitor.
Feeds
These feeds represent online content which I have created. Although in the case of recommended posts and bookmarks this creation might be considered a bit abstract. This was an idea I had a long while ago, and then not so long ago came across a way of quickly and crudely implementing it.
Up until today the feeds have been read, parse and output each time the page was called. This was causing significant delays in the page loading times. As of today a static file containing the parsed feeds is generated every 12 hours. The index page then calls on the static file when a request is made which means that no time is wasted on parsing the feed and preparing the output.
For those interested I’ve done this quite simply by writing taking a php file that was parsing the feeds and having it write the output to a static file. I then have a cron job run the php file at 9am and 9pm everyday:
0 9,21 * * * /usr/bin/php -q /PATH/generate_static_feeds.php > /dev/null
The last bit of that command “/dev/null” sends the output into the ether. Without it the server will e-mail the output of the script… which for some purposes can be rather useful.
In time I’ll be working on a more complex way of displaying the recommended posts, which will allow me to write a little note to go with each explaining why I have recommended them.
Posted in Information, Scripts, Website | No Comments »
Over the Easter holiday I managed to find some time to tinker with a few new features for my website.
Statistics
Update This is no longer available
This grew out of my insatiable curiosity which has meant I have sought to record visitors to my website for some time now. I forced myself around to doing so, managing to get it started by combining it with a new fascination for Google Maps. There are quite a few features I would like to try to work into it, but I think the basics are there. You are being watched! (But only if you visit the index page).
A tool for drawing routes onto Google Maps, and associating information with it. This is very much in development at the moment, and there are lot of things I’m not particularly happy with. This came from a need to display migration routes of my ancestors to be place on the family website my dad was is developing.
Posted in Scripts, Website | No Comments »