PageRenderTime 51ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Goodie/Minecraft.pm

https://github.com/zoracon/zeroclickinfo-goodies
Perl | 76 lines | 57 code | 12 blank | 7 comment | 3 complexity | 6957da259992ea89044c17112fd590fc MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Goodie::Minecraft;
  2. # ABSTRACT: Minecraft crafting recipes.
  3. use strict;
  4. use DDG::Goodie;
  5. use JSON;
  6. zci answer_type => 'minecraft';
  7. zci is_cached => 1;
  8. primary_example_queries 'cake minecraft';
  9. secondary_example_queries 'how do i craft a cake in minecraft';
  10. name 'Minecraft';
  11. description 'Minecraft crafting recipes.';
  12. source 'http://thejool.com/api/crafting-guide.json';
  13. category 'special';
  14. topics 'words_and_games';
  15. code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Minecraft.pm';
  16. attribution
  17. web => ['http://engvik.nu', 'Lars Jansøn Engvik'],
  18. github => [ 'larseng', 'Lars Jansøn Engvik'];
  19. triggers startend => "minecraft";
  20. # Fetch and store recipes in a hash.
  21. my $json = share('crafting-guide.json')->slurp;
  22. my $decoded = decode_json($json);
  23. my %recipes = map{ lc $_->{'name'} => $_ } (@{ $decoded->{'items'} });
  24. # Good words: All the words that recipe names consist of.
  25. # Okay words: Words that are in the good words list, but also could be a part of the query.
  26. # Bad words: Words related to Minecraft, but not related to recipes.
  27. my %good_words = map { $_ => 1 } map { split /\s+/ } (keys %recipes);
  28. my %okay_words = map { $_ => 1 } (qw(a crafting));
  29. my %bad_words = map { $_ => 1 } (qw(download server tutorial mod mods skins skin texture pack packs project projects));
  30. handle remainder => sub {
  31. my @query = split /\s+/, lc $_; # Split on whitespaces.
  32. my @lookup;
  33. # Loop through the query.
  34. foreach (@query) {
  35. return if(exists($bad_words{$_})); # Not looking for a recipe.
  36. push (@lookup, $_) if(exists($good_words{$_})); # Word exists in a recipe, add it.
  37. }
  38. my $recipe = $recipes{join(' ', @lookup)} || $recipes{join(' ', grep { !$okay_words{$_} } @lookup)};
  39. return unless $recipe;
  40. # Recipe found, let's return an answer.
  41. my $plaintext = 'Minecraft ' . $recipe->{'name'} . ' ingredients: ' . $recipe->{'ingredients'} . '.';
  42. return $plaintext,
  43. structured_answer => {
  44. id => 'minecraft',
  45. name => 'Minecraft',
  46. data => {
  47. title => $recipe->{'name'},
  48. subtitle => "Ingredients: " . $recipe->{'ingredients'},
  49. description => $recipe->{'description'},
  50. image => 'https://duckduckgo.com/iu/?u=' . uri_esc( $recipe->{'image'} )
  51. },
  52. meta => {
  53. sourceName => "Minecraft Wiki",
  54. sourceUrl => "http://minecraft.gamepedia.com/Crafting#Complete_recipe_list"
  55. },
  56. templates => {
  57. group => 'info',
  58. options => {
  59. moreAt => 1
  60. }
  61. }
  62. };
  63. };
  64. 1;