Program Yahoo! with REBOL

With REBOL, you can build a graphical interface to Yahoo! with minimal code.

Relative Expression-based Object Language (REBOL) is a technology that provides a lightweight method of distributed computing and communication. REBOL is a messaging language, which means it was built specifically to send messages across Internet protocols. REBOL is available for free, and there are two variations available for download:

REBOL/Core

This is the kernel, the heart of REBOL, with a command-line interface.

REBOL/View

This is a graphical client version, an extension of REBOL/Core for developers who want to work visually.

This hack creates a simple Yahoo! Image search widget with the REBOL/ View variation. The widget allows you to enter a keyword, display the first image in the results, and navigate between other results with Previous and Next buttons. This type of graphical user interface (GUI), which uses buttons in a familiar desktop-style application, is quite a bit different from the scripting, command-line applications in this book.

To begin, download REBOL/View for your platform at http://www.rebol.com/view-platforms.html. Next, you’ll need to download a modified version of Gavin F. McKenzie’s SAX XML parser at http://premshree.seacrow.com/code/rebol/xml-parse.r/view. Place xml-parse.r in the working directory where you’ll create the widget.

The Code

Unlike with most GUI frameworks, creating GUIs is a breeze in REBOL.

Tip

In the following commented code, note that comments in REBOL begin with a semicolon.

Save the following code to a file called yahoo.r and be sure to include your own Yahoo! application ID:

REBOL [
	Title: "Yahoo Search Web Services"
	File: %yahoo.r
	Date: 22-May-2005
	Author: "Premshree Pillai"

	Purpose: {
		Yahoo! Search Web services demo
	}
]
;;
; load the SAX XML Parser
;;
do %xml-parse.r

;;
; set the Application ID
;;
APP_ID: 'insert App ID'

;;
; This function takes two parameters
; 1. query
; 2. Application ID
; It returns the results as an array of maps
; The parsing bit is done by the parse handlers within the parser.
;;
ImageSearch: func [query app_id] [
	return parse-xml+ read rejoin 
[http://api.search.yahoo.com/ImageSearchService/V1/imageSearch '?query='  
query '&appid=' app_id] 
]

;;

; Define a layout
;;
out: layout [
	;;
	; This will appear as the heading in the widget
	;;
	H3 400 {
		Yahoo Search Web Services
		demo using REBOL
	}

;;
; tells REBOL to place following elements across
;;
across
query: field
button "Search" [
    ImageSearchResults: ImageSearch query/text 'rebol-yahoo' APP_ID 
	curr_count: 1 ; current result no. 
	img: load to-url ImageSearchResults/:curr_count/3/2 
	backface/image: img 
	show backface
]

;;
; tells REBOL to place the following elements below
;;
return

;;
; A text box
;;
backface: text 400x300 center

return

;;
; Navigate to previous search result
;;
button "Previous" [
	curr_count: curr_count - 1
	img: load to-url ImageSearchResults/:curr_count/3/2
	backface/image: img
	show backface

]

;;
; Navigate to next search result
;;
button "Next" [
	curr_count: curr_count + 1
	img: load to-url ImageSearchResults/:curr_count/3/2
	backface/image: img
	show backface

]
	pad 0x5
]

;;
; Display the widget!
;;
view out

This minimal widget has no error handling. However, this code should give you a sense of how to use Yahoo!’s Search Web Services with REBOL. Note that the work of parsing the XML response is handled by the handlers in the SAX XML Parser. Modifications to the parse handler functions are commented with my name (Premshree Pillai). If you need to use any of the other Yahoo! Search Web Services, you might need to modify the SAX handlers.

Running the Hack

If you’re on Windows, simply double-click the filename and a widget should pop up. If you’re running Unix, run the hack from the command line, like so:

               rebol yahoo.r

Figure 4-12. A graphical Yahoo! Image search result for "Pink Floyd"

shows the result for a search for Pink Floyd.

Figure 4-12. shows the result for a search for Pink Floyd.

You can click the Next and Previous buttons to browse other Yahoo! Image search results.

Premshree Pillai

Get Yahoo! Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.