Last active
January 2, 2016 00:29
-
-
Save loganom/8223161 to your computer and use it in GitHub Desktop.
Relevant snippet of TodayInHistory.pm
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
#EXCERPT FROM TodayInHistory.pm | |
my $uri = "http://history.com/this-day-in-history/"; | |
my $scraper = scraper { | |
process "div.article.copy.four-under-grey > p", lead => 'TEXT'; | |
process "div.mod.also-today > dl > dd", "list[]" => scraper { | |
process 'dd', body => 'TEXT'; | |
process 'dd > a', link => '@href'; | |
} | |
}; | |
my $res = $scraper->scrape(URI->new("$uri")); | |
#generate random number to select a random three stories from hash | |
my $rand = int( rand(19) + 1); | |
my $count = 0; | |
#initialize ouput | |
my $output = ""; | |
for my $list (@{$res->{list}}) { | |
if($count == 0){ | |
#append lead story | |
$output = $output . "$list->{body}<br/>"; | |
$output = $output . "$res->{lead} <a href=\"$uri\">read more</a><br/><br/>"; | |
}elsif(abs($count - $rand) <= 1){ | |
#append random story | |
if (exists $list->{link}) { | |
#edge case: do not print the lead story twice | |
#(it is the first in hash and has no link) | |
$output = $output . "<a href=\"$list->{link}\">$list->{body}</a><br/>"; | |
} | |
} | |
$count++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment