Clan x86

Technical (Development, Security, etc.) => General Programming => Tutorials, References, and Examples => Topic started by: Ender on August 17, 2006, 06:36:05 PM

Title: fetchx86 -- pointless perl script
Post by: Ender on August 17, 2006, 06:36:05 PM
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-----------";
}

Title: Re: fetchx86 -- pointless perl script
Post by: MyndFyre on August 17, 2006, 07:28:15 PM
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.  -_-
Title: Re: fetchx86 -- pointless perl script
Post by: iago on August 17, 2006, 07:32:26 PM
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. 
Title: Re: fetchx86 -- pointless perl script
Post by: Ender on August 17, 2006, 07:39:09 PM
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.

Title: Re: fetchx86 -- pointless perl script
Post by: MyndFyre on August 17, 2006, 07:47:02 PM
Ugh @ you two.  Just laugh.  IT WAS FUNNY.
Title: Re: fetchx86 -- pointless perl script
Post by: iago on August 17, 2006, 10:38:16 PM
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!
Title: Re: fetchx86 -- pointless perl script
Post by: 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.)
Title: Re: fetchx86 -- pointless perl script
Post by: Sidoh on August 17, 2006, 11:04:22 PM
   $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 ;)
Title: Re: fetchx86 -- pointless perl script
Post by: Ender on August 17, 2006, 11:42:55 PM
Quote from: Sidoh on August 17, 2006, 11:04:22 PM
+CHRISTIAN INFLUENCE -- CHRIST FROWNS AT MASTURBATION -- JOE
LOL!
Title: Re: fetchx86 -- pointless perl script
Post by: MyndFyre on August 18, 2006, 11:49:08 AM
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.
Title: Re: fetchx86 -- pointless perl script
Post by: 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
Title: Re: fetchx86 -- pointless perl script
Post by: Newby on August 18, 2006, 11:58:59 AM
That may be useless, but it beats the repetitive shit Joe codes.
Title: Re: fetchx86 -- pointless perl script
Post by: Ender on August 18, 2006, 12:17:28 PM
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 ;-).
Title: Re: fetchx86 -- pointless perl script
Post by: iago on August 18, 2006, 12:48:20 PM
$_ 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)
Title: Re: fetchx86 -- pointless perl script
Post by: 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.
Title: Re: fetchx86 -- pointless perl script
Post by: iago on August 18, 2006, 01:57:10 PM
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
}
Title: Re: fetchx86 -- pointless perl script
Post by: Newby on August 18, 2006, 02:01:19 PM
That blows. PHP blows! :D
Title: Re: fetchx86 -- pointless perl script
Post by: Sidoh on August 18, 2006, 03:59:09 PM
Quote from: Newby on August 18, 2006, 02:01:19 PM
That blows. PHP blows! :D

You're a tool.
Title: Re: fetchx86 -- pointless perl script
Post by: Joe on August 19, 2006, 06:25:21 AM
<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?)
Title: Re: fetchx86 -- pointless perl script
Post by: MyndFyre on August 19, 2006, 04:27:47 PM
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.  ^^
Title: Re: fetchx86 -- pointless perl script
Post by: Warrior on August 21, 2006, 06:39:16 PM
.NET failing is like IE7 being standard compliant:

A damn lie.
Title: Re: fetchx86 -- pointless perl script
Post by: Joe on August 21, 2006, 11:09:57 PM
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.
Title: Re: fetchx86 -- pointless perl script
Post by: Ender on October 22, 2006, 09:40:56 PM
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.)
Title: Re: fetchx86 -- pointless perl script
Post by: deadly7 on October 29, 2006, 01:19:47 AM
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.