Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created November 6, 2010 06:34
Show Gist options
  • Save unixpickle/665239 to your computer and use it in GitHub Desktop.
Save unixpickle/665239 to your computer and use it in GitHub Desktop.
another short perl script for viewing mac rumors
#!/usr/bin/perl
use LWP::UserAgent;
use strict;
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;
my $cn = 0;
$_ = $contents;
while (s/(\<h3\>(.|\n|\r\n)*?\<p class="storycomments"\>)//i) {
my %arr = &rumorinfo($1 . $2 . $3);
$articles{$cn}{"title"} = $arr{"title"};
$articles{$cn}{"story"} = $arr{"story"};
$cn++;
my $s = "" . $cn . ")";
for (my $j = length($s); $j < 5; $j++) {
$s .= " ";
}
print $s . $articles{$cn - 1}{"title"} . "\n";
}
print "Enter article to view: ";
my $mystring = <STDIN>;
chomp $mystring;
print "*" . $articles{$mystring - 1}{"title"} . "\n" . $articles{$mystring - 1}{"story"} . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment