BPFactiodBot/Source
From Beachapedia
< User:BPFactiodBot
This is the source code for BPFactiodBot. Passwords/API keys have been redacted for obvious reasons.
#!/usr/bin/perl # Factoid Bot # By Chris Wilson # gwsuperfan [at] gmail.com # use MediaWiki::Bot; use Net::Twitter::Lite; use utf8; use Text::Unidecode; use MIME::Lite::TT::HTML; use HTML::FormatText::WithLinks; my $bot = MediaWiki::Bot->new({ protocol => 'http', host => 'www.beachapedia.org', path => '', }); $bot->login({ username => "BPFactiodBot", password => '**REDACTED**', }) or die "Couldn't connect to wiki!\n"; sub zeroFill { my($temp) = shift; my($len) = shift; my($diff) = $len - length($temp); return($temp) if $diff <= 0; return(('0' x $diff) . $temp); } ($sec,$min,$hour,$mday,$month,$year,@junk) = localtime(time); $year = $year + 1900; $month = $month + 1; $mday = zeroFill($mday,2); $month = zeroFill($month,2); $date = join('/', $year, $month, $mday); my $pageid = $bot->get_id("Beachapedia:Factoid/$date"); my $tinyURL = "http://tinyurl.com/695e9ct/$date"; if ($pageid) { $factoidPageText = $bot->get_text("Beachapedia:Factoid/$date"); (@pageLines) = split /\|/, $factoidPageText; foreach $line (@pageLines) { if ($line =~ m/Factoid Headline/) { ($junk, $factoidHeadline) = split /=/, $line; chomp($factoidHeadline); } elsif ($line =~ m/Emailed\/Tweeted/) { ($junk, $factoidStatus) = split /=/, $line; chomp($factoidStatus); $factoidStatus =~ s/\}//g; $factoidStatus =~ s/\n//g; } elsif ($line =~ m/Factoid Text/) { ($junk, $factoidText) = split /=/, $line; chomp($factoidText) } } if (length($factoidHeadline) > 85) { $TfactoidHeadline = substr($factoidHeadline, 0, 82) . "..."; } else { $TfactoidHeadline = $factoidHeadline; } $tweet = "Today's Factoid: $TfactoidHeadline $tinyURL"; } if ($TfactoidHeadline) { unless ($factoidStatus =~ m/Yes/) { my $nt = Net::Twitter::Lite->new( consumer_key => '**REDACTED**', consumer_secret => '**REDACTED**', access_token => '**REDACTED**', access_token_secret => '**REDACTED**' ); $nt->update("$tweet"); my $estwitter = Net::Twitter::Lite->new( consumer_key => '**REDACTED**', consumer_secret => '**REDACTED**', access_token => '**REDACTED**', access_token_secret => '**REDACTED**' ); $estwitter->update("$tweet"); $factoidPageText =~ s/Emailed\/Tweeted=No/Emailed\/Tweeted=Yes/; $bot->edit({ page => "Beachapedia:Factoid/$date", text => $factoidPageText, summary => 'Bot has tweeted the factoid' }); emailFactoid(); } } $bot->logout(); sub emailFactoid() { my $f = HTML::FormatText::WithLinks->new(); @factoidTextWords = split / /, $factoidText; foreach $word (@factoidTextWords) { if ($word =~ m/\[http:\/\//) { $word =~ s/\[/<a href="/; $word .= '">'; $insidelink = 1; } if ($word =~ m/\]/) { if ($insidelink == 1) { $word =~ s/\]/<\/a>/; $insidelink = 0; } } } $factoidTextHTML = join(" ", @factoidTextWords); $factoidTextHTML =~ s/"> /">/g; unidecode($factoidTextHTML); my %options; $options{INCLUDE_PATH} = '/var/www/vhosts/beachapedia.org/bin/MailTemplates'; my %params; $params{factoid_date} = $date; $params{factoid_headline} = $factoidHeadline; $params{factoid_html} = $factoidTextHTML; $params{factoid_text} = $f->parse($factoidTextHTML); my $msg = MIME::Lite::TT::HTML->new( From => '**FROM_ADDRESS**', To => '**ACTIVISTS_LIST**'; Subject => "Coastal Factoid for $date", Template => { text => 'factoid.txt.tt', html => 'factoid.html.tt', }, TmplOptions => \%options, TmplParams => \%params, ); $msg->send; }