#!/usr/bin/perl # # Copyright 2008, Google Inc. # # This code is licensed under the terms of Perl itself. # use strict; use Getopt::Long; use URI; use LWP::UserAgent; use HTTP::Request; use HTTP::Request::Common; use HTTP::Response; sub usage { my $reason = shift; my $text = "Usage: sgapi_test_parse.pl [OPTS] Options: --urlformat={base,sgn,raw} Defaults to base URLs. --fakehost= Rewrite the host/port part of the URL before sending to the Social Graph API. "; if ($reason) { $text = "Error: $reason\n\n$text"; } die $text; } my $sgapi_url = "http://socialgraph.apis.google.com/testparse"; my ($url, $fakehost); my $url_format = "base"; GetOptions("fakehost=s" => \$fakehost, "urlformat=s" => \$url_format, "apiurl=s" => \$sgapi_url) or usage("Unknown options."); usage("Bogus URL format.") unless $url_format =~ /^(base|sgn|raw)$/; $url = shift or usage(); my $ua = LWP::UserAgent->new; my $res = $ua->get($url); unless ($res->is_success) { die "Failed to fetch: $url\n" . $res->status_line . "\n"; } my $content_type = $res->header("Content-Type"); my $body = $res->content; my $api_res = $ua->request(POST $sgapi_url, [body => $body, contentType => $content_type, url => url_with_fake_host($url, $fakehost), urlFormat => $url_format, ]); unless ($api_res->is_success) { die "Error from Social Graph API:\n " . $api_res->status_line . "\n"; } print $api_res->content; sub url_with_fake_host { my ($url, $host) = @_; return $url unless $host; my $uri = URI->new($url); $uri->host($host); return $uri->as_string; }