File Coverage

blib/lib/WWW/Google/CustomSearch.pm
Criterion Covered Total %
statement 42 59 71.1
branch 7 14 50.0
condition 5 12 41.6
subroutine 14 15 93.3
pod 1 2 50.0
total 69 102 67.6


line stmt bran cond sub pod time code
1             package WWW::Google::CustomSearch;
2              
3             $WWW::Google::CustomSearch::VERSION = '0.37';
4             $WWW::Google::CustomSearch::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             WWW::Google::CustomSearch - Interface to Google JSON/Atom Custom Search.
9              
10             =head1 VERSION
11              
12             Version 0.37
13              
14             =cut
15              
16 5     5   57155 use 5.006;
  5         31  
17 5     5   2386 use JSON;
  5         46831  
  5         19  
18 5     5   2870 use Data::Dumper;
  5         30755  
  5         236  
19 5     5   2078 use URI;
  5         17925  
  5         132  
20              
21 5     5   1886 use WWW::Google::UserAgent;
  5         452870  
  5         169  
22 5     5   2203 use WWW::Google::UserAgent::DataTypes qw(:all);
  5         642002  
  5         67  
23 5     5   76498 use WWW::Google::CustomSearch::Params qw($FIELDS);
  5         22  
  5         693  
24 5     5   1913 use WWW::Google::CustomSearch::Result;
  5         14  
  5         138  
25              
26 5     5   31 use Moo;
  5         8  
  5         16  
27 5     5   2290 use Types::Standard qw(Bool);
  5         6  
  5         46  
28 5     5   3375 use namespace::autoclean;
  5         10  
  5         19  
