#!/usr/bin/perl ################################################################## # $Revision: 1.1 $ # $Date: 2008/10/01 15:33:47 $ ################################################################## ############################################### my %mydef= ( api_key => "yours", api_secret => "yours", screen_name => "yours", user_id => "yours like 12345678\@X12", rest_url => "http://api.flickr.com/services/rest", ); ################################################ require HTTP::Request; require LWP::UserAgent; require HTTP::Headers; require HTTP::Response; use Getopt::Std; use warnings; use strict; my $usage = "$0 -m method [-p param1,param2,param3,...] [-o outfile] [-h]\n". " -m: method without 'flickr.' prefix, ex) test.echo\n". " -p: additional param in form of name=value, ex) count=10,photo_id=12345\n". " -h: this help\n". " -o: outfile, if not specified, the result will show up on STDOUT\n". " => api_key, api_secret, and user_id will be passed to flickr by default\n"; my %options=(); getopts("m:o:p:h",\%options); my $method = $options{m}; my $outfile = $options{o}; my $param = $options{p}; die $usage if defined($options{h}); defined($method) or die $usage; my %rest_param=( api_key => $mydef{api_key}, api_secret => $mydef{api_secret}, user_id => $mydef{user_id}, ); split_param($param, \%rest_param) if defined($param) && length($param) > 0; my $link = get_method_link($method, \%rest_param); #DBUG #print $link; my $outstring = run_method($link); if (defined($outfile)) { open OUTFILE, "> $outfile" or die $!; print OUTFILE $outstring; close OUTFILE; } else { print $outstring; } sub split_param { my ($param_string, $ref_hash) = @_; my @lines = split ",", $param_string; #DBUG #print "##$param_string##\n"; foreach my $x (@lines) { #DBUG #print "optionline: $x\n"; my ($key, $value) = split "=", $x; $ref_hash->{$key} = $value; } } sub run_method { my ($req_string) = @_; return "" unless ( defined($req_string) && length($req_string) > 0 ); my $request = HTTP::Request->new(GET => "$req_string"); my $ua = LWP::UserAgent->new; my $response = $ua->request($request); return $response->content(); } sub get_method_link { my ($method, $ref_hash) = @_; return "" unless defined ($method); my $req = "$mydef{rest_url}/?method=flickr." . $method; my @param_array = (); #my $hash_size = keys %loc_hash; foreach my $key (keys %$ref_hash) { push @param_array, "$key=$ref_hash->{$key}"; } my $URL = $req; if ( scalar(@param_array) > 0 ) { $URL .= "&" . join "&", @param_array; } #print "$URL"; return $URL; } ####################################################