File Coverage

blib/lib/WWW/SearchWikipedia.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package WWW::SearchWikipedia;
2              
3 1     1   30940 use Moose;
  0            
  0            
4             use MooseX::Params::Validate;
5             use Moose::Util::TypeConstraints;
6             use namespace::clean;
7              
8             use Carp;
9             use Data::Dumper;
10              
11             use Readonly;
12             use HTTP::Request;
13             use LWP::UserAgent;
14              
15             =head1 NAME
16              
17             WWW::SearchWikipedia - Interface to Search Wikipedia API.
18              
19             =head1 VERSION
20              
21             Version 0.02
22              
23             =cut
24              
25             our $VERSION = '0.02';
26             Readonly my $BASE_URL => 'http://api.wikilocation.org/';
27             Readonly my $LOCALE =>
28             {
29             'ar' => 1,
30             'bg' => 1,
31             'ca' => 1,
32             'cs' => 1,
33             'da' => 1,
34             'de' => 1,
35             'en' => 1,
36             'eo' => 1,
37             'es' => 1,
38             'fa' => 1,
39             'fi' => 1,
40             'fr' => 1,
41             'he' => 1,
42             'hu' => 1,
43             'id' => 1,
44             'it' => 1,
45             'ja' => 1,
46             'ko' => 1,
47             'lt' => 1,
48             'ms' => 1,
49             'nl' => 1,
50             'no' => 1,
51             'pl' => 1,
52             'pt' => 1,
53             'ro' => 1,
54             'ru' => 1,
55             'sk' => 1,
56             'sl' => 1,
57             'sr' => 1,
58             'sv' => 1,
59             'tr' => 1,
60             'uk' => 1,
61             'vi' => 1,
62             'vo' => 1,
63             'war' => 1,
64             'zh' => 1,
65             };
66              
67             =head1 DESCRIPTION
68              
69             A very lightweight wrapper for the Search Wikipedia REST API provided by wikilocation.org.
70              
71             =cut
72              
73             type 'DecimalDegreeFormat' => where { defined($_) && (/^\-?\d+\.?\d+$/) };
74             type 'Limit' => where { defined($_) && (/^\d*$/) && ($_ <= 50) };
75             type 'Radius' => where { defined($_) && (/^\d+\.?\d+$/) && ($_ <= 20000) };
76             type 'Locale' => where { defined($_) && exists($LOCALE->{lc($_)}) };
77             type 'Format' => where { defined($_) && (/\bxml\b|\bjson\b/i) };
78             type 'Boolean' => where { defined($_) && (/\btrue\b|\bfalse\b/i) };
79              
80             has 'locale' => (is => 'ro', isa => 'Locale', default => 'en');
81             has 'format' => (is => 'ro', isa => 'Format', default => 'json');
82             has 'debug' => (is => 'ro', isa => 'Boolean', default => 'false');
83             has 'browser' => (is => 'ro', isa => 'LWP::UserAgent', default => sub { return LWP::UserAgent->new(agent => 'Mozilla/5.0'); });
84              
85             =head1 CONSTRUCTOR
86              
87             +--------+----------+--------------------------------------------------------------------+
88             | Key | Required | Description |
89             +--------+----------+--------------------------------------------------------------------+
90             | locale | No | Use this parameter in order to search for article from a different |
91             | | | wikipedia.org locale. Default is 'en'. |
92             | format | No | The desired output format, either 'json' / 'xml'. Default is json. |
93             | debug | No | If set to true then it will suppress the application/x-json header |
94             | | | on any returned json stream. Default is false. |
95             +--------+----------+--------------------------------------------------------------------+
96              
97             use strict; use warnings;
98             use WWW::SearchWikipedia;
99            
100             my ($search);
101            
102             $search = WWW::SearchWikipedia->new();
103             # or
104             $search = WWW::SearchWikipedia->new(locale => 'en', format => 'xml', debug => 'true');
105             # or
106             $search = WWW::SearchWikipedia->new(locale => 'en');
107             # or
108             $search = WWW::SearchWikipedia->new({locale => 'en', format => 'xml', debug => 'true'});
109             # or
110             $search = WWW::SearchWikipedia->new({locale => 'en'});
111              
112             =head1 LOCALES
113              
114             Supported locales listed here with articles count:
115              
116             +---------------------+
117             | Locale | Articles |
118             +--------+------------+
119             | ar | 6,478 |
120             | bg | 18,281 |
121             | ca | 130,711 |
122             | cs | 7 |
123             | da | 54,053 |
124             | de | 354,629 |
125             | en | 1,116,511 |
126             | eo | 46,399 |
127             | es | 197,825 |
128             | fa | 18,845 |
129             | fi | 16,865 |
130             | fr | 299,472 |
131             | he | 1 |
132             | hu | 26,403 |
133             | id | 17,003 |
134             | it | 152,168 |
135             | ja | 64,246 |
136             | ko | 8,691 |
137             | lt | 33,706 |
138             | ms | 24,877 |
139             | nl | 456,858 |
140             | no | 44,028 |
141             | pl | 173,171 |
142             | pt | 142,149 |
143             | ro | 23,603 |
144             | ru | 196,198 |
145             | sk | 5,664 |
146             | sl | 9,825 |
147             | sr | 53,245 |
148             | sv | 33,738 |
149             | tr | 9,867 |
150             | uk | 103,305 |
151             | vi | 72,188 |
152             | vo | 94,470 |
153             | war | 99 |
154             | zh | 20,412 |
155             +--------+------------+
156              
157             =head1 METHODS
158              
159             =head2 article()
160              
161             Find nearby Wikipedia articles based on a specific latitude and longitude.
162              
163             +--------+----------+--------------------------------------------------------------------+
164             | Key | Required | Description |
165             +--------+----------+--------------------------------------------------------------------+
166             | lat | Yes | Latitude in decimal degree format. |
167             | lng | Yes | Longitude in decimal degree format. |
168             | radius | No | The radius (in metres) to search within. There is a maximum radius |
169             | | | of 20km and it will default to 250m if no radius is supplied. |
170             | limit | No | The number of results you want to return. There is a maximum limit |
171             | | | of 50 results although you can paginate. This will default to 50 |
172             | | | if no lower figure is sent. |
173             | offset | No | The offset from the first result returned. There is no maximum for |
174             | | | this parameter. The default is 0. |
175             | type | No | The type of article you are interested in.There are various options|
176             | | | for this so best to look at some results and then filter from there|
177             | | | (you could filter by a type of "river" or "landmark"). The default |
178             | | | is no filter. |
179             +--------+----------+--------------------------------------------------------------------+
180              
181             use strict; use warnings;
182             use WWW::SearchWikipedia;
183            
184             my $search = WWW::SearchWikipedia->new();
185             print $search->article({lat => 51.500688, lng => -0.124411, limit => 1}) . "\n";
186              
187             =cut
188              
189             sub article
190             {
191             my $self = shift;
192             my %param = validated_hash(\@_,
193             'lat' => { isa => 'DecimalDegreeFormat', required => 1 },
194             'lng' => { isa => 'DecimalDegreeFormat', required => 1 },
195             'radius' => { isa => 'Radius', default => 250 },
196             'limit' => { isa => 'Limit', default => 50 },
197             'offset' => { isa => 'Num', default => 0 },
198             'type' => { isa => 'Str', default => undef },
199             MX_PARAMS_VALIDATE_NO_CACHE => 1);
200            
201             my $url = sprintf("%s/articles?lat=%s&lng=%s&radius=%s&limit=%s&offset=%s&format=%s&locale=%s&debug=%s",
202             $BASE_URL, $param{lat}, $param{lng}, $param{radius}, $param{limit}, $param{offset},
203             $self->{format}, $self->{locale}, $self->{debug});
204             return $self->_process($url);
205             }
206              
207             =head2 gowalla()
208              
209             Find Wikipedia articles based on a Gowalla spot ID. The spot ID can be found by going to the
210             spot on the Gowalla website ( e.g. http://gowalla.com/spots/22087 ) and then taking the digits
211             from the end of the URL (in this case "22087").
212             This method will return not only an "articles" array (which lists the Wikipedia entries along
213             with their distance from the Gowalla spot) but also a "spot" array giving some basic details
214             from Gowalla such as lat/lng, name, and the radius.
215              
216             +-------+----------+---------------------------------------------------------------------+
217             | Key | Required | Description |
218             +-------+----------+---------------------------------------------------------------------+
219             | id | Yes | The Gowalla spot ID. |
220             | exact | No | Defaults to false. If set to true it will only return articles with |
221             | | | the exact same name as the Gowalla spot. |
222             +-------+----------+---------------------------------------------------------------------+
223              
224             use strict; use warnings;
225             use WWW::SearchWikipedia;
226            
227             my $search = WWW::SearchWikipedia->new();
228             print $search->gowalla(id => 22087) . "\n";
229              
230             =cut
231              
232             sub gowalla
233             {
234             my $self = shift;
235             my %param = validated_hash(\@_,
236             'id' => { isa => 'Num', required => 1 },
237             'exact' => { isa => 'Boolean', default => 'false' },
238             MX_PARAMS_VALIDATE_NO_CACHE => 1);
239            
240             my $url = sprintf("%s/gowalla/%d?exact=%s&format=%s&locale=%s&debug=%s",
241             $BASE_URL, $param{id}, $param{exact},
242             $self->{format}, $self->{locale}, $self->{debug});
243             return $self->_process($url);
244             }
245              
246             =head2 foursquare()
247              
248             Find Wikipedia articles based on a Foursquare venue ID. The venue ID can be found by going to
249             the spot on the Foursquare website ( e.g. http://foursquare.com/venue/141395 ) and then taking
250             the digits from the end of the URL (in this case "141395").
251             This method will return not only an "articles" array ( which lists the Wikipedia entries along
252             with their distance from the Foursquare venue ) but also a "venue" array giving some basic
253             details from Foursquare such as lat/lng and name.The radius will be either the default of 250m
254             or the radius you defined as a parameter.
255              
256             +--------+----------+--------------------------------------------------------------------+
257             | Key | Required | Description |
258             +--------+----------+--------------------------------------------------------------------+
259             | id | Yes | The Foursquare venue ID. |
260             | radius | No | The radius (in metres) to search within. There is a maximum radius |
261             | | | of 5km and it will default to 250m if no radius is supplied. |
262             | exact | No | Defaults to false. If set to true it will only return articles with|
263             | | | the exact same name as the Foursquare venue. |
264             +--------+----------+--------------------------------------------------------------------+
265              
266             use strict; use warnings;
267             use WWW::SearchWikipedia;
268            
269             my $search = WWW::SearchWikipedia->new();
270             print $search->foursquare(id => 141395) . "\n";
271              
272             =cut
273              
274             sub foursquare
275             {
276             my $self = shift;
277             my %param = validated_hash(\@_,
278             'id' => { isa => 'Num', required => 1 },
279             'radius' => { isa => 'Radius', default => 250 },
280             'exact' => { isa => 'Boolean', default => 'false' },
281             MX_PARAMS_VALIDATE_NO_CACHE => 1);
282            
283             my $url = sprintf("%s/foursquare/%d?exact=%s&radius=%s&format=%s&locale=%s&debug=%s",
284             $BASE_URL, $param{id}, $param{exact}, $param{radius},
285             $self->{format}, $self->{locale}, $self->{debug});
286             return $self->_process($url);
287             }
288              
289             =head2 woeid()
290              
291             Find Wikipedia articles based on a Yahoo! WOEID.A WOEID is a unique, non-repetitive identifier
292             for any geolocational object from a famous landmark to a continent.
293             This method will return not only an "articles" array ( which lists the Wikipedia entries along
294             with their distance from the "centroid" ) but also a "woeid" array giving some basic details
295             from Yahoo! such as lat/lng & name.The radius will be either the default of 250m or the radius
296             you defined as a parameter.
297              
298             +--------+----------+--------------------------------------------------------------------+
299             | Key | Required | Description |
300             +--------+----------+--------------------------------------------------------------------+
301             | id | Yes | The Yahoo! WOEID. |
302             | radius | No | The radius (in metres) to search within. There is a maximum radius |
303             | | | of 5km and it will default to 250m if no radius is supplied. |
304             | exact | No | Defaults to false. If set to true it will only return articles with|
305             | | | the exact same name as the WOEID name. |
306             +--------+----------+--------------------------------------------------------------------+
307              
308             use strict; use warnings;
309             use WWW::SearchWikipedia;
310            
311             my $search = WWW::SearchWikipedia->new();
312             print $search->woeid(id => 22474116) . "\n";
313              
314             =cut
315              
316             sub woeid
317             {
318             my $self = shift;
319             my %param = validated_hash(\@_,
320             'id' => { isa => 'Num', required => 1 },
321             'radius' => { isa => 'Radius', default => 250 },
322             'exact' => { isa => 'Boolean', default => 'false' },
323             MX_PARAMS_VALIDATE_NO_CACHE => 1);
324              
325             my $url = sprintf("%s/woeid/%d?exact=%s&radius=%s&format=%s&locale=%s&debug=%s",
326             $BASE_URL, $param{id}, $param{exact}, $param{radius},
327             $self->{format}, $self->{locale}, $self->{debug});
328             return $self->_process($url);
329             }
330              
331             sub _process
332             {
333             my $self = shift;
334             my $url = shift;
335            
336             my ($browser, $request, $response, $content);
337             $browser = $self->browser;
338             $browser->env_proxy;
339             $request = HTTP::Request->new(GET => $url);
340             $response = $browser->request($request);
341             croak("ERROR: Couldn't fetch data [$url]:[".$response->status_line."]\n")
342             unless $response->is_success;
343             $content = $response->content;
344             croak("ERROR: No data found.\n") unless defined $content;
345             return $content;
346             }
347              
348             =head1 AUTHOR
349              
350             Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
351              
352             =head1 BUGS
353              
354             Please report any bugs or feature requests to C<bug-www-searchwikipedia at rt.cpan.org>, or
355             through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-SearchWikipedia>.
356             I will be notified and then you'll automatically be notified of progress on your bug as I make
357             changes.
358              
359             =head1 SUPPORT
360              
361             You can find documentation for this module with the perldoc command.
362              
363             perldoc WWW::SearchWikipedia
364              
365             You can also look for information at:
366              
367             =over 4
368              
369             =item * RT: CPAN's request tracker
370              
371             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-SearchWikipedia>
372              
373             =item * AnnoCPAN: Annotated CPAN documentation
374              
375             L<http://annocpan.org/dist/WWW-SearchWikipedia>
376              
377             =item * CPAN Ratings
378              
379             L<http://cpanratings.perl.org/d/WWW-SearchWikipedia>
380              
381             =item * Search CPAN
382              
383             L<http://search.cpan.org/dist/WWW-SearchWikipedia/>
384              
385             =back
386              
387             =head1 ACKNOWLEDGEMENT
388              
389             Ben Dodson (author of WikiLocation REST API).
390              
391             =head1 LICENSE AND COPYRIGHT
392              
393             Copyright 2011 Mohammad S Anwar.
394              
395             This program is free software; you can redistribute it and/or modify it under the terms of
396             either: the GNU General Public License as published by the Free Software Foundation; or the
397             Artistic License.
398              
399             See http://dev.perl.org/licenses/ for more information.
400              
401             =head1 DISCLAIMER
402              
403             This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
404             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
405              
406             =cut
407              
408             __PACKAGE__->meta->make_immutable;
409             no Moose; # Keywords are removed from the WWW::SearchWikipedia package
410             no Moose::Util::TypeConstraints;
411              
412             1; # End of WWW::SearchWikipedia