PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/SearchCode.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 41 lines | 22 code | 9 blank | 10 comment | 4 complexity | 90f695482b99b96d6203ee0f11a08f90 MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::SearchCode;
  2. # ABSTRACT: Searches for programming code
  3. use strict;
  4. use DDG::Spice;
  5. # known bad queries
  6. my %skip_queries = map { $_ => undef } (
  7. 'sql server with',
  8. );
  9. my @triggers = share('triggers.txt')->slurp;
  10. triggers startend => @triggers;
  11. spice to => 'https://searchcode.com/api/jsonp_search_IV/?q=$1&callback={{callback}}';
  12. # use list of trigger words to create regex
  13. # and strip newline characters
  14. my $words = join "|", @triggers;
  15. $words =~ s/\n//g;
  16. handle query_raw => sub {
  17. return if exists $skip_queries{lc($_)};
  18. # don't trigger on:
  19. # app (for Quixey)
  20. # code/example (for CodeSearch)
  21. # jobs (for Github jobs)
  22. return if m/(^|\s+)(app|code|example|jobs)(\s+|$)/ig;
  23. # make sure there is more to the
  24. # query besides the trigger itself
  25. if (m/$words/i){
  26. my $query = $_;
  27. s/$words//ig;
  28. return $query if length $_ > 1;
  29. }
  30. return;
  31. };
  32. 1;