Author Topic: fetchx86 -- pointless perl script  (Read 10761 times)

0 Members and 2 Guests are viewing this topic.

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
fetchx86 -- pointless perl script
« 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-----------";
}

« Last Edit: August 18, 2006, 12:16:11 am by Ender »

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: fetchx86 -- pointless perl script
« Reply #1 on: August 17, 2006, 07:28:15 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.  -_-
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: fetchx86 -- pointless perl script
« Reply #2 on: August 17, 2006, 07:32:26 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. 

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #3 on: August 17, 2006, 07:39:09 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.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: fetchx86 -- pointless perl script
« Reply #4 on: August 17, 2006, 07:47:02 pm »
Ugh @ you two.  Just laugh.  IT WAS FUNNY.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: fetchx86 -- pointless perl script
« Reply #5 on: August 17, 2006, 10:38:16 pm »
Ugh @ you two.  Just laugh.  IT WAS FUNNY.
What was?  The cheap shot at Joe?  Come on!

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #6 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.)
« Last Edit: August 17, 2006, 11:04:14 pm by Ender »

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: fetchx86 -- pointless perl script
« Reply #7 on: August 17, 2006, 11:04:22 pm »
Code: [Select]
   $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 ;)

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #8 on: August 17, 2006, 11:42:55 pm »
+CHRISTIAN INFLUENCE -- CHRIST FROWNS AT MASTURBATION -- JOE
LOL!

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: fetchx86 -- pointless perl script
« Reply #9 on: August 18, 2006, 11:49:08 am »
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.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: fetchx86 -- pointless perl script
« Reply #10 on: August 18, 2006, 11:55:38 am »
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

Offline Newby

  • x86
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #11 on: August 18, 2006, 11:58:59 am »
That may be useless, but it beats the repetitive shit Joe codes.
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #12 on: August 18, 2006, 12:17:28 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 ;-).
« Last Edit: August 18, 2006, 12:20:08 pm by Ender »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: fetchx86 -- pointless perl script
« Reply #13 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)

Offline Newby

  • x86
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #14 on: August 18, 2006, 01:43:25 pm »
In PHP, you have to do preg_matches("/regex/", $yourvariable).  That's even longer than Java! :P

Code: [Select]
if $cline ~= /regex/ {
# code goes here
}

Something like that.
- Newby
http://www.x86labs.org

Quote
[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT.