This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 941
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1452 from Akryum/master
Hearthstone Spice debut
- Loading branch information
Showing
5 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package DDG::Spice::Hearthstone; | ||
# ABSTRACT: Retrieve the card infos from Hearthstone game. | ||
|
||
use DDG::Spice; | ||
|
||
# Caching | ||
spice is_cached => 1; | ||
spice proxy_cache_valid => "200 4d"; | ||
|
||
# Metadata | ||
name "Hearthstone Card Search"; | ||
source "Bytevortex (Gamepedia Proxy)"; | ||
icon_url "http://eu.battle.net/hearthstone/static/images/icons/favicon.ico"; | ||
description "Get a preview of any Hearthstone card."; | ||
primary_example_queries "hearthstone leeroy jenkins", "hearthstone leeroy", "leeroy hearthstone", "hearthstone mirror"; | ||
category "entertainment"; | ||
topics "gaming"; | ||
code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Hearthstone.pm"; | ||
attribution github => ["https://github.com/Akryum", "Akryum"], | ||
twitter => "Akryum"; | ||
|
||
# Triggers | ||
triggers startend => "hearthstone"; | ||
|
||
# Target URL | ||
spice to => 'http://bytevortex.net/hearthstone.php?search=$1'; | ||
|
||
# JS Callback | ||
spice wrap_jsonp_callback => 1; | ||
|
||
# Keyword blacklist | ||
my $keyword_guard = qr/game|instruction|stoves|warcraft|deck|forum|wiki|reddit/; | ||
|
||
# Handle statement | ||
handle remainder => sub { | ||
|
||
# Guard against "no answer" | ||
return unless $_; | ||
|
||
# Keyword Blacklist | ||
return if (/$keyword_guard/); | ||
|
||
# Regex guard | ||
# Allows string similar to any Hearthstone card name: multiple words separated by spaces, commas or dashes. | ||
# The card name may also have dots, double dots, slashes or an exclamation mark (Those Blizzard devs). | ||
return $_ if (/^([a-z0-9':!\/,.-]+\s*)+$/i); | ||
|
||
return; | ||
}; | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.zci--hearthstone b { | ||
font-weight: normal; | ||
} | ||
|
||
.zci--hearthstone i { | ||
font-style: normal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div class="zci--hearthstone"> | ||
{{#if card_description}} | ||
<div class="zci--hearthstone--description">{{{card_description}}}</div> | ||
{{/if}} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
(function(env) { | ||
"use strict"; | ||
|
||
env.ddg_spice_hearthstone = function(api_result) { | ||
|
||
// Validate the response | ||
if(!api_result || api_result.error || !api_result.Has_name || !api_result.page) { | ||
return Spice.failed('hearthstone'); | ||
} | ||
|
||
// Infobox Labels | ||
var infoboxItems = { | ||
Has_card_type: "Type", | ||
Has_class: "Playable by", | ||
Has_mana_cost: { | ||
label: "Cost", | ||
icon: "http://hydra-media.cursecdn.com/hearthstone.gamepedia.com/8/8b/Mana_icon.png" | ||
}, | ||
Has_attack: { | ||
label: "Attack", | ||
icon: "http://hydra-media.cursecdn.com/hearthstone.gamepedia.com/f/fe/Attack_icon.png" | ||
}, | ||
Has_health: { | ||
label: "Health", | ||
icon: "http://hydra-media.cursecdn.com/hearthstone.gamepedia.com/1/17/Health_icon.png" | ||
}, | ||
Has_rarity: "Rarity", | ||
Is_part_of_set: "Set", | ||
Is_collectible: "Collectible" | ||
}; | ||
|
||
// Render the response | ||
Spice.add({ | ||
id: "hearthstone", | ||
name: "Hearthstone", | ||
data: api_result, | ||
meta: { | ||
sourceName: "Hearthstone Gamepedia Wiki", | ||
sourceUrl: "http://hearthstone.gamepedia.com/" + api_result.page | ||
}, | ||
normalize: function(item) { | ||
|
||
// Infobox Heading | ||
var infoboxData = [{ | ||
heading: 'Card Details' | ||
}]; | ||
|
||
// Is collectible | ||
api_result.Is_collectible = (api_result.Is_collectible === "t" ? "Yes" : "No"); | ||
|
||
// Hero | ||
if(api_result.Has_class === "Any") { | ||
api_result.Has_class = "Any Hero"; | ||
} | ||
|
||
// InfoBox data | ||
$.each(infoboxItems, function(key, data) { | ||
var label, | ||
value; | ||
if(api_result[key]) { | ||
value = api_result[key]; | ||
if(data.label) { | ||
label = data.label; | ||
} else { | ||
label = data; | ||
} | ||
infoboxData.push({ | ||
label: label, | ||
value: value | ||
}); | ||
} | ||
}); | ||
|
||
// Normalized data | ||
var card = { | ||
title: item.Has_name, | ||
url: "http://hearthstone.gamepedia.com/" + item.page, | ||
infoboxData: infoboxData | ||
}; | ||
|
||
// Optionnal fields | ||
// Image | ||
if(item.image_url) { | ||
card.image = item.image_url; | ||
} | ||
|
||
// Description | ||
if(item.Has_description) { | ||
card.card_description = item.Has_description; | ||
} | ||
|
||
// Flavor text | ||
if(item.Has_flavor_text) { | ||
card.subtitle = item.Has_flavor_text; | ||
} | ||
|
||
return card; | ||
}, | ||
templates: { | ||
group: 'info', | ||
options: { | ||
content: Spice.hearthstone.hearthstone | ||
} | ||
} | ||
}); | ||
}; | ||
}(this)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use DDG::Test::Spice; | ||
|
||
spice is_cached => 1; | ||
|
||
ddg_spice_test( | ||
[qw( DDG::Spice::Hearthstone)], | ||
'hearthstone leeroy jenkins' => test_spice( | ||
'/js/spice/hearthstone/leeroy%20jenkins', | ||
call_type => 'include', | ||
caller => 'DDG::Spice::Hearthstone' | ||
), | ||
'hearthstone leeroy' => test_spice( | ||
'/js/spice/hearthstone/leeroy', | ||
call_type => 'include', | ||
caller => 'DDG::Spice::Hearthstone' | ||
), | ||
'leeroy hearthstone' => test_spice( | ||
'/js/spice/hearthstone/leeroy', | ||
call_type => 'include', | ||
caller => 'DDG::Spice::Hearthstone' | ||
), | ||
'hearthstone mirror' => test_spice( | ||
'/js/spice/hearthstone/mirror', | ||
call_type => 'include', | ||
caller => 'DDG::Spice::Hearthstone' | ||
), | ||
'hearthstone' => undef, | ||
'hearthstone heroes of warcraft' => undef, | ||
'hearthstone wood stoves' => undef, | ||
'hearthstone mage deck' => undef, | ||
'hearthstone tide_caller' => undef, | ||
'hearthstone tidecaller $' => undef | ||
); | ||
|
||
done_testing; |