Hi monks!

A few days ago, I thought of a module that would enable to put some images easily on the internet. Luckily, there is a website that allows people to put their pictures on, freely ("free" as in "be offered a beer").
Taking my courage with both hands I wrote my first module ever, an OO module, above all \o/
It lets you put a picture on ImagesHack.us, and gives you all the links to the image, ie direct links, forum links, and all that stuff the website gives you pre-chewed.
The code is still a little bit dirty, some vars have a one letter-long name, but when I clean it, if I see you like this module, I'll put it on CPAN, probably under WWW:: (unless you have a better idea, but I don't think it deserves ACME::)


Anyway...All comments welcome...

The module itself
#!/usr/bin/perl -w #===================================================================== +========== # # FILE: ImagesHack.pm # # USAGE: use WWW::ImagesHack # # DEshowImageCRIPTION: upload a file on imageshack.us # # OPTIONshowImage: --- # REQUIREMENTshowImage: --- # BUGshowImage: --- # NOTEshowImage: --- # AUTHOR: Axioplase (Axioplase), <dev@pied.mine.nu> # COMPANY: None # VERshowImageION: 1.0 # CREATED: 15.09.2005 01:25:46 CEshowImageT # REVIshowImageION: --- #===================================================================== +========== package WWW::ImagesHack; use base 'Exporter'; use strict; #for those who dare use LWP::UserAgent; use HTML::Form; use HTML::TokeParser; sub new { my $class = shift; # $_[0] contains the class name my $img = shift; # $_[1] contains the name of the file my %liste; my $self = {}; bless( $self, $class ); $self->{image} = $img; return $self; } sub upload { my $self=shift; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(POST => 'http://imageshack.us/'); $req->content_type('application/x-www-form-urlencoded'); my $res = $ua->request($req); my $form = (HTML::Form->parse($res))[0]; $form->value( 'fileupload',$self->{image} ); my $ares= ${$ua->request($form->click)}{_content}; my $p = HTML::TokeParser->new(\$ares); foreach(qw /thumbnailForWebsite thumbnailForForums1 thumbnailForForu +ms2 hotlinkForForums1 hotlinkForForums2 hotlinkForWebsite showImage d +irectLinkToImage/){ my $token = $p->get_tag("input"); $self->{liste}{$_} = $token->[1]{value}; } } sub get_all { my $self = shift; return $self->{liste}; } sub get_thumbnailForWebsite{ my $self = shift; return $self->{liste}{thumbnailForWebsite}; } sub get_thumbnailForForums1{ my $self = shift; return $self->{liste}{thumbnailForForums1}; } sub get_thumbnailForForums2{ my $self = shift; return $self->{liste}{thumbnailForForums2}; } sub get_hotlinkForForums1{ my $self = shift; return $self->{liste}{hotlinkForForums1}; } sub get_hotlinkForForums2{ my $self = shift; return $self->{liste}{hotlinkForForums2}; } sub get_hotlinkForWebsite{ my $self = shift; return $self->{liste}{hotlinkForWebsite}; } sub get_showImage{ my $self = shift; return $self->{liste}{showImage}; } sub get_directLinkToImage{ my $self = shift; return $self->{liste}{directLinkToImage}; } 1;



And the test

#!/usr/bin/perl -w #===================================================================== +========== # # FILE: testimageshack.pl # # USAGE: ./testimageshack.pl # # DESCRIPTION: a test file for ImagesHack.pm # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: --- # AUTHOR: Axioplase (Axioplase), <dev@pied.mine.nu> # COMPANY: None # VERSION: 1.0 # CREATED: 15.09.2005 03:08:31 CEST # REVISION: --- #===================================================================== +========== use strict; use lib '/home/Pied/Code/Perl/WWW::ImagesHack/'; use WWW::ImagesHack; my $img='/tmp/foo.jpg'; #edit this.... my $ih=WWW::ImagesHack->new($img); $ih->upload; my %res=%{$ih->get_all}; foreach (keys %res){ print "$_ => $res{$_}\n"; } print "\n\n"; print $ih->get_thumbnailForWebsite,"\n"; print $ih->get_thumbnailForForums1,"\n"; print $ih->get_thumbnailForForums2,"\n"; print $ih->get_hotlinkForForums1,"\n"; print $ih->get_hotlinkForForums2,"\n"; print $ih->get_hotlinkForWebsite,"\n"; print $ih->get_showImage,"\n"; print $ih->get_directLinkToImage,"\n";




P!

Are the monkeys gone yet?

Replies are listed 'Best First'.
Re: WWW::ImagesHack
by PodMaster (Abbot) on Sep 15, 2005 at 09:45 UTC
    Here's a couple of tips
    • the name should probably be WWW::ImageShack :)
    • you don't need use base 'Exporter';
    • you should use pod to document the module
    • to prepare for cpan, read How to make a CPAN Module Distribution and the linked material (there is even a book with a similar title)
    • if you used WWW::Mechanize, you could've skipped dealing with those HTML:: modules directly.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: WWW::ImagesHack
by davidrw (Prior) on Sep 15, 2005 at 12:40 UTC
    I second podmaster's onte about WWW::Mechanize -- it can make code much simpler..

    But what i wanted to note is that you could reduce the duplicate code and make maintenance (and use) a little easier by just having one accessor .. (there's many ways to do this -- here's just a quick one)
    our KEY_LIST => [ qw/ thumbnailForWebsite thumbnailForForums1 thumbnailForForums2 hotlinkForForums1 hotlinkForForums2 hotlinkForWebsite showImage directLinkToImage / ]; sub get { my $self = shift; my $key = shift; # could grep @{&KEY_LIST} here too to check for valid key return exists $self->{liste}->{$key} ? $self->{liste}->{$key} : unde +f; } # this could be left the same or changed to this: sub get_all { my $self = shift; return { map { $_ => $self->{liste}->{$_} } @{&KEY_LIST} }; } # note: could also replace get_all() with get() or get('all') # and note that the loop in upload() becomes just: foreach( @{&KEY_LIST} ){ ... }
    Use of the module then becomes:
    print $ih->get('thumbnailForWebsite'), "\n"; print $ih->get('thumbnailForForums1'), "\n"; print $ih->get('thumbnailForForums2'), "\n"; print $ih->get('hotlinkForForums1'), "\n"; print $ih->get('hotlinkForForums2'), "\n"; print $ih->get('hotlinkForWebsite'), "\n"; print $ih->get('showImage'), "\n"; print $ih->get('directLinkToImage'), "\n";
      Yes thanks. I'll definitely use WWW::Mechanize. Ihad been searching CPAN for stuff like that but couldn't find it (as I had never used it, didn't really know what to look for).

      As for the refactoring, yes, this is a good idea, though what I was thinking of was rather dynamic sub creation, something like
      foreach(@keylist){ my $key = shift; sub get_$key { #code of the func} push @funcs,\get_$key; }
      But if this is possible, then I should return the subs' references so that they aren't destroyed when I loop in the foreach...and then export them all dereferenced...
      How about that? Or is it obfuscating the code for peanuts?


      P!

      Are the monkeys gone yet?
        i meant to mention AUTOLOAD as another way to do it dymanically ... check out the "AUTOLOAD: Proxy Methods" section in perltoot -- i think it's exactly what you're looking for...