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

/lib/DDG/Spice/Quixey.pm

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