29             extends 'WWW::Google::UserAgent';
30              
31             our $BASE_URL = "https://www.googleapis.com/customsearch/v1";
32              
33             has 'prettyprint' => (is => 'ro', isa => TrueFalse);
34             has 'c2coff' => (is => 'ro', isa => Bool);
35             has 'fileType' => (is => 'ro', isa => FileType);
36             has 'hl' => (is => 'ro', isa => InterfaceLang);
37             has 'cr' => (is => 'ro', isa => CountryC);
38             has 'gl' => (is => 'ro', isa => CountryCode);
39             has 'filter' => (is => 'ro', isa => Bool, default => sub { return 1 });
40             has 'imgColorType' => (is => 'ro', isa => ColorType);
41             has 'imgDominantColor' => (is => 'ro', isa => DominantColor);
42             has 'imgSize' => (is => 'ro', isa => ImageSize);
43             has 'imgType' => (is => 'ro', isa => ImageType);
44             has 'lr' => (is => 'ro', isa => LanguageC);
45             has 'num' => (is => 'ro', default => sub { return 10 });
46             has 'safe' => (is => 'ro', default => sub { return 'off' });
47             has 'searchType' => (is => 'ro', isa => SearchType);
48             has 'siteSearchFilter' => (is => 'ro', isa => SearchFilter);
49             has 'start' => (is => 'ro', default => sub { return 1 });
50             has 'alt' => (is => 'ro', isa => FileType, default => sub { return 'json' });
51              
52             has [ qw(callback fields quotaUser userIp cref cx dateRestrict exactTerms
53             excludeTerms googlehost highRange hq linkSite lowRange orTerms
54             relatedSite rights sort siteSearch) ] => (is => 'ro');
55              
56             =head1 DESCRIPTION
57              
58             This module is intended for anyone who wants to write applications that can
59             interact with the JSON/Atom Custom Search API. With Google Custom Search, you can
60             harness the power of Google to create a customized search experience for your own
61             website. You can use the JSON/Atom Custom Search API to retrieve Google Custom
62             Search results programmatically.
63              
64             The JSON / Atom Custom Search API requires the use of an API key, which you can
65             get from the Google APIs console. The API provides 100 search queries per day for
66             free. If you need more, you may sign up for billing in the console.
67              
68             The official Google API document can be found L.
69             For more information about the Google custom search, please click L.
70              
71             Important:The version v1 of the Google JSON/Atom Custom Search API is in Labs and
72             its features might change unexpectedly until it graduates.
73              
74             =head1 SYNOPSIS
75              
76             =head2 Single Page Search
77              
78             use strict; use warnings;
79             use WWW::Google::CustomSearch;
80              
81             my $api_key = 'Your_API_Key';
82             my $cx = 'Search_Engine_Identifier';
83             my $engine = WWW::Google::CustomSearch->new(api_key=>$api_key, cx=>$cx);
84             my $result = $engine->search('Google');
85              
86             print "Search time: ", $result->searchTime, "\n";
87             foreach my $item (@{$result->items}) {
88             print "Snippet: ", $item->snippet, "\n";
89             }
90              
91             =head2 Multiple Page Search
92              
93             use strict; use warnings;
94             use WWW::Google::CustomSearch;
95              
96             my $api_key = 'Your_API_Key';
97             my $cx = 'Search_Engine_Identifier';
98             my $engine = WWW::Google::CustomSearch->new(api_key=>$api_key, cx=>$cx);
99             my $result = $engine->search('Google');
100              
101             if (defined $result) {
102             my $page_count = 2;
103             my $page_no = 1;
104             print "Result Count: ", $result->totalResults, "\n";
105             while (defined $result && ($page_no <= $page_count)) {
106             print "Page [$page_no]:\n\n";
107             foreach my $item (@{$result->items}) {
108             print "Snippet: ", $item->snippet, "\n";
109             }
110             print "----------------------------------\n\n";
111              
112             sleep 10;
113             my $page = $result->nextPage;
114             $result = $page->fetch;
115             $page_no++;
116             }
117             }
118              
119             See L for further details of the search result.
120              
121             =head1 LANGUAGES (lr)
122              
123             +-----------------------+---------------------------------------------------+
124             | Language | Value |
125             +-----------------------+---------------------------------------------------+
126             | Arabic | lang_ar |
127             | Bulgarian | lang_bg |
128             | Catalan | lang_ca |
129             | Chinese (Simplified) | lang_zh-CN |
130             | Chinese (Traditional) | lang_zh-TW |
131             | Croatian | lang_hr |
132             | Czech | lang_cs |
133             | Danish | lang_da |
134             | Dutch | lang_nl |
135             | English | lang_en |
136             | Estonian | lang_et |
137             | Finnish | lang_fi |
138             | French | lang_fr |
139             | German | lang_de |
140             | Greek | lang_el |
141             | Hebrew | lang_iw |
142             | Hungarian | lang_hu |
143             | Icelandic | lang_is |
144             | Indonesian | lang_id |
145             | Italian | lang_it |
146             | Japanese | lang_ja |
147             | Korean | lang_ko |
148             | Latvian | lang_lv |
149             | Lithuanian | lang_lt |
150             | Norwegian | lang_no |
151             | Polish | lang_pl |
152             | Portuguese | lang_pt |
153             | Romanian | lang_ro |
154             | Russian | lang_ru |
155             | Serbian | lang_sr |
156             | Slovak | lang_sk |
157             | Slovenian | lang_sl |
158             | Spanish | lang_es |
159             | Swedish | lang_sv |
160             | Turkish | lang_tr |
161             +-----------------------+---------------------------------------------------+
162              
163             =head1 Country Collection Values (cr)
164              
165             +----------------------------------------------+----------------------------+
166             | Country | Country Collection Name |
167             +----------------------------------------------+----------------------------+
168             | Afghanistan | countryAF |
169             | Albania | countryAL |
170             | Algeria | countryDZ |
171             | American Samoa | countryAS |
172             | Andorra | countryAD |
173             | Angola | countryAO |
174             | Anguilla | countryAI |
175             | Antarctica | countryAQ |
176             | Antigua and Barbuda | countryAG |
177             | Argentina | countryAR |
178             | Armenia | countryAM |
179             | Aruba | countryAW |
180             | Australia | countryAU |
181             | Austria | countryAT |
182             | Azerbaijan | countryAZ |
183             | Bahamas | countryBS |
184             | Bahrain | countryBH |
185             | Bangladesh | countryBD |
186             | Barbados | countryBB |
187             | Belarus | countryBY |
188             | Belgium | countryBE |
189             | Belize | countryBZ |
190             | Benin | countryBJ |
191             | Bermuda | countryBM |
192             | Bhutan | countryBT |
193             | Bolivia | countryBO |
194             | Bosnia and Herzegovina | countryBA |
195             | Botswana | countryBW |
196             | Bouvet Island | countryBV |
197             | Brazil | countryBR |
198             | British Indian Ocean Territory | countryIO |
199             | Brunei Darussalam | countryBN |
200             | Bulgaria | countryBG |
201             | Burkina Faso | countryBF |
202             | Burundi | countryBI |
203             | Cambodia | countryKH |
204             | Cameroon | countryCM |
205             | Canada | countryCA |
206             | Cape Verde | countryCV |
207             | Cayman Islands | countryKY |
208             | Central African Republic | countryCF |
209             | Chad | countryTD |
210             | Chile | countryCL |
211             | China | countryCN |
212             | Christmas Island | countryCX |
213             | Cocos (Keeling) Islands | countryCC |
214             | Colombia | countryCO |
215             | Comoros | countryKM |
216             | Congo | countryCG |
217             | Congo, the Democratic Republic of the | countryCD |
218             | Cook Islands | countryCK |
219             | Costa Rica | countryCR |
220             | Cote D'ivoire | countryCI |
221             | Croatia (Hrvatska) | countryHR |
222             | Cuba | countryCU |
223             | Cyprus | countryCY |
224             | Czech Republic | countryCZ |
225             | Denmark | countryDK |
226             | Djibouti | countryDJ |
227             | Dominica | countryDM |
228             | Dominican Republic | countryDO |
229             | East Timor | countryTP |
230             | Ecuador | countryEC |
231             | Egypt | countryEG |
232             | El Salvador | countrySV |
233             | Equatorial Guinea | countryGQ |
234             | Eritrea | countryER |
235             | Estonia | countryEE |
236             | Ethiopia | countryET |
237             | European Union | countryEU |
238             | Falkland Islands (Malvinas) | countryFK |
239             | Faroe Islands | countryFO |
240             | Fiji | countryFJ |
241             | Finland | countryFI |
242             | France | countryFR |
243             | France, Metropolitan | countryFX |
244             | French Guiana | countryGF |
245             | French Polynesia | countryPF |
246             | French Southern Territories | countryTF |
247             | Gabon | countryGA |
248             | Gambia | countryGM |
249             | Georgia | countryGE |
250             | Germany | countryDE |
251             | Ghana | countryGH |
252             | Gibraltar | countryGI |
253             | Greece | countryGR |
254             | Greenland | countryGL |
255             | Grenada | countryGD |
256             | Guadeloupe | countryGP |
257             | Guam | countryGU |
258             | Guatemala | countryGT |
259             | Guinea | countryGN |
260             | Guinea-Bissau | countryGW |
261             | Guyana | countryGY |
262             | Haiti | countryHT |
263             | Heard Island and Mcdonald Islands | countryHM |
264             | Holy See (Vatican City State) | countryVA |
265             | Honduras | countryHN |
266             | Hong Kong | countryHK |
267             | Hungary | countryHU |
268             | Iceland | countryIS |
269             | India | countryIN |
270             | Indonesia | countryID |
271             | Iran, Islamic Republic of | countryIR |
272             | Iraq | countryIQ |
273             | Ireland | countryIE |
274             | Israel | countryIL |
275             | Italy | countryIT |
276             | Jamaica | countryJM |
277             | Japan | countryJP |
278             | Jordan | countryJO |
279             | Kazakhstan | countryKZ |
280             | Kenya | countryKE |
281             | Kiribati | countryKI |
282             | Korea, Democratic People's Republic of | countryKP |
283             | Korea, Republic of | countryKR |
284             | Kuwait | countryKW |
285             | Kyrgyzstan | countryKG |
286             | Lao People's Democratic Republic | countryLA |
287             | Latvia | countryLV |
288             | Lebanon | countryLB |
289             | Lesotho | countryLS |
290             | Liberia | countryLR |
291             | Libyan Arab Jamahiriya | countryLY |
292             | Liechtenstein | countryLI |
293             | Lithuania | countryLT |
294             | Luxembourg | countryLU |
295             | Macao | countryMO |
296             | Macedonia, the Former Yugosalv Republic of | countryMK |
297             | Madagascar | countryMG |
298             | Malawi | countryMW |
299             | Malaysia | countryMY |
300             | Maldives | countryMV |
301             | Mali | countryML |
302             | Malta | countryMT |
303             | Marshall Islands | countryMH |
304             | Martinique | countryMQ |
305             | Mauritania | countryMR |
306             | Mauritius | countryMU |
307             | Mayotte | countryYT |
308             | Mexico | countryMX |
309             | Micronesia, Federated States of | countryFM |
310             | Moldova, Republic of | countryMD |
311             | Monaco | countryMC |
312             | Mongolia | countryMN |
313             | Montserrat | countryMS |
314             | Morocco | countryMA |
315             | Mozambique | countryMZ |
316             | Myanmar | countryMM |
317             | Namibia | countryNA |
318             | Nauru | countryNR |
319             | Nepal | countryNP |
320             | Netherlands | countryNL |
321             | Netherlands Antilles | countryAN |
322             | New Caledonia | countryNC |
323             | New Zealand | countryNZ |
324             | Nicaragua | countryNI |
325             | Niger | countryNE |
326             | Nigeria | countryNG |
327             | Niue | countryNU |
328             | Norfolk Island | countryNF |
329             | Northern Mariana Islands | countryMP |
330             | Norway | countryNO |
331             | Oman | countryOM |
332             | Pakistan | countryPK |
333             | Palau | countryPW |
334             | Palestinian Territory | countryPS |
335             | Panama | countryPA |
336             | Papua New Guinea | countryPG |
337             | Paraguay | countryPY |
338             | Peru | countryPE |
339             | Philippines | countryPH |
340             | Pitcairn | countryPN |
341             | Poland | countryPL |
342             | Portugal | countryPT |
343             | Puerto Rico | countryPR |
344             | Qatar | countryQA |
345             | Reunion | countryRE |
346             | Romania | countryRO |
347             | Russian Federation | countryRU |
348             | Rwanda | countryRW |
349             | Saint Helena | countrySH |
350             | Saint Kitts and Nevis | countryKN |
351             | Saint Lucia | countryLC |
352             | Saint Pierre and Miquelon | countryPM |
353             | Saint Vincent and the Grenadines | countryVC |
354             | Samoa | countryWS |
355             | San Marino | countrySM |
356             | Sao Tome and Principe | countryST |
357             | Saudi Arabia | countrySA |
358             | Senegal | countrySN |
359             | Serbia and Montenegro | countryCS |
360             | Seychelles | countrySC |
361             | Sierra Leone | countrySL |
362             | Singapore | countrySG |
363             | Slovakia | countrySK |
364             | Slovenia | countrySI |
365             | Solomon Islands | countrySB |
366             | Somalia | countrySO |
367             | South Africa | countryZA |
368             | South Georgia and the South Sandwich Islands | countryGS |
369             | Spain | countryES |
370             | Sri Lanka | countryLK |
371             | Sudan | countrySD |
372             | Suriname | countrySR |
373             | Svalbard and Jan Mayen | countrySJ |
374             | Swaziland | countrySZ |
375             | Sweden | countrySE |
376             | Switzerland | countryCH |
377             | Syrian Arab Republic | countrySY |
378             | Taiwan, Province of China | countryTW |
379             | Tajikistan | countryTJ |
380             | Tanzania, United Republic of | countryTZ |
381             | Thailand | countryTH |
382             | Togo | countryTG |
383             | Tokelau | countryTK |
384             | Tonga | countryTO |
385             | Trinidad and Tobago | countryTT |
386             | Tunisia | countryTN |
387             | Turkey | countryTR |
388             | Turkmenistan | countryTM |
389             | Turks and Caicos Islands | countryTC |
390             | Tuvalu | countryTV |
391             | Uganda | countryUG |
392             | Ukraine | countryUA |
393             | United Arab Emirates | countryAE |
394             | United Kingdom | countryUK |
395             | United States | countryUS |
396             | United States Minor Outlying Islands | countryUM |
397             | Uruguay | countryUY |
398             | Uzbekistan | countryUZ |
399             | Vanuatu | countryVU |
400             | Venezuela | countryVE |
401             | Vietnam | countryVN |
402             | Virgin Islands, British | countryVG |
403             | Virgin Islands, U.S. | countryVI |
404             | Wallis and Futuna | countryWF |
405             | Western Sahara | countryEH |
406             | Yemen | countryYE |
407             | Yugoslavia | countryYU |
408             | Zambia | countryZM |
409             | Zimbabwe | countryZW |
410             +----------------------------------------------+----------------------------+
411              
412             =head1 Country Codes (gl)
413              
414             +----------------------------------------------+----------------------------+
415             | Country | Country Code |
416             +----------------------------------------------+----------------------------+
417             | Afghanistan | af |
418             | Albania | al |
419             | Algeria | dz |
420             | American Samoa | as |
421             | Andorra | ad |
422             | Angola | ao |
423             | Anguilla | ai |
424             | Antarctica | aq |
425             | Antigua and Barbuda | ag |
426             | Argentina | ar |
427             | Armenia | am |
428             | Aruba | aw |
429             | Australia | au |
430             | Austria | at |
431             | Azerbaijan | az |
432             | Bahamas | bs |
433             | Bahrain | bh |
434             | Bangladesh | bd |
435             | Barbados | bb |
436             | Belarus | by |
437             | Belgium | be |
438             | Belize | bz |
439             | Benin | bj |
440             | Bermuda | bm |
441             | Bhutan | bt |
442             | Bolivia | bo |
443             | Bosnia and Herzegovina | ba |
444             | Botswana | bw |
445             | Bouvet Island | bv |
446             | Brazil | br |
447             | British Indian Ocean Territory | io |
448             | Brunei Darussalam | bn |
449             | Bulgaria | bg |
450             | Burkina Faso | bf |
451             | Burundi | bi |
452             | Cambodia | kh |
453             | Cameroon | cm |
454             | Canada | ca |
455             | Cape Verde | cv |
456             | Cayman Islands | ky |
457             | Central African Republic | cf |
458             | Chad | td |
459             | Chile | cl |
460             | China | cn |
461             | Christmas Island | cx |
462             | Cocos (Keeling) Islands | cc |
463             | Colombia | co |
464             | Comoros | km |
465             | Congo | cg |
466             | Congo, the Democratic Republic of the | cd |
467             | Cook Islands | ck |
468             | Costa Rica | cr |
469             | Cote D'ivoire | ci |
470             | Croatia | hr |
471             | Cuba | cu |
472             | Cyprus | cy |
473             | Czech Republic | cz |
474             | Denmark | dk |
475             | Djibouti | dj |
476             | Dominica | dm |
477             | Dominican Republic | do |
478             | Ecuador | ec |
479             | Egypt | eg |
480             | El Salvador | sv |
481             | Equatorial Guinea | gq |
482             | Eritrea | er |
483             | Estonia | ee |
484             | Ethiopia | et |
485             | Falkland Islands (Malvinas) | fk |
486             | Faroe Islands | fo |
487             | Fiji | fj |
488             | Finland | fi |
489             | France | fr |
490             | French Guiana | gf |
491             | French Polynesia | pf |
492             | French Southern Territories | tf |
493             | Gabon | ga |
494             | Gambia | gm |
495             | Georgia | ge |
496             | Germany | de |
497             | Ghana | gh |
498             | Gibraltar | gi |
499             | Greece | gr |
500             | Greenland | gl |
501             | Grenada | gd |
502             | Guadeloupe | gp |
503             | Guam | gu |
504             | Guatemala | gt |
505             | Guinea | gn |
506             | Guinea-Bissau | gw |
507             | Guyana | gy |
508             | Haiti | ht |
509             | Heard Island and Mcdonald Islands | hm |
510             | Holy See (Vatican City State) | va |
511             | Honduras | hn |
512             | Hong Kong | hk |
513             | Hungary | hu |
514             | Iceland | is |
515             | India | in |
516             | Indonesia | id |
517             | Iran, Islamic Republic of | ir |
518             | Iraq | iq |
519             | Ireland | ie |
520             | Israel | il |
521             | Italy | it |
522             | Jamaica | jm |
523             | Japan | jp |
524             | Jordan | jo |
525             | Kazakhstan | kz |
526             | Kenya | ke |
527             | Kiribati | ki |
528             | Korea, Democratic People's Republic of | kp |
529             | Korea, Republic of | kr |
530             | Kuwait | kw |
531             | Kyrgyzstan | kg |
532             | Lao People's Democratic Republic | la |
533             | Latvia | lv |
534             | Lebanon | lb |
535             | Lesotho | ls |
536             | Liberia | lr |
537             | Libyan Arab Jamahiriya | ly |
538             | Liechtenstein | li |
539             | Lithuania | lt |
540             | Luxembourg | lu |
541             | Macao | mo |
542             | Macedonia, the Former Yugosalv Republic of | mk |
543             | Madagascar | mg |
544             | Malawi | mw |
545             | Malaysia | my |
546             | Maldives | mv |
547             | Mali | ml |
548             | Malta | mt |
549             | Marshall Islands | mh |
550             | Martinique | mq |
551             | Mauritania | mr |
552             | Mauritius | mu |
553             | Mayotte | yt |
554             | Mexico | mx |
555             | Micronesia, Federated States of | fm |
556             | Moldova, Republic of | md |
557             | Monaco | mc |
558             | Mongolia | mn |
559             | Montserrat | ms |
560             | Morocco | ma |
561             | Mozambique | mz |
562             | Myanmar | mm |
563             | Namibia | na |
564             | Nauru | nr |
565             | Nepal | np |
566             | Netherlands | nl |
567             | Netherlands Antilles | an |
568             | New Caledonia | nc |
569             | New Zealand | nz |
570             | Nicaragua | ni |
571             | Niger | ne |
572             | Nigeria | ng |
573             | Niue | nu |
574             | Norfolk Island | nf |
575             | Northern Mariana Islands | mp |
576             | Norway | no |
577             | Oman | om |
578             | Pakistan | pk |
579             | Palau | pw |
580             | Palestinian Territory, Occupied | ps |
581             | Panama | pa |
582             | Papua New Guinea | pg |
583             | Paraguay | py |
584             | Peru | pe |
585             | Philippines | ph |
586             | Pitcairn | pn |
587             | Poland | pl |
588             | Portugal | pt |
589             | Puerto Rico | pr |
590             | Qatar | qa |
591             | Reunion | re |
592             | Romania | ro |
593             | Russian Federation | ru |
594             | Rwanda | rw |
595             | Saint Helena | sh |
596             | Saint Kitts and Nevis | kn |
597             | Saint Lucia | lc |
598             | Saint Pierre and Miquelon | pm |
599             | Saint Vincent and the Grenadines | vc |
600             | Samoa | ws |
601             | San Marino | sm |
602             | Sao Tome and Principe | st |
603             | Saudi Arabia | sa |
604             | Senegal | sn |
605             | Serbia and Montenegro | cs |
606             | Seychelles | sc |
607             | Sierra Leone | sl |
608             | Singapore | sg |
609             | Slovakia | sk |
610             | Slovenia | si |
611             | Solomon Islands | sb |
612             | Somalia | so |
613             | South Africa | za |
614             | South Georgia and the South Sandwich Islands | gs |
615             | Spain | es |
616             | Sri Lanka | lk |
617             | Sudan | sd |
618             | Suriname | sr |
619             | Svalbard and Jan Mayen | sj |
620             | Swaziland | sz |
621             | Sweden | se |
622             | Switzerland | ch |
623             | Syrian Arab Republic | sy |
624             | Taiwan, Province of China | tw |
625             | Tajikistan | tj |
626             | Tanzania, United Republic of | tz |
627             | Thailand | th |
628             | Timor-Leste | tl |
629             | Togo | tg |
630             | Tokelau | tk |
631             | Tonga | to |
632             | Trinidad and Tobago | tt |
633             | Tunisia | tn |
634             | Turkey | tr |
635             | Turkmenistan | tm |
636             | Turks and Caicos Islands | tc |
637             | Tuvalu | tv |
638             | Uganda | ug |
639             | Ukraine | ua |
640             | United Arab Emirates | ae |
641             | United Kingdom | uk |
642             | United States | us |
643             | United States Minor Outlying Islands | um |
644             | Uruguay | uy |
645             | Uzbekistan | uz |
646             | Vanuatu | vu |
647             | Venezuela | ve |
648             | Viet Nam | vn |
649             | Virgin Islands, British | vg |
650             | Virgin Islands, U.S. | vi |
651             | Wallis and Futuna | wf |
652             | Western Sahara | eh |
653             | Yemen | ye |
654             | Zambia | zm |
655             | Zimbabwe | zw |
656             +----------------------------------------------+----------------------------+
657              
658             =head1 Interface Language (hl)
659              
660             +---------------------------+-----------------------------------------------+
661             | Display Language | Parameter Value |
662             +---------------------------+-----------------------------------------------+
663             | Afrikaans | af |
664             | Albanian | sq |
665             | Amharic | sm |
666             | Arabic | ar |
667             | Azerbaijani | az |
668             | Basque | eu |
669             | Belarusian | be |
670             | Bengali | bn |
671             | Bihari | bh |
672             | Bosnian | bs |
673             | Bulgarian | bg |
674             | Catalan | ca |
675             | Chinese (Simplified) | zh-CN |
676             | Chinese (Traditional) | zh-TW |
677             | Croatian | hr |
678             | Czech | cs |
679             | Danish | da |
680             | Dutch | nl |
681             | English | en |
682             | Esperanto | eo |
683             | Estonian | et |
684             | Faroese | fo |
685             | Finnish | fi |
686             | French | fr |
687             | Frisian | fy |
688             | Galician | gl |
689             | Georgian | ka |
690             | German | de |
691             | Greek | el |
692             | Gujarati | gu |
693             | Hebrew | iw |
694             | Hindi | hi |
695             | Hungarian | hu |
696             | Icelandic | is |
697             | Indonesian | id |
698             | Interlingua | ia |
699             | Irish | ga |
700             | Italian | it |
701             | Japanese | ja |
702             | Javanese | jw |
703             | Kannada | kn |
704             | Korean | ko |
705             | Latin | la |
706             | Latvian | lv |
707             | Lithuanian | lt |
708             | Macedonian | mk |
709             | Malay | ms |
710             | Malayam | ml |
711             | Maltese | mt |
712             | Marathi | mr |
713             | Nepali | ne |
714             | Norwegian | no |
715             | Norwegian (Nynorsk) | nn |
716             | Occitan | oc |
717             | Persian | fa |
718             | Polish | pl |
719             | Portuguese (Brazil) | pt-BR |
720             | Portuguese (Portugal) | pt-PT |
721             | Punjabi | pa |
722             | Romanian | ro |
723             | Russian | ru |
724             | Scots Gaelic | gd |
725             | Serbian | sr |
726             | Sinhalese | si |
727             | Slovak | sk |
728             | Slovenian | sl |
729             | Spanish | es |
730             | Sudanese | su |
731             | Swahili | sw |
732             | Swedish | sv |
733             | Tagalog | tl |
734             | Tamil | ta |
735             | Telugu | te |
736             | Thai | th |
737             | Tigrinya | ti |
738             | Turkish | tr |
739             | Ukrainian | uk |
740             | Urdu | ur |
741             | Uzbek | uz |
742             | Vietnamese | vi |
743             | Welsh | cy |
744             | Xhosa | xh |
745             | Zulu | zu |
746             +---------------------------+-----------------------------------------------+
747              
748             =head1 File Types
749              
750             +--------------------------------+------------------------------------------+
751             | File Type | Extension |
752             +--------------------------------+------------------------------------------+
753             | Adobe Flash | .swf |
754             | Adobe Portable Document Format | .pdf |
755             | Adobe PostScript | .ps |
756             | Autodesk Design Web Format | .dwf |
757             | Google Earth | .kml, .kmz |
758             | GPS eXchange Format | .gpx |
759             | Hancom Hanword | .hwp |
760             | HTML | .htm, .html |
761             | Microsoft Excel | .xls, .xlsx |
762             | Microsoft PowerPoint | .ppt, .pptx |
763             | Microsoft Word | .doc, .docx |
764             | OpenOffice presentation | .odp |
765             | OpenOffice spreadsheet | .ods |
766             | OpenOffice text | .odt |
767             | Rich Text Format | .rtf, .wri |
768             | Scalable Vector Graphics | .svg |
769             | TeX/LaTeX | .tex |
770             | Text | .txt, .text |
771             | Basic source code | .bas |
772             | C/C++ source code | .c, .cc, .cpp, .cxx, .h, .hpp |
773             | C# source code | .cs |
774             | Java source code | .java |
775             | Perl source code | .pl |
776             | Python source code | .py |
777             | Wireless Markup Language | .wml, .wap |
778             | XML | .xml |
779             +--------------------------------+------------------------------------------+
780              
781             =head1 CONSTRUCTOR
782              
783             The constructor expects application API Key and Custom search engine identifier.
784             Use either C/C to specify the custom search engine you want to perform
785             this search. If both are specified, C is used.
786              
787             +------------------+--------------------------------------------------------+
788             | Key | Description |
789             +------------------+--------------------------------------------------------+
790             | api_key | Your application API Key. |
791             | | |
792             | callback | Callback function. Name of the JavaScript callback |
793             | | function that handles the response. |
794             | | |
795             | fields | Selector specifying a subset of fields to include in |
796             | | the response. |
797             | | |
798             | prettyprint | Returns a response with indentations and line breaks. |
799             | | If prettyprint=true, the results returned by the server|
800             | | will be human readable (pretty printed). |
801             | | |
802             | userIp | IP address of the end user for whom the API call is |
803             | | being made. |
804             | | |
805             | quotaUser | Alternative to userIp. Lets you enforce per-user quotas|
806             | | from a server-side application even in cases when the |
807             | | user's IP address is unknown. You can choose any |
808             | | arbitrary string that uniquely identifies a user, but |
809             | | it is limited to 40 characters. Overrides userIp if |
810             | | both are provided. |
811             | | |
812             | c2coff | Enables or disables Simplified and Traditional Chinese |
813             | | Search. The default value for this parameter is 0, |
814             | | meaning that the feature is enabled. Supported values |
815             | | are: |
816             | | * 1 - Disabled |
817             | | * 2 - Enabled (default) |
818             | | |
819             | cr | Restricts search results to documents originating in a |
820             | | particular country. |
821             | | See section "Country Collection Name" for valid data. |
822             | | |
823             | cref | For a linked custom search engine. |
824             | | |
825             | cx | For a search engine created with the Google Custom |
826             | | Search page. |
827             | | |
828             | dateRestrict | Restricts results to URLs based on date. Supported |
829             | |values include: |
830             | | * d[number]: requests results from the specified number|
831             | | of past days. |
832             | | * w[number]: requests results from the specified number|
833             | | of past weeks. |
834             | | * m[number]: requests results from the specified number|
835             | | of past months. |
836             | | * y[number]: requests results from the specified number|
837             | | of past years. |
838             | | |
839             | exactTerms | Identifies a phrase that all documents in the search |
840             | | results must contain. |
841             | | |
842             | excludeTerms | Identifies a word or phrase that should not appear in |
843             | | any documents in the search results. |
844             | | |
845             | fileType | Restricts results to files of a specified extension. |
846             | | |
847             | filter | Controls turning on / off the duplicate content filter.|
848             | | * filter=0 - Turns off the duplicate content filter. |
849             | | * filter=1 - Turns on the duplicate content filter |
850             | | (default). |
851             | | |
852             | gl | Geolocation of end user. The gl parameter value is a |
853             | | two-letter country code. See section "Country Codes". |
854             | | |
855             | googlehost | The local Google domain (for example, google.com, |
856             | | google.de, or google.fr) to use to perform the search. |
857             | | Default "www.google.com". |
858             | | |
859             | highRange | Specifies the ending value for a search range. |
860             | | |
861             | hl | Sets the user interface language. Explicitly setting |
862             | | this parameter improves the performance and the quality|
863             | | of your search results.See section "Interface Language"|
864             | | |
865             | hq | Appends the specified query terms to the query, as if |
866             | |they were combined with a logical AND operator. |
867             | | |
868             | imgColorType | Returns black and white, grayscale, or color images. |
869             | | Acceptable values are mono, gray, and color. |
870             | | |
871             | imgDominantColor | Returns images of a specific dominant color. Acceptable|
872             | | values are "black", "blue", "brown", "gray", "green", |
873             | | "pink", "purple", "teal", "white", "yellow". |
874             | | |
875             | imgSize | Returns images of a specified size. Acceptable values |
876             | | are "huge","icon","large","medium","small","xlarge", |
877             | | "xxlarge". |
878             | | |
879             | imgType | Returns images of a type. Acceptable values are |
880             | | "clipart", "face", "news", "lineart", "photo". |
881             | | |
882             | linkSite | Specifies that all search results should contain a link|
883             | | to a particular URL |
884             | | |
885             | lowRange | Specifies the starting value for a search range. |
886             | | |
887             | lr | The language restriction for the search results. |
888             | | |
889             | num | Number of search results to return. Valid values are |
890             | | integers between 1 and 10, Default is 10. |
891             | | |
892             | orTerms | Provides additional search terms to check for in a |
893             | | document,where each document in the search results must|
894             | | contain at least one of the additional search terms. |
895             | | |
896             | relatedSite | Specifies that all search results should be pages that |
897             | | are related to the specified URL. |
898             | | |
899             | rights | Filters based on licensing. Supported values include: |
900             | | * cc_publicdomain |
901             | | * cc_attribute |
902             | | * cc_sharealike |
903             | | * cc_noncommercial |
904             | | * cc_nonderived, and combinations of these. |
905             | | |
906             | safe | Search safety level. Default is off. Possible values |
907             | | are: |
908             | | * high -enables highest level of safe search filtering.|
909             | | * medium - enables moderate safe search filtering. |
910             | | * off - disables safe search filtering. |
911             | | |
912             | searchType | Specifies the search type: image. If unspecified, |
913             | | results are limited to webpages. |
914             | | |
915             | siteSearch | Specifies all search results should be pages from a |
916             | | given site. |
917             | | |
918             | siteSearchFilter | Controls whether to include or exclude results from the|
919             | | site named in the siteSearch parameter. Acceptable |
920             | | values are: |
921             | | * "e": exclude |
922             | | * "i": include |
923             | | |
924             | sort | The sort expression to apply to the results. |
925             | | |
926             | start | The index of the first result to return. Default is 1. |
927             +------------------+--------------------------------------------------------+
928              
929             =cut
930              
931             sub BUILD {
932 12     12 0 951 my ($self) = @_;
933              
934 12 100 66     74 die("ERROR: cx or cref must be specified.") unless ($self->cx || $self->cref);
935              
936 10         33 $self->_validate;
937             }
938              
939             =head1 METHODS
940              
941             =head2 search($query_string)
942              
943             Get search result L for the given query, which
944             can be used to probe for further information about the search result.
945              
946             use strict; use warnings;
947             use WWW::Google::CustomSearch;
948              
949             my $api_key = 'Your_API_Key';
950             my $cx = 'Search_Engine_Identifier';
951             my $engine = WWW::Google::CustomSearch->new(api_key=>$api_key, cx=>$cx, start=>2);
952             my $result = $engine->search('Google');
953              
954             =cut
955              
956             sub search {
957 1     1 1 6 my ($self, $query) = @_;
958 1 50       8 die "ERROR: Missing query string." unless defined $query;
959              
960 0         0 my $url = $self->_url($query);
961 0         0 my $response = $self->get($url);
962 0         0 my $contents = from_json($response->{content});
963              
964 0         0 return WWW::Google::CustomSearch::Result->new(raw => $contents, api_key => $self->api_key);
965             }
966              
967             #
968             # PRIVATE METHODS
969             #
970              
971             sub _validate {
972 10     10   17 my ($self) = @_;
973              
974 10         12 foreach my $key (keys %{$FIELDS}) {
  10         56  
975 224 100 100     424 next unless (defined $self->{$key} && exists $FIELDS->{$key}->{check});
976              
977             die "ERROR: Invalid data for param: $key [$self->{$key}]"
978 21 100       56 unless ($FIELDS->{$key}->{check}->($self->{$key}));
979             }
980             }
981              
982             sub _url {
983 0     0     my ($self, $query) = @_;
984              
985 0           my $url = URI->new($BASE_URL);
986 0           my $params = {
987             'key' => $self->api_key,
988             'q' => $query,
989             };
990              
991 0 0 0       if (($self->cx) || ($self->cx && $self->cref)) {
    0 0        
992 0           $params->{'cx'} = $self->cx;
993             }
994             elsif ($self->cref) {
995 0           $params->{'cref'} = $self->cref;
996             }
997              
998 0           foreach my $key (keys %{$FIELDS}) {
  0            
999 0 0         next unless defined $self->{$key};
1000 0           my $value_template = sprintf('%%%s', $FIELDS->{$key}->{type});
1001 0           $params->{$key} = sprintf($value_template, $self->{$key});
1002             }
1003              
1004 0           $url->query_form($params);
1005              
1006 0           return $url;
1007             }
1008              
1009             =head1 AUTHOR
1010              
1011             Mohammad S Anwar, C<< >>
1012              
1013             =head1 REPOSITORY
1014              
1015             L
1016              
1017             =head1 CONTRIBUTORS
1018              
1019             David Kitcher-Jones (m4ddav3)
1020              
1021             =head1 BUGS
1022              
1023             Please report any bugs or feature requests to C
1024             rt.cpan.org>, or through the web interface at L.
1025             I will be notified, and then you'll automatically be notified of progress on your
1026             bug as I make changes.
1027              
1028             =head1 SUPPORT
1029              
1030             You can find documentation for this module with the perldoc command.
1031              
1032             perldoc WWW::Google::CustomSearch
1033              
1034             You can also look for information at:
1035              
1036             =over 4
1037              
1038             =item * RT: CPAN's request tracker (report bugs here)
1039              
1040             L
1041              
1042             =item * AnnoCPAN: Annotated CPAN documentation
1043              
1044             L
1045              
1046             =item * CPAN Ratings
1047              
1048             L
1049              
1050             =item * Search CPAN
1051              
1052             L
1053              
1054             =back
1055              
1056             =head1 LICENSE AND COPYRIGHT
1057              
1058             Copyright (C) 2011 - 2015 Mohammad S Anwar.
1059              
1060             This program is free software; you can redistribute it and / or modify it under
1061             the terms of the the Artistic License (2.0). You may obtain a copy of the full
1062             license at:
1063              
1064             L
1065              
1066             Any use, modification, and distribution of the Standard or Modified Versions is
1067             governed by this Artistic License.By using, modifying or distributing the Package,
1068             you accept this license. Do not use, modify, or distribute the Package, if you do
1069             not accept this license.
1070              
1071             If your Modified Version has been derived from a Modified Version made by someone
1072             other than you,you are nevertheless required to ensure that your Modified Version
1073             complies with the requirements of this license.
1074              
1075             This license does not grant you the right to use any trademark, service mark,
1076             tradename, or logo of the Copyright Holder.
1077              
1078             This license includes the non-exclusive, worldwide, free-of-charge patent license
1079             to make, have made, use, offer to sell, sell, import and otherwise transfer the
1080             Package with respect to any patent claims licensable by the Copyright Holder that
1081             are necessarily infringed by the Package. If you institute patent litigation
1082             (including a cross-claim or counterclaim) against any party alleging that the
1083             Package constitutes direct or contributory patent infringement,then this Artistic
1084             License to you shall terminate on the date that such litigation is filed.
1085              
1086             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
1087             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
1088             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
1089             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
1090             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
1091             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
1092             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1093              
1094             =cut
1095              
1096             1; # End of WWW::Google::CustomSearch