Skip to content

Instantly share code, notes, and snippets.

@loganom
Last active January 2, 2016 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganom/8223161 to your computer and use it in GitHub Desktop.
Save loganom/8223161 to your computer and use it in GitHub Desktop.
Relevant snippet of TodayInHistory.pm
#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