Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created November 6, 2010 04:19
Show Gist options
  • Save unixpickle/665187 to your computer and use it in GitHub Desktop.
Save unixpickle/665187 to your computer and use it in GitHub Desktop.
mac rumors reading app for perl
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
sub getmacrumors {
my $ua = new LWP::UserAgent;
$ua->timeout(120);
my $request = new HTTP::Request('GET', 'http://www.macrumors.com/');
my $response = $ua->request($request);
my $content = $response->content();
return $content;
}
sub rumortitle {
my $rm = $_[0];
$rm =~ s/<h3><a href=.*?>(.*?)<\/a><\/h3>//i;
$1;
}
sub rumorcontent {
my $rm = $_[0];
$rm =~ s/<div class="storybody">((.|\n|\r\n)*?)<p class="storycomments">//i;
$rm = $1;
my $article = $1;
$article =~ s/\<(.|\n|\r\n)*?\>//ig;
$article;
}
sub rumorinfo {
my $rumor = $_[0];
my %ret;
$ret{"title"} = &rumortitle($rumor);
$ret{"story"} = &rumorcontent($rumor);
%ret;
}
my $contents = &getmacrumors;
my @articles = ();
my $i = 0;
$_ = $contents;
while (s/(\<h3\>(.|\n|\r\n)*?\<p class="storycomments"\>)//i) {
push(@articles, $1 . $2. $3);
}
foreach my $str (@articles) {
my %info = &rumorinfo($str);
$_ = "" . ++$i . ")";
for (my $j = length($_); $j < 5; $j++) {
$_ .= " ";
}
print $_ . $info{"title"} . "\n";
}
print "Enter article to view: ";
my $mystring = <STDIN>;
chomp $mystring;
my %article = &rumorinfo($articles[$mystring - 1]);
print "- " . $article{"title"} . " -\n" . $article{"story"} . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment