mojodna / placemaker-cli

Command-line interface for Y!'s Placemaker API

This URL has Read+Write access

James E. Flemer (author)
Thu May 21 13:14:15 -0700 2009
commit  34ede757a937a938e99ea68e5e6592572167c53f
tree    1c2e45ff882e516fdb7e3ab41744b9784cdcc70c
parent  ab08920a4db40370522bb6d0c46f540360c4d0dc
placemaker-cli / placemaker
100644 95 lines (85 sloc) 2.442 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env perl
 
use strict;
use warnings;
 
use File::Basename;
use Getopt::Long qw(:config bundling);
use HTTP::Request::Common;
use LWP::UserAgent;
use URI;
 
my %CFG;
$CFG{placemaker}{uri} = new URI('http://wherein.yahooapis.com/v1/document');
 
my %OPT;
$OPT{appid} = undef;
$OPT{inputLanguage} = 'en-US';
$OPT{documentType} = 'text/html';
$OPT{documentTitle} = '';
$OPT{outputType} = 'xml';
$OPT{autoDisambiguate} = 'true';
$OPT{focusWoeid} = '';
 
sub do_pm
{
  my %q = @_;
  my $uri = $CFG{placemaker}{uri};
  my $ua = new LWP::UserAgent;
  #$ua->proxy([qw(http https)] => 'socks://127.0.0.1:1080');
  $ua->env_proxy();
  my $req = POST $uri, [ %q, %OPT ];
  my $res = $ua->request($req);
  if (!$res->is_success)
  {
    print STDERR "PlaceMaker call failed:\n -> POST $uri\n";
    map { print STDERR " -> $_=" . $q{$_} . "\n" } keys %q;
    map { print STDERR " -> $_=" . $OPT{$_} . "\n" } keys %OPT;
    print STDERR " <- HTTP " . $res->code . ' ' . $res->message . "\n";
    print $res->content;
    return undef;
  }
  #my $rss = XML::RAI->parse_string($res->content);
  $res->content;
}
 
sub usage
{
  my $name = basename($0);
  my $spad = (' ') x length($name);
  print <<EOF;
usage: $name --appid=<YAHOO_APPID> [ --documentType=mime/type ]
$spad [ --inputLanguage=lang-code] [ --autoDisambiguate=true|false ]
$spad [ --outputType={xml|rss} ] [ --focusWoeid=1234 ] url(s) ...
or:
$name --appid=<YAHOO_APPID> [ --documentType=mime/type ]
$spad [ --inputLanguage=lang-code] [ --autoDisambiguate=true|false ]
$spad [ --outputType={xml|rss} ] [ --focusWoeid=1234 ]
$spad [ --documentTitle="string" ] -
 
EOF
  exit 64;
}
 
{
  my $show_usage = 0;
  GetOptions(
      'appid|a=s' => \$OPT{appid},
      'autoDisambiguate|auto|d=s' => \$OPT{autoDisambiguate},
      'inputLanguage|lang|l=s' => \$OPT{inputLanguage},
      'documentType|type|t=s' => \$OPT{documentType},
      'documentTitle|title|i=s' => \$OPT{documentTitle},
      'outputType|format|f=s' => \$OPT{outputType},
      'focusWoeid|woeid|w=s' => \$OPT{focusWoeid},
      'h|help' => \$show_usage
    ) or $show_usage = 1;
  $show_usage = 1 unless defined $OPT{appid};
  usage if $show_usage || @ARGV == 0;
}
 
foreach my $u (@ARGV)
{
  my %q;
  if ($u eq '-')
  {
    local $/ = undef;
    %q = (documentContent => <> );
  }
  else
  {
    %q = (documentURL => $u);
  }
  my $c = do_pm(%q);
  print $c if defined $c;
}