define - A lovely utility

I frequently use Google for finding several definitions. It feels always painful to run browser every time to grab some definitions. Being a terminal lover, I always wanted to do it on commandline. I wrote a few lines of python code, and its done.

I love this utility. Cheers !

#!/usr/bin/env python

import urllib2,re,sys

def usage():
	if sys.argv.__len__() is not 2:
		print "usage: define < word >\n"
	else:

		txt = define(sys.argv[1])
		if txt is not None:
        		print '\n\033[31mDefinition of ' + sys.argv[1].upper() + ':\033[0m\n'+ txt+ '\033[0m\n'
		else:
        		print "\nNo definition found\n"

def define(str):
	opener = urllib2.build_opener()
	opener.addheaders = [('User-agent', 'define grabber by t3rm1n4l')]
        try:

		txt=opener.open('http://www.google.com/search?q=define:'+ str)
        	txt=txt.read()
        	regex = re.compile("<li>[a-zA-Z,. ]*\.\.\.")
        	obj = regex.findall(txt)
        	return obj[0][4:]
 	except:
		pass
usage()

UPDATE: toolz suggested me that it would be nice to print all the definitions avaliable.

So I have updated the script with more options :)

get it here

Google is playing more with User-agent strings. You won’t find same search results for different User-agent strings :). That’s fun.

$ define keyword -all # for printing all definitions

$ define keyword -4 # For printing 4 definitions

$ define keyword # prints 1 definiton

define utility