Yahoo! Search Python API Howto

I guess most of you would’ve heard the announcement by now that a Yahoo! Search Developer Network a.k.a. YSDN has been started. The idea is to build a community around Yahoo! Search by opening up search webservices (with documentation) along with mailing lists, a blog and even a wiki. I haven’t had much time to explore it, but here’s what I’ve explored so far. This is more of a ‘how i did it’ rather than ‘howto’ but oh well, let’s get on with it.

First, I got an application ID. You need a Yahoo! ID to get a appID. Since I had already logged into my mail, I didn’t need to sign in. I gave a very unimaginative name ‘qpicsearch’ – I was thinking of creating an image search GUI using PyQt. I clicked on ‘Submit Registration’ and done.

I downloaded the SDK and read the FAQ. Python is my language of choice and as you can see from the title of this post, I am concentrating on that. Then, I installed the Python API:

[code]
python setup.py build
sudo python setup.py install
[/code]

It first gave me an error that /usr/lib/python2.3/config/Makefile was not found, so I installed the python-dev debian package, the installation then proceeded smoothly.

The pYsearch-1.0/docs/ directory has some nifty pydoc-generated documentation. It helped me get started with my first program:

[python]

!/usr/bin/env python

import sys
from yahoo.search.webservices import ImageSearch

app_id = ‘qpicsearch’ # your appid here!
searcher = ImageSearch(app_id)
searcher.query = sys.argv[1]
searcher.results = 1

results = searcher.parse_results()
for result in results:

    for key, value in result.items():
            print key, '->', value

[/python]

and I ran it like this:

[code]
$ python qpicsearch1.py bangalore
Thumbnail -> {‘Url’: u’http://re2.mm-c.yimg.com/image/548038611′, ‘Width’: 120, ‘Height’: 81}
Publisher ->
RefererUrl -> http://www.zonevoyage.com/photos/inde/imagepages/image1.htm
Copyright ->
Title -> IND Bangalore VBangal1
Url -> http://www.zonevoyage.com/photos/inde/images/IND-Bangalore-VBangal1.jpg
ClickUrl -> http://www.zonevoyage.com/photos/inde/images/IND-Bangalore-VBangal1.jpg
Summary ->
Width -> 450
FileSize -> 43602
Height -> 304
FileFormat -> jpeg
Restrictions ->
[/code]

That’s an amazingly small program and does so much cool stuff. Leif has designed a very intuitive API. Hats off to him!

Next, I wanted to create a GUI using PyQt. So, I opened the Qt Designer and drag-and-dropped a line input, button and iconview widgets.

Qt Designer

I used urllib2 to download the actual images and then used QIconViewItem to display each of the images. I even put in a nifty progress bar that indicates how many images have been downloaded. So, here’s the result!

QPicView GUI to search for images using Yahoo! Search

Pretty neat, eh? There’s actually much much more – local search, news search, video search and of course, the web search. I wonder what kind of applications will start using the Y! Search API. There’s a whole range of possibilities. As the Chinese proverb goes, ‘May you live in interesting times!’

Note:

The source files are here. This is just a 20 minute hack, so if you are using this, YMMV.

Since I used the GPLed edition of Qt, the QPicSearch source is also GPLed. However, you can reuse the idea, as you wish, in your own applications. Update: As ac commented, I found out that I can release PyQt-based apps using any GPL-compatible license. So, the code is under BSD license now.

I have set the number of results to just 3 in the source. Change this number to whatever you want and run ./run.sh to generate new code from the uic file and it will automatically run the application as well. This requires pyuic to be installed.

Subscribe to Swaroop CH

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe