PageRenderTime 2ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/DDG/Spice/Quixey.pm

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