PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Quixey.pm

https://github.com/robertbrook/zeroclickinfo-spice
Perl | 115 lines | 83 code | 20 blank | 12 comment | 11 complexity | 2c9257a25af35825bad3a6f0b55d510c MD5 | raw file
  1. package DDG::Spice::Quixey;
  2. use DDG::Spice;
  3. use JSON;
  4. use String::Trim;
  5. use List::Uniq ':all';
  6. primary_example_queries "flight tracking app", "quixey angry birds";
  7. secondary_example_queries "free calculator app", "tiny piano for iphone";
  8. description "Search for mobile apps";
  9. name "Quixey App Search";
  10. source "Quixey";
  11. code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Quixey.pm";
  12. icon_url "/i/www.quixey.com.ico";
  13. category "entertainment";
  14. topics "everyday", "special_interest";
  15. attribution github => ['https://github.com/duckduckgo', 'DuckDuckGo'],
  16. twitter => ['http://twitter.com/duckduckgo', 'DuckDuckGo'];
  17. # Variable Definitions
  18. my %custom_ids = (2005 => 75675980, 2004 => 78989893);
  19. my %platform_ids = (
  20. "android" => 2005,
  21. "droid" => 2005,
  22. "google play store" => 2005,
  23. "google play" => 2005,
  24. "windows phone 8" => 8556073,
  25. "windows phone" => 8556073,
  26. "windows mobile" => 8556073,
  27. "playbook" => 2008,
  28. "blackberry" => 2008,
  29. "apple app store" => 2004,
  30. "apple app" => 2004,
  31. "ios" => 2004,
  32. "ipod touch" => 2004,
  33. "ipod" => 2004,
  34. "iphone" => 2004,
  35. "ipad" => 2015,
  36. );
  37. my @triggers = keys %platform_ids;
  38. my @extraTriggers = qw(quixey app apps);
  39. push(@triggers, @extraTriggers);
  40. triggers any => @triggers;
  41. spice from => '([^/]+)/([^/]+)/?([^/]+)?/?([^/]+)?';
  42. spice to => 'https://api.quixey.com/1.0/search?partner_id=2073823582&partner_secret={{ENV{DDG_SPICE_QUIXEY_APIKEY}}}&q=$1&platform_ids=$2&max_cents=$3&custom_id=$4&limit=50&skip=0&format=json&callback={{callback}}';
  43. spice proxy_ssl_session_reuse => "off";
  44. handle query_parts => sub {
  45. my $full_query = join(" ", @_);
  46. my $restriction;
  47. my $max_price = 999999;
  48. # set price to $0 if "free" is in the query
  49. $max_price = 0 if ($full_query =~ s/\bfree\b//ig);
  50. # check if device mentioned, if so verify app search intent
  51. if ($full_query =~ qr/\b(iphone|ipad|ipod|ios|blackberry|playbook|android)\b/) {
  52. my $device = $1;
  53. return unless ($full_query =~ qr/\b(?:on|for)\s+$device/i or $full_query =~ qr/\b(apps?|quixey)\b/i );
  54. $full_query =~ s/\b(on|for)\s+$device/ $device/gi;
  55. }
  56. # check for platform specific trigger in query
  57. # if found remove from query
  58. # Note: sort trigger list to catch longer phrase first (eg "ipod touch" vs "ipod")
  59. my @matches = grep { $full_query =~ /\b$_\b/ig } sort { length($a) <=> length($b) } keys %platform_ids;
  60. if (length scalar @matches){
  61. my @sorted_matches = sort { length($b) <=> length($a) } @matches;
  62. foreach my $match (@sorted_matches){
  63. $full_query =~ s/\b$match\b//ig;
  64. $restriction = $platform_ids{ $match };
  65. }
  66. }
  67. # check for and strip extra triggers and whitespace
  68. # if nothing remains query was just trigger words
  69. # so return nothing
  70. $full_query =~ s/\b$_\b//ig foreach (@extraTriggers);
  71. $full_query =~ s/\s+/ /ig;
  72. $full_query = trim $full_query;
  73. return unless (length $full_query);
  74. my @platforms;
  75. my $platforms_encoded;
  76. # if platform restiction(s) detected
  77. # return query, specify proper ids for API
  78. if (defined $restriction) {
  79. push @platforms, $restriction;
  80. $platforms_encoded = encode_json \@platforms;
  81. if ($restriction == 2005 or $restriction == 2004) {
  82. return $full_query, $platforms_encoded, $max_price, $custom_ids{ $restriction };
  83. } else {
  84. return $full_query, $platforms_encoded, $max_price, "2414062669";
  85. }
  86. } else {
  87. my @full_platforms = uniq({sort => 1}, values %platform_ids);
  88. # need to recast as int because uniq and sort convert to string
  89. push @platforms, int($_) foreach @full_platforms;
  90. $platforms_encoded = encode_json \@platforms;
  91. return $full_query, $platforms_encoded, $max_price, "2414062669";
  92. }
  93. return;
  94. };
  95. 1;