2015-01-20 16:22:59 -08:00
|
|
|
package DDG::Goodie::SolarSystem;
|
2015-01-20 16:54:20 -08:00
|
|
|
# ABSTRACT: Return various attributes of a object
|
2014-12-24 03:48:46 -08:00
|
|
|
|
|
|
|
use DDG::Goodie;
|
2015-01-07 04:51:58 -08:00
|
|
|
with 'DDG::GoodieRole::ImageLoader';
|
2014-12-24 03:48:46 -08:00
|
|
|
use YAML::XS qw( Load );
|
2014-12-24 04:33:24 -08:00
|
|
|
use POSIX;
|
2014-12-24 03:48:46 -08:00
|
|
|
|
2015-01-20 16:22:59 -08:00
|
|
|
zci answer_type => "solarsystem";
|
2014-12-24 03:48:46 -08:00
|
|
|
zci is_cached => 1;
|
|
|
|
|
2015-01-20 16:22:59 -08:00
|
|
|
name "SolarSystem";
|
2014-12-24 03:48:46 -08:00
|
|
|
primary_example_queries 'size of venus';
|
|
|
|
secondary_example_queries 'what is the size of venus', 'volume of venus';
|
2015-01-20 16:54:20 -08:00
|
|
|
description 'Lookup various object attributes';
|
|
|
|
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/objects.pm";
|
2015-01-15 22:14:10 -08:00
|
|
|
category 'random';
|
2014-12-24 03:48:46 -08:00
|
|
|
topics 'special_interest';
|
2015-01-03 17:39:37 -08:00
|
|
|
attribution github => ["MrChrisW", "Chris Wilson"],
|
|
|
|
web => ["http://chrisjwilson.com", "Chris Wilson"];
|
2014-12-24 03:48:46 -08:00
|
|
|
|
2015-03-22 01:12:42 -07:00
|
|
|
my @triggers = ( 'earth', 'jupiter', 'mars', 'mercury', 'neptune', 'saturn', 'uranus', 'venus', 'pluto', 'sun', 'moon');
|
2015-01-15 22:14:10 -08:00
|
|
|
|
2015-01-20 01:27:27 -08:00
|
|
|
my @attributesArray = ( 'size', 'radius', 'volume', 'mass', 'surface area', 'area');
|
|
|
|
|
2014-12-24 04:33:24 -08:00
|
|
|
triggers any => @triggers;
|
|
|
|
|
2015-01-20 16:54:20 -08:00
|
|
|
# Load object data
|
|
|
|
my $objects = Load(scalar share('objects.yml')->slurp);
|
2014-12-24 03:48:46 -08:00
|
|
|
|
|
|
|
# Handle statement
|
2014-12-24 04:33:24 -08:00
|
|
|
handle query_lc => sub {
|
2015-01-15 22:14:10 -08:00
|
|
|
# Declare vars
|
2015-03-19 05:57:51 -07:00
|
|
|
my ($attribute, $attributesString, $result, $objectObj, $image, $width, $height);
|
2015-01-15 19:45:02 -08:00
|
|
|
|
2015-01-21 19:05:29 -08:00
|
|
|
s/^what is (the)|(of)|(object)//g; # Remove common words, strip question marks
|
2015-01-15 22:14:10 -08:00
|
|
|
|
|
|
|
$attributesString = join('|', @attributesArray);
|
|
|
|
return unless /$attributesString/; # Ensure we match at least one attribute, eg. size, volume
|
|
|
|
|
|
|
|
# Switch attribute depending on search query
|
2015-01-15 19:45:02 -08:00
|
|
|
if(m/size|radius/) {$attribute = "radius"}
|
|
|
|
elsif(m/volume/) {$attribute = "volume"}
|
|
|
|
elsif(m/mass/) {$attribute = "mass"}
|
|
|
|
elsif(m/area/) {$attribute = "surface_area"}
|
2014-12-24 03:48:46 -08:00
|
|
|
|
2015-01-15 22:14:10 -08:00
|
|
|
s/$attributesString//g; # Remove attributes
|
|
|
|
s/^\s+|\s+$//g; # Trim
|
2014-12-24 03:48:46 -08:00
|
|
|
|
2015-01-15 22:14:10 -08:00
|
|
|
return unless $_; # Return if empty query
|
2015-01-20 16:54:20 -08:00
|
|
|
$objectObj = $objects->{$_}; # Get object data
|
|
|
|
return unless $objectObj; # Return if we don't have a valid object
|
2014-12-24 03:48:46 -08:00
|
|
|
|
2015-01-15 22:14:10 -08:00
|
|
|
# Switch to imperial for non-metric countries
|
|
|
|
# https://en.wikipedia.org/wiki/Metrication
|
2015-02-06 01:19:32 -08:00
|
|
|
if ($loc->country_code =~ m/US|MM|LR/i) {
|
2015-01-20 16:54:20 -08:00
|
|
|
$result = $objectObj->{$attribute."_imperial"};
|
2015-01-03 17:39:37 -08:00
|
|
|
} else {
|
2015-01-20 16:54:20 -08:00
|
|
|
$result = $objectObj->{$attribute};
|
2015-01-03 17:39:37 -08:00
|
|
|
}
|
2015-01-15 19:45:02 -08:00
|
|
|
|
2015-01-15 22:14:10 -08:00
|
|
|
# Convert flag surface_area = Surface Area
|
2015-01-15 19:45:02 -08:00
|
|
|
if($attribute =~ /_/ ) { $attribute = join ' ', map ucfirst lc, split /[_]+/, $attribute; }
|
2015-01-07 04:51:58 -08:00
|
|
|
|
2015-01-20 16:54:20 -08:00
|
|
|
# Human friendly object name + attribute
|
2015-02-06 01:19:32 -08:00
|
|
|
my $operation = ucfirst($_)." - ".ucfirst($attribute);
|
2015-01-07 18:43:34 -08:00
|
|
|
|
2015-01-15 22:14:10 -08:00
|
|
|
# Superscript for km3, mi3, km2 or mi2
|
2015-01-07 18:43:34 -08:00
|
|
|
if($result =~ m/(km|mi)(\d)/) {
|
2015-03-26 09:26:02 -07:00
|
|
|
my $symbol = $1; my $superscript = $2;
|
|
|
|
$result =~ s/$symbol$superscript/$symbol<sup>$superscript<\/sup>/;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Superscript for scientific notation
|
|
|
|
# Convert x to HTML entity ×
|
|
|
|
if($result =~ m/x\s(10)(\d\d)/) {
|
|
|
|
my $number = $1; my $exponent = $2;
|
|
|
|
$result =~ s/$number$exponent/$number<sup>$exponent<\/sup>/;
|
|
|
|
$result =~ s/x/×/;
|
2015-01-07 04:51:58 -08:00
|
|
|
}
|
|
|
|
|
2015-03-19 05:57:51 -07:00
|
|
|
#Change width for saturn image
|
2015-03-26 09:40:41 -07:00
|
|
|
#Ensure we have a vaild image
|
2015-03-19 05:57:51 -07:00
|
|
|
$width = ($_ eq "saturn") ? 45 : 40;
|
|
|
|
$height = ($_ eq "saturn") ? 28 : 40;
|
|
|
|
$image = goodie_img_tag({filename=>"/img/".$_.".png", height => $height, width => $width});
|
2015-02-06 01:19:32 -08:00
|
|
|
return unless $image;
|
2015-01-20 16:54:20 -08:00
|
|
|
|
2015-01-20 01:27:27 -08:00
|
|
|
#Return result and html
|
2015-02-06 01:19:32 -08:00
|
|
|
return $operation." is ".$result, pretty_output($result, $operation, $image);
|
2015-01-07 18:43:34 -08:00
|
|
|
};
|
|
|
|
|
2015-03-19 05:57:51 -07:00
|
|
|
|
2015-01-15 19:45:02 -08:00
|
|
|
#Build HTML output
|
2015-01-07 18:43:34 -08:00
|
|
|
sub pretty_output {
|
|
|
|
my ($result, $operation, $image) = @_;
|
2015-01-20 16:54:20 -08:00
|
|
|
my $html = "<div class=\"zci--objects\">";
|
|
|
|
$html .= "<span class=\"objects--objectImage\">";
|
2015-01-07 18:43:34 -08:00
|
|
|
$html .= $image;
|
2015-01-07 04:51:58 -08:00
|
|
|
$html .= "</span>";
|
2015-01-20 16:54:20 -08:00
|
|
|
$html .= "<span class=\"objects--info\">";
|
|
|
|
$html .= "<span class=\"text--primary objects--objectAttribute\">";
|
2015-01-07 04:51:58 -08:00
|
|
|
$html .= $result;
|
|
|
|
$html .= "</span>";
|
2015-01-20 16:54:20 -08:00
|
|
|
$html .= "<span class=\"text--secondary objects--objectName\">";
|
2015-01-07 18:43:34 -08:00
|
|
|
$html .= $operation;
|
2015-01-07 04:51:58 -08:00
|
|
|
$html .= "</span>";
|
|
|
|
$html .= "</span>";
|
|
|
|
$html .= "</div>";
|
|
|
|
return (html => $html);
|
2015-01-07 18:43:34 -08:00
|
|
|
}
|
2015-01-15 22:14:10 -08:00
|
|
|
1;
|