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-----------";
}
Quote from: Ender on August 17, 2006, 06:36:05 PM
I [,,,] made a pointless perl script
Look! Joe's not the only one who does pointless stuff anymore!
I'm trying to decide whether that's good or bad. -_-
Quote from: MyndFyrex86] link=topic=7087.msg87997#msg87997 date=1155857295]
Quote from: Ender on August 17, 2006, 06:36:05 PM
I [,,,] made a pointless perl script
Look! Joe's not the only one who does pointless stuff anymore!
I'm trying to decide whether that's good or bad. -_-
It's a good thing. It's how you learn. And posting it here is a good way to get constructive criticism.
Quote from: MyndFyrex86] link=topic=7087.msg87997#msg87997 date=1155857295]
Quote from: Ender on August 17, 2006, 06:36:05 PM
I [,,,] made a pointless perl script
Look! Joe's not the only one who does pointless stuff anymore!
I'm trying to decide whether that's good or bad. -_-
Actually, I take that back. I refreshed myself on a lot of annoying perl things. And I had never used the LWP::Simple module before. Much more time-efficient to use it for downloading than IO::Socket or IO::Event::Socket.
I'm glad I wrote this. By pointless, I mean it's sort of pointless to use it. But writing it was good for learning and sharpening and I think it could do the same for others.
Ugh @ you two. Just laugh. IT WAS FUNNY.
Quote from: MyndFyrex86] link=topic=7087.msg88002#msg88002 date=1155858422]
Ugh @ you two. Just laugh. IT WAS FUNNY.
What was? The cheap shot at Joe? Come on!
Perl regex makes me so horny. Java regex is like... "let's make them write an essay!"
<3 java, sorry for dissing you
C# probably makes you write a book though. (Myndfyre, just laugh.)
$flags |= HAS_CHRISTIAN_INFLUENCE if $lastpost=~/jesus|christ/i;
... lol. Predicted outcome:
+CHRISTIAN INFLUENCE -- JESUS WILL SMITE YOUR FACE -- JOE
+CHRISTIAN INFLUENCE -- JESUS WILL SEND YOU TO HELL -- JOE
+CHRISTIAN INFLUENCE -- CHRIST FROWNS AT MASTURBATION -- JOE
... hehe. Just kidding Joe ;)
Quote from: Sidoh on August 17, 2006, 11:04:22 PM
+CHRISTIAN INFLUENCE -- CHRIST FROWNS AT MASTURBATION -- JOE
LOL!
Quote from: Ender on August 17, 2006, 11:02:18 PM
Java regex is like... "let's make them write an essay!"
<3 java, sorry for dissing you
C# probably makes you write a book though. (Myndfyre, just laugh.)
I haven't used Java regex, so I don't have a basis for comparison.
Quote from: Ender on August 17, 2006, 11:02:18 PM
Perl regex makes me so horny. Java regex is like... "let's make them write an essay!"
<3 java, sorry for dissing you
C# probably makes you write a book though. (Myndfyre, just laugh.)
In Java, you do yourvariable.matches("regex"); That's totally not enough to warrant your hyperbole!
In PHP, you have to do preg_matches("/regex/", $yourvariable). That's even longer than Java! :P
That may be useless, but it beats the repetitive shit Joe codes.
Quote from: iago on August 18, 2006, 11:55:38 AM
Quote from: Ender on August 17, 2006, 11:02:18 PM
Perl regex makes me so horny. Java regex is like... "let's make them write an essay!"
<3 java, sorry for dissing you
C# probably makes you write a book though. (Myndfyre, just laugh.)
In Java, you do yourvariable.matches("regex"); That's totally not enough to warrant your hyperbole!
In PHP, you have to do preg_matches("/regex/", $yourvariable). That's even longer than Java! :P
I don't think the String.matches method lets you specify options, like case insensitive. Then you have to go into the java.util.regex package and the Pattern and Matcher classes which are just too verbose for my liking. Perl is definitely my language of choice for doing regex, and the default variables kick ass ;-).
$_ and @_ suck, they're too confusing :P
I try to avoid them as much as I can, unless I'm writing code that's intended to be cryptic (like that rot13 program :D)
Quote from: iago on August 18, 2006, 11:55:38 AM
In PHP, you have to do preg_matches("/regex/", $yourvariable). That's even longer than Java! :P
if $cline ~= /regex/ {
# code goes here
}
Something like that.
Quote from: Newby on August 18, 2006, 01:43:25 PM
Quote from: iago on August 18, 2006, 11:55:38 AM
In PHP, you have to do preg_matches("/regex/", $yourvariable). That's even longer than Java! :P
if $cline ~= /regex/ {
# code goes here
}
Something like that.
Yeah, that's perl. In PHP, it's
if (preg_match('/regex/', $cline) {
# code goes here
}
That blows. PHP blows! :D
<3 Sidoh. Give him many gifts in thanks for his wisdom.
Newby, however, seems to be confused. This "Joe" character we speak of hasn't coded anything for a very long time.
(Actually I was just downloading VC#. I'm going to try learning that just for the fun of it. I mean.. we all know .NET will fail, right?)
Quote from: Joex86] link=topic=7087.msg88197#msg88197 date=1155983121]
(Actually I was just downloading VC#. I'm going to try learning that just for the fun of it. I mean.. we all know .NET will fail, right?)
Once you switch you'll never go back. ^^
.NET failing is like IE7 being standard compliant:
A damn lie.
Quote from: MyndFyrex86] link=topic=7087.msg88265#msg88265 date=1156019267]
Quote from: Joex86] link=topic=7087.msg88197#msg88197 date=1155983121]
(Actually I was just downloading VC#. I'm going to try learning that just for the fun of it. I mean.. we all know .NET will fail, right?)
Once you switch you'll never go back. ^^
I will tell you, I am liking it.
Quote from: Joex86] link=topic=7087.msg88689#msg88689 date=1156216197]
Quote from: MyndFyrex86] link=topic=7087.msg88265#msg88265 date=1156019267]
Quote from: Joex86] link=topic=7087.msg88197#msg88197 date=1155983121]
(Actually I was just downloading VC#. I'm going to try learning that just for the fun of it. I mean.. we all know .NET will fail, right?)
Once you switch you'll never go back. ^^
I will tell you, I am liking it.
You'd bang a hole in the wall, let a lone like any esoteric language.
(Props to whoever came up with the first clause, I think it was Newby's dad.)
Quote from: Ender on October 22, 2006, 09:40:56 PM
You'd bang a hole in the wall, let a lone like any esoteric language.
(Props to whoever came up with the first clause, I think it was Newby's dad.)
HAH..
Yeah, that was Newby's dad.