/lib/DDG/Spice/Rainfall.pm

https://github.com/pardocz/zeroclickinfo-spice · Perl · 81 lines · 58 code · 15 blank · 8 comment · 6 complexity · 03d053564c22a3259ce7278a33794d0b MD5 · raw file

  1. package DDG::Spice::Rainfall;
  2. # ABSTRACT: Returns the annual rainfall (precipitation) of the country searched
  3. use DDG::Spice;
  4. use Locale::Country;
  5. primary_example_queries "rainfall australia";
  6. secondary_example_queries "australia annual rainfall";
  7. description "Shows annual rainfall";
  8. name "Rainfall";
  9. code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Rainfall.pm";
  10. topics "everyday";
  11. category "geography";
  12. attribution github => ['https://github.com/chrisjwilsoncom', 'chrisjwilsoncom'];
  13. triggers any => "rainfall", "rainfall in", "annual rainfall", "annual rainfall in", "average rainfall", "average annual rainfall", "average rainfall in";
  14. spice from => '([^/]+)/?(?:([^/]+)/?(?:([^/]+)|)|)';
  15. spice to => 'http://api.worldbank.org/countries/$1/indicators/AG.LND.PRCP.MM?&date=$2&format=json';
  16. spice wrap_jsonp_callback => 1;
  17. # Country alias
  18. Locale::Country::add_country_alias("Lao People's Democratic Republic" => "Laos");
  19. Locale::Country::add_country_alias('Russian Federation' => 'Russia');
  20. # Country rename
  21. Locale::Country::rename_country('ae' => 'the United Arab Emirates');
  22. Locale::Country::rename_country('do' => 'the Dominican Republic');
  23. Locale::Country::rename_country('gb' => 'the United Kingdom');
  24. Locale::Country::rename_country('kr' => "the Republic of Korea");
  25. Locale::Country::rename_country('kp' => "the Democratic People's Republic of Korea");
  26. Locale::Country::rename_country('ky' => 'the Cayman Islands');
  27. Locale::Country::rename_country('mp' => 'the Northern Mariana Islands');
  28. Locale::Country::rename_country('nl' => 'the Netherlands');
  29. Locale::Country::rename_country('ph' => 'the Philippines');
  30. Locale::Country::rename_country('ru' => 'the Russian Federation');
  31. Locale::Country::rename_country('tw' => 'Taiwan');
  32. Locale::Country::rename_country('us' => 'the United States');
  33. Locale::Country::rename_country('va' => 'the Holy See (Vatican City State)');
  34. Locale::Country::rename_country('vg' => 'the British Virgin Islands');
  35. Locale::Country::rename_country('vi' => 'the US Virgin Islands');
  36. # Current date and time
  37. my $curYear = (localtime)[5] + 1900;
  38. # Vaild reporting dates for AG.LND.PRCP.MM indicator (every 5 years)
  39. my @reportingDates = (2012,2017,2022,2027,2032);
  40. handle remainder_lc => sub {
  41. my ($validDate, $countryName, $countryCode, $countryCodeTwo);
  42. return if ($_ eq '');
  43. $countryName = shift;
  44. $countryCode = country2code($countryName, LOCALE_CODE_ALPHA_3); # Return alpha-3 country code
  45. if($countryCode) {
  46. $countryName = code2country(country2code($countryName, LOCALE_CODE_ALPHA_2), LOCALE_CODE_ALPHA_2);
  47. } else {
  48. $countryName = code2country(country2code(code2country($countryName, LOCALE_CODE_ALPHA_3), LOCALE_CODE_ALPHA_2), LOCALE_CODE_ALPHA_2);
  49. $countryCode = country2code($countryName, LOCALE_CODE_ALPHA_3);
  50. }
  51. return unless defined $countryName;
  52. # Check if the country string has a comma, split the string and only include the first element
  53. if (index($countryName, ',') != -1) {
  54. ($countryName) = split(',', $countryName);
  55. }
  56. # Loop to check valid reporting dates against current date
  57. foreach my $date (@reportingDates){
  58. if($curYear >= $date) {
  59. $validDate = join(':', $date,$date);
  60. }
  61. }
  62. # Ensure variables are defined before returning a result
  63. return unless (defined $countryCode and defined $validDate and defined $countryName);
  64. return uc $countryCode, $validDate, $countryName;
  65. };
  66. 1;