Created
June 22, 2014 02:52
-
-
Save swuecho/2a0445ccfeb3c5d84518 to your computer and use it in GitHub Desktop.
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
use v5.12; | |
use IO::Scalar; | |
use Encode qw/decode/; | |
use LWP::Simple; | |
use Mojo::DOM; | |
use Email::Stuffer; | |
my $data; | |
## Open a handle on a string, and append to it: | |
my $SH = new IO::Scalar \$data; | |
my $host = "http://www.mitbbs.com"; | |
my $selector = 'td[width="726"] tr'; # CSS selector | |
my $encoding = "euc-cn"; | |
parse_bankuai( $_, $encoding, $selector ) for qw(Programming JobHunting); | |
# Create and send the email | |
# configure ssmtp | |
# http://www.havetheknowhow.com/Configure-the-server/Install-ssmtp.html | |
Email::Stuffer->from('echowuhao@gmail.com')->to('echowuhao@gmail.com') | |
->subject('MITBBS Daily')->html_body($data)->send; | |
sub parse_bankuai { | |
my ( $bankuai, $encoding, $selector ) = @_; | |
my $url = "$host/bbsdoc/$bankuai.html"; | |
my $utf8_content = fetch_content( $url, $encoding ); | |
my @trs = Mojo::DOM->new($utf8_content)->find($selector)->each; | |
$SH->print("<h2> $bankuai </h2>"); | |
# throw away the table title lines; | |
shift @trs; | |
for (@trs) { | |
my @td = $_->children->each; | |
next if @td < 6; # only keep the line have 6 children | |
extract_tds(@td); | |
} | |
} | |
sub fetch_content { | |
my ( $url, $encoding ) = @_; | |
my $raw_content = get($url) or die "no result"; | |
return decode( $encoding, $raw_content ); | |
} | |
sub extract_tds { | |
my @c = @_; | |
my $day = (localtime)[3]; | |
my $bbs_day; | |
if ( $c[3]->text > 5 | |
and ( ($bbs_day) = $c[5]->at('span')->text =~ /-(\d+)/ ) | |
and $bbs_day >= $day ) | |
{ | |
#sequence number | |
# say $c[0]->text; | |
# url | |
my $abs_url = $host . $c[2]->at('a')->attr('href'); | |
my $title = $c[2]->at('a')->text; | |
# link | |
my $title_html = qq|<a href="$abs_url"> $title </a>|; | |
$SH->print($title_html); | |
$SH->print("<br>"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment