Saturday, 26 December 2009

A Ruby script to search bookstores online

I started dabbling in Ruby some weeks back. The initial interest was sparked after reading "Treating Code as an Essay" (Yukihiro Matsumoto) - one of the chapters in Beautiful Code. So I started doing these bootstrapping exercises in Ruby. Some of the exercises are good - but nothing beats doing a small project to learn a new language.

I buy a lot of books, mostly online. There are a few good online bookstores in India, notably Flipkart.com, Infibeam.com and Indiaplaza.in (Sadly, Amazon does not have full-fledged shipping to India yet). The way I usually search for a book in online bookstores is (was, till now)

  1. Go to books.google.com and enter the book title
  2. Click on the best match
  3. Click on 'All Sellers' on the left of the page
  4. The Indian bookstores are usually listed towards the bottom. It does not include all stores, and sometimes the prices are not listed. I have to go to each individual site and check them out.

I wanted to collapse these steps into one - a simple script that would accept the name of the book and show results from all these bookstores, with comparative pricing. And the result was this

http://github.com/talonx/book-search

It's in Ruby, runs from the command line and writes the output to an HTML in the same directory called 'search.html'. Much needs to be done, like

  • Price based listing with the lowest on top
  • A web interface for the search
  • Add more bookstores - it's only Flipkart.com, Infibeam.com, Indiaplaza and Bookadda.com right now.

To run the script, type this (you need Ruby 1.8.x, available from http://www.ruby-lang.org/en/downloads/ and the Hpricot HTML parser library, available from http://github.com/whymirror/hpricot)
ruby lib\book-search.rb "<book title (in quotes if it has spaces)>"

Saturday, 12 December 2009

Do that side project

Do that side project.

How many times have you told yourself

  • I'll start that open source project I've been thinking of
  • I'll write that utility which will make my job easier
  • I'll enroll for that course on Artificial Intelligence and write that amazing recommendation system

and then did nothing?

Well, guess what. Time passes. Yes, really.

Anne Dillard said

"How we spend our days is, of course, how we spend our lives."
Think about that for a moment.

Don't waste time on thinking about when to think about planning to think about thinking about when to start thinking about doing it. Do it now.

Here are some more resources on the subject -

  1. Shut up and Hack - http://www.slideshare.net/bluesmoon/shut-up-and-hack
  2. Do it Now - http://www.stevepavlina.com/articles/do-it-now.htm
  3. Do it Fucking now - http://seoblackhat.com/2007/01/29/do-it-fucking-now/
  4. Chris Wanstrath's keynote - http://gist.github.com/6443

Monday, 7 December 2009

Adding MySQL server instances using mysqlmanager

The MySQL instance manager - mysqlmanager - provides a way to manage multiple MySQL server instances on the same installation. All these instances use a common my.cnf file - but each can be configured individually (using the same file). mysqlmanager itself provides a command line interface to control the individual instances.

Part of a sample mysql.cnf with multiple mysql instances

[mysqld1]
user = mysql
datadir = /data/mysql-1
socket = /tmp/mysql-1.sock
port = 3306

[mysqld2]
user = mysql
datadir = /data/mysql-2
socket = /tmp/mysql-2.sock
port = 3307

The ability to setup multiple database servers fast is particularly useful in development boxes where fresh DBs need to be created often. In my team, we often need to do this. Every time a new DB has to be setup, we have to go through the steps of creating a datadir, installing the system tables, adding a root password, adding the entries to the my.cnf file and starting the instance using the mysqlmanager shell.

So I whipped up a small Linux shell script which automates this process.

Here it is.

It's still in a quite primitive state - but it works!

Usage is simple -
add-mysql-instance.sh mysql config-file-location datadir groupname username password instance port instance-name mysqlmanager-user mysqlmanager-password mysqlmanager-socket-file

Of course, mysqlmanager has to be running for this to work.

I'll be adding improvements to this script - like the ability to generate a mysql instance name based on existing instances (instance names are usually mysqld1, mysqld2 etc), picking up the user name from the file itself etc.