So I was bored at work today and made a pointless perl script. It was fun though =). It fetches the last post of a given user from these forums and predicts how interesting it will be. It predicts the potential interest by factors such as caps, swearing, and christian influence. The user's UID must be added to a database (which is in the url when you view someone's profile). The database file is expected to be uids but if you don't like that then just modify the source.
I figure that this script can become less useless if I post it here. Maybe someone will learn from it. It demonstrates perl regex, shortcircuiting, constants, bitwise operators, and how to use the LWP::Simple module for easy file downloading.
Oh, and I think the code tag makes things look fugly. So I'm going to follow Joe's habit of using teletype.
#!/usr/bin/perl -p
use strict;
use warnings;
use LWP::Simple;
#require "log.pl";
#createDebugLog("debug");
#setConsoleDisplay(1);
# gets the id of the specified user from the database
sub getUserId($) {
my $username = shift;
open UIDS, '<', 'uids' or die "There is no uids file!\n";
while (<UIDS>) {
# stores the user id
/(\d+)/;
# shortcircuits if there is no uid
$1 or die "There is no uid for $username in the database.\n";
return $1;
}
}
# fetches the last post of a specified user from x86labs.org
sub fetchLastPost($) {
my $username = shift;
my $uid = getUserId($username);
my $url = "http://www.x86labs.org:81/forum/?action=profile;u=$uid;sa=showPosts";
#get page unless it doesn't exist
my $content = LWP::Simple::get($url) unless -e $url;
# get last post (
$content =~ /<div class="post">(.*)<\/div>/i;
# return last post
return $1;
}
# flags for determining how exciting the post is ^.^
my $flags = 0;
use constant HAS_MANY_CAPS => 1;
use constant HAS_PROFANITY => 2;
use constant HAS_IMAGE => 4;
use constant HAS_CHRISTIAN_INFLUENCE => 8;
my @profanity =
('shit', 'fuck', 'bitch', 'damn');
# goes through all the users passed as arguments and analyzes their last posts
foreach my $user ($ARGV) {
my $lastpost = fetchLastPost($user)."\n";
print "-----------\nLast Post\n-----------\n";
print $lastpost;
$flags |= HAS_MANY_CAPS if $lastpost=~/[A-Z]{2,}/;
foreach my $word (@profanity) {
if ($lastpost =~ /$word/i) {
$flags |= HAS_PROFANITY;
last;
}
}
$flags |= HAS_IMAGE if $lastpost=~/<img.*>/i;
$flags |= HAS_CHRISTIAN_INFLUENCE if $lastpost=~/jesus|christ/i;
print "-----------\nFlags: $flags\n-----------";
}