PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Congress.pm

https://github.com/aembleton/zeroclickinfo-spice
Perl | 169 lines | 148 code | 17 blank | 4 comment | 4 complexity | 9b74187e4eaffb7802c8399722d68adf MD5 | raw file
  1. package DDG::Spice::Congress;
  2. # ABSTRACT: Return current Congress members for given state.
  3. use DDG::Spice;
  4. primary_example_queries "new york senators";
  5. secondary_example_queries "florida representatives", "house california";
  6. description "Shows congress by state";
  7. name "Congress";
  8. icon_url "/i/topics.nytimes.com.ico";
  9. source "The New York Times";
  10. code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Congress.pm";
  11. topics "special_interest", "trivia";
  12. category "facts";
  13. attribution web => ['http://kevinschaul.com','Kevin Schaul'],
  14. email => ['kevin.schaul@gmail.com','Kevin Schaul'];
  15. spice to => 'http://api.nytimes.com/svc/politics/v3/us/legislative/congress/112/$1/members.json?state=$2&api-key={{ENV{DDG_SPICE_CONGRESS_APIKEY}}}';
  16. spice from => '([^/]+)/?(?:([^/]+)/?(?:([^/]+)|)|)';
  17. spice wrap_jsonp_callback => 1;
  18. triggers startend => 'senate', 'senators', 'representatives', 'reps', 'house';
  19. sub rtrim($);
  20. handle query_lc => sub {
  21. my (%states) = (
  22. 'alabama' => 'al',
  23. 'al' => 'al',
  24. 'alaska' => 'ak',
  25. 'ak' => 'ak',
  26. 'arizona' => 'az',
  27. 'az' => 'az',
  28. 'arkansas' => 'ar',
  29. 'ar' => 'ar',
  30. 'california' => 'ca',
  31. 'ca' => 'ca',
  32. 'colorado' => 'co',
  33. 'co' => 'co',
  34. 'connecticut' => 'ct',
  35. 'ct' => 'ct',
  36. 'delaware' => 'de',
  37. 'de' => 'de',
  38. 'florida' => 'fl',
  39. 'fl' => 'fl',
  40. 'georgia' => 'ga',
  41. 'ga' => 'ga',
  42. 'hawaii' => 'hi',
  43. 'hi' => 'hi',
  44. 'idaho' => 'id',
  45. 'id' => 'id',
  46. 'illinois' => 'il',
  47. 'il' => 'il',
  48. 'indiana' => 'in',
  49. 'in' => 'in',
  50. 'iowa' => 'ia',
  51. 'ia' => 'ia',
  52. 'kansas' => 'ks',
  53. 'ks' => 'ks',
  54. 'kentucky' => 'ky',
  55. 'ky' => 'ky',
  56. 'louisiana' => 'la',
  57. 'la' => 'la',
  58. 'maine' => 'me',
  59. 'me' => 'me',
  60. 'maryland' => 'md',
  61. 'md' => 'md',
  62. 'massachusetts' => 'ma',
  63. 'ma' => 'ma',
  64. 'michigan' => 'mi',
  65. 'mi' => 'mi',
  66. 'minnesota' => 'mn',
  67. 'mn' => 'mn',
  68. 'mississippi' => 'ms',
  69. 'ms' => 'ms',
  70. 'missouri' => 'mo',
  71. 'mo' => 'mo',
  72. 'montana' => 'mt',
  73. 'mt' => 'mt',
  74. 'nebraska' => 'ne',
  75. 'ne' => 'ne',
  76. 'nevada' => 'nv',
  77. 'nv' => 'nv',
  78. 'new hampshire' => 'nh',
  79. 'nh' => 'nh',
  80. 'new jersey' => 'nj',
  81. 'nj' => 'nj',
  82. 'new mexico' => 'nm',
  83. 'nm' => 'nm',
  84. 'new york' => 'ny',
  85. 'ny' => 'ny',
  86. 'north carolina' => 'nc',
  87. 'nc' => 'nc',
  88. 'north dakota' => 'nd',
  89. 'nd' => 'nd',
  90. 'ohio' => 'oh',
  91. 'oh' => 'oh',
  92. 'oklahoma' => 'ok',
  93. 'ok' => 'ok',
  94. 'oregon' => 'or',
  95. 'or' => 'or',
  96. 'pennsylvania' => 'pa',
  97. 'pa' => 'pa',
  98. 'rhode island' => 'ri',
  99. 'ri' => 'ri',
  100. 'south carolina' => 'sc',
  101. 'sc' => 'sc',
  102. 'south dakota' => 'sd',
  103. 'sd' => 'sd',
  104. 'tennessee' => 'tn',
  105. 'tn' => 'tn',
  106. 'texas' => 'tx',
  107. 'tx' => 'tx',
  108. 'utah' => 'ut',
  109. 'ut' => 'ut',
  110. 'vermont' => 'vt',
  111. 'vt' => 'vt',
  112. 'virginia' => 'va',
  113. 'va' => 'va',
  114. 'washington' => 'wa',
  115. 'wa' => 'wa',
  116. 'west virginia' => 'wv',
  117. 'wv' => 'wv',
  118. 'wisconsin' => 'wi',
  119. 'wi' => 'wi',
  120. 'wyoming' => 'wy',
  121. 'wy' => 'wy'
  122. );
  123. # Matches against special variable `$_`, which contains query
  124. my ($state1, $chamber, $state2) = /^(.*)(?:\s)*(senate|senators|representatives|reps|house)(?:\s)*(.*)$/g;
  125. my ($state);
  126. # Regex returns a space after $1, so we must remove it before hashing
  127. $state1 = rtrim($state1);
  128. if (exists $states{$state1}) {
  129. $state = $states{$state1};
  130. } elsif (exists $states{$state2}) {
  131. $state = $states{$state2};
  132. } else {
  133. return;
  134. }
  135. my (%chambers) = (
  136. "senators" => "senate",
  137. "senate" => "senate",
  138. "reps" => "house",
  139. "representatives" => "house",
  140. "house" => "house"
  141. );
  142. $chamber = $chambers{$chamber};
  143. return $chamber, $state if ($chamber && $state);
  144. return;
  145. };
  146. 1;
  147. sub rtrim($) {
  148. # Removes trailing whitespace
  149. my $string = shift;
  150. $string =~ s/\s+$//;
  151. return $string;
  152. }