package WWW::Reddit; { use warnings; use strict; use Object::InsideOut; use WWW::Mechanize; use XML::RSS; =head1 NAME WWW::Reddit - interface with reddit.com =head1 VERSION Version 0.05 =cut our $VERSION = '0.05'; =head1 SYNOPSIS use WWW::Reddit; my $r = WWW::Reddit->new( username => $username, password => $password ); $r->post( url => 'http://www.example.com', title => 'The new and wonderful example website' ); =head1 METHODS =head2 new used for instantiation. my $r = WWW::Reddit->new( username => $username, password => $password ); required arguments: username - your reddit.com username password - your reddit.com password =cut # Fields containing username and password # with standard get_* and set_* accessors # and automatic paramter processing upon object creation my @username :Field :Std_All(username); my @password :Field :Std_All(password); # Field containing WWW::Mechanize objects # With combined accessor # no automatic parameter processing on object creation. We instantiate this object. my @mech :Field :Type(WWW::Mechanize) :Std(mech); # Field containing the reddit ID for the current post. # combined accessor # no parameter procssing my @id :Field :Std(id); sub _init :Init { my $self = shift; $self->_login(); } sub _login { my $self = shift; my $mech = WWW::Mechanize->new(); $mech->get( 'http://www.reddit.com/' ); $mech->submit_form( form_number => 2, fields => { user_login => $self->get_username(), passwd_login => $self->get_password(), } ); $self->set_mech( $mech ); } =head2 post add a URL to reddit.com my $id = $r->post( url => 'http://www.example.com' title => 'The new and wonderful example website' ); required parameters: url - the address of the url that you'd like to post title - the title that you would like to have appear on reddit. returns: the string that represents the reddit ID of the URL posted. =cut sub post { my $self = shift; my %args = @_; my $mech = $self->get_mech(); $mech->get( 'http://reddit.com/submit' ); $mech->submit_form( form_number => 2, fields => { url => $args{'url'}, title => $args{'title'}, } ); $self->set_id( $self->get_id_from_url( $mech->uri() ) ); return $self->get_id(); } =head2 get_id my $id = $r->get_id(); get the reddit ID of the current submission. =head2 set_id $r->set_id( '63iup' ); pass in the ID of the reddit submission. =head2 details fetch the details for a reddit submission takes an optional reddit ID for a submisstion. Otherwise, uses the ID already in the object. returns a hashref that looks like: { 'submitted' => '29 Nov 2007', 'points' => '2', 'upvotes' => '17', 'downvotes' => '15' }; =cut sub details { my $self = shift; # If we have an ID passed in, set it. if ( my $id = shift ) { $self->set_id( $id ); } return unless $self->get_id(); my $mech = $self->get_mech(); my $url = sprintf( 'http://reddit.com/info/%s/details', $self->get_id ); $mech->get( $url ); my $details = { submitted => undef, # DD Mon YYYY points => undef, upvotes => undef, downvotes => undef, url => undef, title => undef, }; my $body = $mech->content(); return unless $body; if ( $body =~ /submitted<\/td>