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

0 Members and 1 Guest 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. 

Offline iago

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

Yeah, that's perl.  In PHP, it's
Code: [Select]
if (preg_match('/regex/', $cline) {
# code goes here
}

Offline Newby

  • x86
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #16 on: August 18, 2006, 02:01:19 pm »
That blows. PHP blows! :D
- 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 Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: fetchx86 -- pointless perl script
« Reply #17 on: August 18, 2006, 03:59:09 pm »
That blows. PHP blows! :D

You're a tool.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: fetchx86 -- pointless perl script
« Reply #18 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?)
I'd personally do as Joe suggests

You might be right about that, Joe.


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 #19 on: August 19, 2006, 04:27:47 pm »
(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 have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #20 on: August 21, 2006, 06:39:16 pm »
.NET failing is like IE7 being standard compliant:

A damn lie.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: fetchx86 -- pointless perl script
« Reply #21 on: August 21, 2006, 11:09:57 pm »
(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.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #22 on: October 22, 2006, 09:40:56 pm »
(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.)

Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: fetchx86 -- pointless perl script
« Reply #23 on: October 29, 2006, 01:19:47 am »
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.
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine