File Coverage

blib/lib/WWW/Google/KnowledgeGraphSearch.pm
Criterion Covered Total %
statement 29 46 63.0
branch 0 8 0.0
condition n/a
subroutine 10 11 90.9
pod 1 1 100.0
total 40 66 60.6


line stmt bran cond sub pod time code
1             package WWW::Google::KnowledgeGraphSearch;
2              
3             $WWW::Google::KnowledgeGraphSearch::VERSION = '0.05';
4             $WWW::Google::KnowledgeGraphSearch::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             WWW::Google::KnowledgeGraphSearch - Interface to Google Knowledge Graph Search API.
9              
10             =head1 VERSION
11              
12             Version 0.05
13              
14             =cut
15              
16 3     3   54133 use 5.006;
  3         19  
17 3     3   1344 use URI;
  3         18443  
  3         87  
18 3     3   1521 use JSON;
  3         23426  
  3         12  
19 3     3   2129 use Data::Dumper;
  3         15339  
  3         166  
20              
21 3     3   1148 use WWW::Google::UserAgent;
  3         271471  
  3         96  
22 3     3   1401 use WWW::Google::UserAgent::DataTypes qw(:all);
  3         374755  
  3         33  
23 3     3   44216 use WWW::Google::KnowledgeGraphSearch::Result;
  3         9  
  3         77  
24              
25 3     3   16 use Moo;
  3         5  
  3         12  
26 3     3   1148 use namespace::autoclean;
  3         6  
  3         12  
27 3     3   182 use Types::Standard qw(Bool Str Int);
  3         19  
  3         24  
28             extends 'WWW::Google::UserAgent';
29              
30             our $BASE_URL = "https://kgsearch.googleapis.com/v1/entities:search";
31              
32             our $ENTITY_TYPES = {
33             'Book' => 1,
34             'BookSeries' => 1,
35             'EducationalOrganization' => 1,
36             'Event' => 1,
37             'GovernmentOrganization' => 1,
38             'LocalBusiness' => 1,
39             'Movie' => 1,
40             'MovieSeries' => 1,
41             'MusicAlbum' => 1,
42             'MusicGroup' => 1,
43             'MusicRecording' => 1,
44             'Organization' => 1,
45             'Periodical' => 1,
46             'Person' => 1,
47             'Place' => 1,
48             'SportsTeam' => 1,
49             'TVEpisode' => 1,
50             'TVSeries' => 1,
51             'VideoGame' => 1,
52             'VideoGameSeries' => 1,
53             'WebSite' => 1,
54             };
55              
56             our $LANGUAGES = {
57             'ab' => 1, 'aa' => 1, 'af' => 1, 'ak' => 1, 'sq' => 1, 'am' => 1, 'ar' => 1, 'an' => 1,
58             'hy' => 1, 'as' => 1, 'av' => 1, 'ae' => 1, 'ay' => 1, 'az' => 1, 'bm' => 1, 'br' => 1,
59             'eu' => 1, 'be' => 1, 'bn' => 1, 'bh' => 1, 'bi' => 1, 'bs' => 1, 'br' => 1, 'bg' => 1,
60             'my' => 1, 'ca' => 1, 'ch' => 1, 'ce' => 1, 'ny' => 1, 'zh' => 1, 'cv' => 1, 'kw' => 1,
61             'co' => 1, 'cr' => 1, 'hr' => 1, 'cs' => 1, 'da' => 1, 'dv' => 1, 'nl' => 1, 'dz' => 1,
62             'en' => 1, 'eo' => 1, 'et' => 1, 'ee' => 1, 'fo' => 1, 'fj' => 1, 'fi' => 1, 'fr' => 1,
63             'ff' => 1, 'gl' => 1, 'ka' => 1, 'de' => 1, 'el' => 1, 'gn' => 1, 'gu' => 1, 'ht' => 1,
64             'ha' => 1, 'he' => 1, 'hz' => 1, 'hi' => 1, 'ho' => 1, 'hu' => 1, 'ia' => 1, 'id' => 1,
65             'ie' => 1, 'ga' => 1, 'ig' => 1, 'ik' => 1, 'io' => 1, 'is' => 1, 'it' => 1, 'iu' => 1,
66             'ja' => 1, 'jv' => 1, 'kl' => 1, 'kn' => 1, 'kr' => 1, 'ks' => 1, 'kk' => 1, 'km' => 1,
67             'ki' => 1, 'rw' => 1, 'ky' => 1, 'kv' => 1, 'kg' => 1, 'ko' => 1, 'ku' => 1, 'kj' => 1,
68             'la' => 1, 'lb' => 1, 'lg' => 1, 'li' => 1, 'ln' => 1, 'lo' => 1, 'lt' => 1, 'lu' => 1,
69             'lv' => 1, 'gv' => 1, 'mk' => 1, 'mg' => 1, 'ms' => 1, 'ml' => 1, 'mt' => 1, 'mi' => 1,
70             'mr' => 1, 'mh' => 1, 'mn' => 1, 'na' => 1, 'nv' => 1, 'nd' => 1, 'ne' => 1, 'ng' => 1,
71             'nb' => 1, 'nn' => 1, 'no' => 1, 'ii' => 1, 'nr' => 1, 'oc' => 1, 'oj' => 1, 'cu' => 1,
72             'om' => 1, 'or' => 1, 'os' => 1, 'pa' => 1, 'pi' => 1, 'fa' => 1, 'pl' => 1, 'ps' => 1,
73             'pt' => 1, 'qu' => 1, 'rm' => 1, 'rn' => 1, 'ro' => 1, 'ru' => 1, 'sa' => 1, 'sc' => 1,
74             'sd' => 1, 'se' => 1, 'sm' => 1, 'sg' => 1, 'sr' => 1, 'gd' => 1, 'sn' => 1, 'si' => 1,
75             'sk' => 1, 'sl' => 1, 'so' => 1, 'st' => 1, 'es' => 1, 'su' => 1, 'sw' => 1, 'ss' => 1,
76             'sv' => 1, 'ta' => 1, 'te' => 1, 'tg' => 1, 'th' => 1, 't1' => 1, 'bo' => 1, 'tk' => 1,
77             'tl' => 1, 'tn' => 1, 'to' => 1, 'tr' => 1, 'ts' => 1, 'tt' => 1, 'tw' => 1, 'ty' => 1,
78             'ug' => 1, 'uk' => 1, 'ur' => 1, 'uz' => 1, 've' => 1, 'vi' => 1, 'vo' => 1, 'wa' => 1,
79             'cy' => 1, 'wo' => 1, 'fy' => 1, 'xh' => 1, 'yi' => 1, 'yo' => 1, 'za' => 1, 'zu' => 1,
80             };
81              
82             has 'languages' => (is => 'ro', isa => Str, default => sub { 'en' });
83             has 'limit' => (is => 'ro', isa => Int, default => sub { 1 });
84              
85             =head1 DESCRIPTION
86              
87             The Knowledge Graph Search API lets you find entities in the Google Knowledge
88             Graph. The Google Knowledge Graph Search API requires the use of an API key which
89             you can get from the Google APIs console. The API provides 100,000 search queries
90             per day for free. If you need more, you may sign up for billing in the console.
91              
92             The official Google API document can be found L.
93              
94             Important:The version v1 of the Google Knowledge Graph Search API is in Labs and
95             its features might change unexpectedly until it graduates.
96              
97             =head1 SYNOPSIS
98              
99             use strict; use warnings;
100             use WWW::Google::KnowledgeGraphSearch;
101              
102             my $api_key = 'Your_API_Key';
103             my $engine = WWW::Google::KnowledgeGraphSearch->new(api_key => $api_key);
104             my $result = $engine->search('Taylor Swift');
105              
106             print $result->[0]->id, "\n";
107             print $result->[0]->name, "\n";
108             print $result->[0]->description, "\n";
109             print $result->[0]->descriptionBody, "\n";
110             print $result->[0]->resultScore, "\n";
111              
112             See L for further details of the search result.
113              
114             =head1 CONSTRUCTOR
115              
116             The constructor expects application API Key C mandatory, all others are
117             optional.
118              
119             +-----------+---------------------------------------------------------------+
120             | Key | Description |
121             +-----------+---------------------------------------------------------------+
122             | api_key | API Key for Google Knowledge Graph Search API. |
123             | | |
124             | languages | A comma separated list of language codes (defined in ISO 639).|
125             | | Default is 'en'. |
126             | | |
127             | limit | Limits the number of entities to be returned. Default is 1. |
128             +-----------+---------------------------------------------------------------+
129              
130             =head1 LANGUAGE ISO 639
131              
132             +-------------------+-------------------------------------------------------+
133             | Language | Value |
134             +-------------------+-------------------------------------------------------+
135             | Abkhazian | ab |
136             | Afar | aa |
137             | Afrikaan | af |
138             | Akan | ak |
139             | Albanian | sq |
140             | Amharic | am |
141             | Arabic | ar |
142             | Aragonese | an |
143             | Armenian | hy |
144             | Assamese | as |
145             | Avaric | av |
146             | Avestan | ae |
147             | Aymara | ay |
148             | Azerbaijani | az |
149             | Bambara | bm |
150             | Bashkir | br |
151             | Basque | eu |
152             | Belarusian | be |
153             | Bengali | bn |
154             | Bihari languages | bh |
155             | Bislama | bi |
156             | Bosnian | bs |
157             | Breton | br |
158             | Bulgarian | bg |
159             | Burmese | my |
160             | Catalan | ca |
161             | Chamorro | ch |
162             | Chechen | ce |
163             | Chichewa | ny |
164             | Chinese | zh |
165             | Chuvash | cv |
166             | Cornish | kw |
167             | Corsican | co |
168             | Cree | cr |
169             | Croatian | hr |
170             | Czech | cs |
171             | Danish | da |
172             | Divehi | dv |
173             | Dutch | nl |
174             | Dzongkha | dz |
175             | English | en |
176             | Esperanto | eo |
177             | Estonian | et |
178             | Ewe | ee |
179             | Faroese | fo |
180             | Fijian | fj |
181             | Finnish | fi |
182             | French | fr |
183             | Fulah | ff |
184             | Galician | gl |
185             | Georgian | ka |
186             | German | de |
187             | Greek | el |
188             | Guarani | gn |
189             | Gujarati | gu |
190             | Haitian | ht |
191             | Hausa | ha |
192             | Hebrew | he |
193             | Herero | hz |
194             | Hindi | hi |
195             | Hiri Motu | ho |
196             | Hungarian | hu |
197             | Interlingua | ia |
198             | Indonesian | id |
199             | Interlingue | ie |
200             | Irish | ga |
201             | Igbo | ig |
202             | Inupiaq | ik |
203             | Ido | io |
204             | Icelandic | is |
205             | Italian | it |
206             | Inuktitut | iu |
207             | Japanese | ja |
208             | Javanese | jv |
209             | Kalaallisut | kl |
210             | Kannada | kn |
211             | Kanuri | kr |
212             | Kashmiri | ks |
213             | Kazakh | kk |
214             | Central Khmer | km |
215             | Kikuyu | ki |
216             | Kinyarwanda | rw |
217             | Kirghiz | ky |
218             | Komi | kv |
219             | Kongo | kg |
220             | Korean | ko |
221             | Kurdish | ku |
222             | Kuanyama | kj |
223             | Latin | la |
224             | Luxembourgish | lb |
225             | Ganda | lg |
226             | Limburgan | li |
227             | Lingala | ln |
228             | Lao | lo |
229             | Lithuanian | lt |
230             | Luba-Katanga | lu |
231             | Latvian | lv |
232             | Manx | gv |
233             | Macedonian | mk |
234             | Malagasy | mg |
235             | Malay | ms |
236             | Malayalam | ml |
237             | Maltese | mt |
238             | Maori | mi |
239             | Marathi | mr |
240             | Marshallese | mh |
241             | Mongolian | mn |
242             | Nauru | na |
243             | Navajo | nv |
244             | North Ndebele | nd |
245             | Nepali | ne |
246             | Ndonga | ng |
247             | Norwegian Bokmal | nb |
248             | Norwegian Nynorsk | nn |
249             | Norwegian | no |
250             | Sichuan Yi | ii |
251             | South Ndebele | nr |
252             | Occitan | oc |
253             | Ojibwa | oj |
254             | Church Slavic | cu |
255             | Oromo | om |
256             | Oriya | or |
257             | Ossetian | os |
258             | Panjabi | pa |
259             | Pali | pi |
260             | Persian | fa |
261             | Polish | pl |
262             | Pashto | ps |
263             | Portuguese | pt |
264             | Quechua | qu |
265             | Romansh | rm |
266             | Rundi | rn |
267             | Romanian | ro |
268             | Russian | ru |
269             | Sanskrit | sa |
270             | Sardinian | sc |
271             | Sindhi | sd |
272             | Northern Sami | se |
273             | Samoan | sm |
274             | Sango | sg |
275             | Serbian | sr |
276             | Gaelic | gd |
277             | Shona | sn |
278             | Sinhala | si |
279             | Slovak | sk |
280             | Slovenian | sl |
281             | Somali | so |
282             | Southern Sotho | st |
283             | Spanish | es |
284             | Sundanese | su |
285             | Swahili | sw |
286             | Swati | ss |
287             | Swedish | sv |
288             | Tamil | ta |
289             | Telugu | te |
290             | Tajik | tg |
291             | Thai | th |
292             | Tigrinya | t1 |
293             | Tibetan | bo |
294             | Turkmen | tk |
295             | Tagalog | tl |
296             | Tswana | tn |
297             | Tonga | to |
298             | Turkish | tr |
299             | Tsonga | ts |
300             | Tatar | tt |
301             | Twi | tw |
302             | Tahitian | ty |
303             | Uighur | ug |
304             | Ukrainian | uk |
305             | Urdu | ur |
306             | Uzbek | uz |
307             | Venda | ve |
308             | Vietnamese | vi |
309             | Volapuk | vo |
310             | Walloon | wa |
311             | Welsh | cy |
312             | Wolof | wo |
313             | Western Frisian | fy |
314             | Xhosa | xh |
315             | Yiddish | yi |
316             | Yoruba | yo |
317             | Zhuang | za |
318             | Zulu | zu |
319             +-------------------+-------------------------------------------------------+
320              
321             =head1 KNOWLEDGE GRAPH ENTITY TYPES
322              
323             The API search the following knowledge graph entity types:
324              
325             +---------------------------------------------------------------------------+
326             | Book |
327             | BookSeries |
328             | EducationalOrganization |
329             | Event |
330             | GovernmentOrganization |
331             | LocalBusiness |
332             | Movie |
333             | MovieSeries |
334             | MusicAlbum |
335             | MusicGroup |
336             | MusicRecording |
337             | Organization |
338             | Periodical |
339             | Person |
340             | Place |
341             | SportsTeam |
342             | TVEpisode |
343             | TVSeries |
344             | VideoGame |
345             | VideoGameSeries |
346             | WebSite |
347             +---------------------------------------------------------------------------+
348              
349             =head1 METHODS
350              
351             =head2 search($query_string)
352              
353             Get search result L.
354              
355             use strict; use warnings;
356             use WWW::Google::KnowledgeGraphSearch;
357              
358             my $api_key = 'Your_API_Key';
359             my $engine = WWW::Google::KnowledeGraphSearch->new(api_key => $api_key);
360             my $result = $engine->search('Taylor Swift');
361              
362             print $result->[0]->id, "\n";
363             print $result->[0]->name, "\n";
364             print $result->[0]->description, "\n";
365             print $result->[0]->descriptionBody, "\n";
366             print $result->[0]->resultScore, "\n";
367              
368             =cut
369              
370             sub search {
371 0     0 1   my ($self, $query, $ids, $types) = @_;
372 0 0         die "ERROR: Missing query string." unless defined $query;
373              
374 0           my $url = URI->new($BASE_URL);
375 0           my $params = {
376             key => $self->api_key,
377             limit => $self->limit,
378             languages => $self->languages,
379             query => $query,
380             };
381              
382 0 0         if (defined $ids) {
383 0           $params->{ids} = $ids;
384             }
385              
386 0 0         if (defined $types) {
387             die "ERROR: Invalid entity type $types."
388 0 0         unless (exists $ENTITY_TYPES->{$types});
389 0           $params->{types} = $types;
390             }
391              
392 0           $url->query_form($params);
393              
394 0           my $response = $self->get($url);
395 0           my $contents = from_json($response->{content});
396              
397 0           my $results = [];
398 0           foreach my $result (@{$contents->{itemListElement}}) {
  0            
399 0           push @$results, WWW::Google::KnowledgeGraphSearch::Result->new(raw => $result);
400             }
401              
402 0           return $results;
403             }
404              
405             =head1 AUTHOR
406              
407             Mohammad S Anwar, C<< >>
408              
409             =head1 REPOSITORY
410              
411             L
412              
413             =head1 BUGS
414              
415             Please report any bugs or feature requests to C
416             rt.cpan.org>, or through the web interface at L.
417             I will be notified, and then you'll automatically be notified of progress on your
418             bug as I make changes.
419              
420             =head1 SUPPORT
421              
422             You can find documentation for this module with the perldoc command.
423              
424             perldoc WWW::Google::KnowledgeGraphSearch
425              
426             You can also look for information at:
427              
428             =over 4
429              
430             =item * RT: CPAN's request tracker (report bugs here)
431              
432             L
433              
434             =item * AnnoCPAN: Annotated CPAN documentation
435              
436             L
437              
438             =item * CPAN Ratings
439              
440             L
441              
442             =item * Search CPAN
443              
444             L
445              
446             =back
447              
448             =head1 LICENSE AND COPYRIGHT
449              
450             Copyright (C) 2017 Mohammad S Anwar.
451              
452             This program is free software; you can redistribute it and / or modify it under
453             the terms of the the Artistic License (2.0). You may obtain a copy of the full
454             license at:
455              
456             L
457              
458             Any use, modification, and distribution of the Standard or Modified Versions is
459             governed by this Artistic License.By using, modifying or distributing the Package,
460             you accept this license. Do not use, modify, or distribute the Package, if you do
461             not accept this license.
462              
463             If your Modified Version has been derived from a Modified Version made by someone
464             other than you,you are nevertheless required to ensure that your Modified Version
465             complies with the requirements of this license.
466              
467             This license does not grant you the right to use any trademark, service mark,
468             tradename, or logo of the Copyright Holder.
469              
470             This license includes the non-exclusive, worldwide, free-of-charge patent license
471             to make, have made, use, offer to sell, sell, import and otherwise transfer the
472             Package with respect to any patent claims licensable by the Copyright Holder that
473             are necessarily infringed by the Package. If you institute patent litigation
474             (including a cross-claim or counterclaim) against any party alleging that the
475             Package constitutes direct or contributory patent infringement,then this Artistic
476             License to you shall terminate on the date that such litigation is filed.
477              
478             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
479             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
480             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
481             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
482             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
483             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
484             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
485              
486             =cut
487              
488             1; # End of WWW::Google::KnowledgeGraphSearch