zeroclickinfo-goodies/lib/DDG/Goodie/SolarSystem.pm

129 lines
4.0 KiB
Perl
Raw Normal View History

2015-01-20 16:22:59 -08:00
package DDG::Goodie::SolarSystem;
# ABSTRACT: Return various attributes of a object
2014-12-24 03:48:46 -08:00
use DDG::Goodie;
use YAML::XS qw( Load );
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';
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';
attribution github => ["MrChrisW", "Chris Wilson"],
web => ["http://chrisjwilson.com", "Chris Wilson"];
2014-12-24 03:48:46 -08: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');
my @unitTriggers = ( 'kg', 'km2', 'km3', 'km', 'mi2', 'mi3', 'mi', 'lbs', 'metric', 'imperial');
triggers any => @triggers;
# Load object data
my $objects = Load(scalar share('objects.yml')->slurp);
2014-12-24 03:48:46 -08:00
# Handle statement
handle query_lc => sub {
2015-01-15 22:14:10 -08:00
# Declare vars
my ($attribute, $attributesString, $unitsString, $result, $objectObj, $objectName, $saturn, $unitType);
s/^what is (the)|(of)|(object)|(in)//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
$unitsString = join('|', @unitTriggers);
# Set attribute depending on search query
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
# Switch unit type based on user input
# kg, km, metric | lbs mi imperial
if(m/kg|km\d?|metric/) {
$unitType = $attribute;
} elsif (m/lbs|mi\d?|imperial/) {
$unitType = $attribute."_imperial";
}
s/$unitsString//g; # Remove unit/unit type
2015-01-15 22:14:10 -08:00
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
$objectObj = $objects->{$_}; # Get object data
return unless $objectObj; # Return if we don't have a valid object
$objectName = $_;
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
if (!$unitType)
{
if ($loc->country_code =~ m/US|MM|LR/i) {
$unitType = $attribute."_imperial";
} else {
$unitType = $attribute;
}
}
# Get correct unit type
$result = $objectObj->{$unitType};
2015-01-15 22:14:10 -08:00
# Convert flag surface_area = Surface Area
if($attribute =~ /_/ ) { $attribute = join ' ', map ucfirst lc, split /[_]+/, $attribute; }
# Human friendly object name + attribute
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)/) {
my $symbol = $1; my $superscript = $2;
$result =~ s/$symbol$superscript/$symbol<sup>$superscript<\/sup>/;
}
# Superscript for scientific notation
# Convert x to HTML entity &times;
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/&times;/;
}
#$saturn var is provided to handlebars template to set size of image
$saturn = ($objectName eq "saturn") ? 1 : 0;
2015-01-20 01:27:27 -08:00
#Return result and html
return $operation." is ".$result,
structured_answer => {
id => 'solar_system',
name => 'Answer',
data => {
attributes => $result,
operation => $operation,
imageName => $objectName,
saturn => $saturn
},
meta => {
sourceUrl => "https://solarsystem.nasa.gov/planets/index.cfm",
sourceName => "NASA"
},
templates => {
group => 'base',
detail_mobile => 'DDH.solar_system.mobile',
options => {
content => 'DDH.solar_system.content',
}
}
};
2015-01-07 18:43:34 -08:00
};
2015-01-15 22:14:10 -08:00
1;