Overview
You are asked to implement a tool to search www.flikr.com's library of images in PERL. In order to encourage you to become familiar with UNIX man pages, the details of the lab are given in a similar format. The lower portion of this document contains hints, including information about a powerful library that will help you to access the Web via PERL.
NAME
getimage - get images by keyword from www.flickr.com
SYNOPSIS
getimage [OPTION]... KEYWORD [KEYWORD ...]
DESCRIPTION
Query Flickr's Web site, by keyword, for images. Download the full-size images (not thumbnails) matching all provided keywords, saving them into the current working directory.
Saved files maintain their extension, but are named after the keywords, and enumerated, beginning with 0, in the order downloaded. For example, a query via the keywords "pony" and "hat" might result in the creation of the files "pony_hat_0.jpg" and "pony_hat_1.jpg".
The use of multi-word keywords is not support. For example, "George Washington" may only be expressed as two and'ed keywords, not an ordered tuple.
-n n
Optional argument limiting the number of results to, at most, n images. The default limit, absent this argument, is 10.-t target_directoryOptional argument forcing files to be saved into the target_directory instead of the current working directory.
RETURN VALUE
Returns 0 on success, non-zero on any other condition. For example, returns 1 if the target directory does not exist.
EXAMPLES
getimage pony hatDownload up to 10 images matching both of the keywords "pony" and "hat", save them into the current working directory, and name them using the convention as follows: pony_hat_0.jpg, pony_hat_1.jpg, &c. The limit of at most 10 matching images is the default imposed in the absence of the -n flag, which defines a user-specified limit.
getimage -n3 pony hat
As above, but save no more than three (3) matches.
getimage -n3 -timage_files pony hat
As above, but place results into the "images files" subdirectory.
The LWP Library
We'll be using the get() and getstore() functions from the LWP library to interact with the Web site. The get() function requests a page by URL. The resulting object is returned as a string. The getstore() function has two arguments, the second of which provides the name of the file to which the object should be saved on disk.Implementation HintsThe examples below illustrate these uses of get() and getstore(). In each of the examples below, please notice the "use LWP::Simple;" line. It is an tells PERL that we'll be using the Simple object from within the LWP library. The same form is used to include other libraries in PERL.
Example 1: Returning the page as a string
#!/usr/bin/perl use LWP::Simple; $url = "http://www.google.com"; print get($url);Example 2: Storing the page into a file
#!/usr/bin/perl use LWP::Simple; getstore('http://www.cmu.edu', 'cmu.html');
Hints:
- In your browser of choice go to http://www.flickr.com/ and search for something. On the page with your results, what happened to the URL? What happens when you change the URL directly? What happens to the URL when you go to a second page of results?
- Search the Web for "PERL LWP get" and "PERL LWP getstore". Figure out how to specify the name of the output file for your image files.
- Consider what happens if FlickrTM displays the results on multiple pages: You'll need to traverse the pages. See the first hint for advice on figureing out how to do this.
- Consider the following example HTML image tag:
Which components of this expression are going to change? Which componants will reamain static and can therefore be used as landmarks?<img ... src="http://www.foo.com/bar.jpg"/>
- Are all the images on the page related to the search term? What about the banner and buddy icons? Not so much, right? Look at the html and see how the page is structured to get an idea of how you can parse it (hint: class="photo_container pc_t")
- Perl is a magical language and the Internet is your friend. Look for pre-built modules that do what you're interested before reinventing the wheel. The "use" statements below may be useful in your code:
- use Getopt::Long;
- use LWP::Simple;
We're Here To Help!
As always -- remember, we're here to help!
Autograde Tests