#!/usr/pkg/bin/perl 

use POSIX;
use HTML::TokeParser;
use LWP::UserAgent;
use HTTP::Request::Common qw(POST GET);
use HTTP::Cookies;



$|++;

sub getdoc {
    my $docurl = shift;


    my $req = GET  $docurl;

    $req->header(Accept => $acpthdr);

    # Pass request to the user agent and get a response back
    my $res = $ua->request($req);

    $cookie_jar->add_cookie_header( $req );
    $cookie_jar->extract_cookies( $res );
    $cookie_jar->save;


    # Check the outcome of the response
    if ($res->is_success) {
#        print "Got document for $docurl\n";
        print ".";
#        print $res->content();
    } else {
        print "x\n";
#        print $res->error_as_HTML;
        print $res->as_string();
        return ''
    }
#    undef $req;
#    undef $res;
}

sub p1 {
    my $docurl = shift;
    

    my $req = POST  $docurl , [ PHPSESSID=>$phpsid, action=>"voteinpoll",
pq505=>"505",
pq506=>"506",
pa506=>"7431",
pq507=>"507",
pq508=>"508",
pq509=>"509",
pq510=>"510",
pq511=>"511",
pq512=>"512",
pq513=>"513",
submit.x=>"23", submit.y=>"7", submit=>"vote" ]; 

    $req->header(Accept => $acpthdr);

    # Pass request to the user agent and get a response back
    my $res = $ua->request($req);

    $cookie_jar->add_cookie_header( $req );
    $cookie_jar->extract_cookies( $res );
    $cookie_jar->save;

    
    # Check the outcome of the response
    if ($res->is_success) {
        print ".";
    } else {
        print "x\n";
        return ''
    }
}

sub p2 {
    my $docurl = shift;
    

    my $req = POST  $docurl , [ PHPSESSID=>$phpsid, action=>"voteinpoll",
pq532=>"505",
pa532=>"7690",
pa533=>"533",
pq534=>"534",
submit.x=>"23", submit.y=>"7", submit=>"vote" ]; 

    $req->header(Accept => $acpthdr);

    # Pass request to the user agent and get a response back
    my $res = $ua->request($req);

    $cookie_jar->add_cookie_header( $req );
    $cookie_jar->extract_cookies( $res );
    $cookie_jar->save;

    
    # Check the outcome of the response
    if ($res->is_success) {
        print ".";
    } else {
        print "x\n";
        return ''
    }
}

sub p3 {
    my $docurl = shift;
    
    my $req = POST  $docurl , [ PHPSESSID=>$phpsid, action=>"voteinpoll",
pq514=>"514",
pq515=>"515",
pq516=>"516",
pq517=>"517",
pq518=>"518",
pa518=>"7488",
pq519=>"519",
submit.x=>"23", submit.y=>"7", submit=>"vote" ]; 

    $req->header(Accept => $acpthdr);

    # Pass request to the user agent and get a response back
    my $res = $ua->request($req);

    $cookie_jar->add_cookie_header( $req );
    $cookie_jar->extract_cookies( $res );
    $cookie_jar->save;

    
    # Check the outcome of the response
    if ($res->is_success) {
        print ".";
    } else {
        print "x\n";
        return ''
    }
}

while (5) {
$ua = LWP::UserAgent->new( keep-alive => 1, timeout => 10);
$ua->agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3");
$cookie_jar = HTTP::Cookies->new( 
  file => "$ENV{'HOME'}/lwp_cookies.dat",
           autosave => 1,
         );
$ua->cookie_jar($cookie_jar);

getdoc("http://fuse.tv/content/generic_poll/index3.php?poll_ids=505,506,507,508,509,510,511,512,513");

#print $cookie_jar->as_string;
($phpsid=$cookie_jar->as_string) =~ s/^.*PHPSESSID=([a-f0-9]*);.*/$1/s;
print "<<<$phpsid>>>\n";


$fucker="http://fuse.tv/content/generic_poll/index3.php?poll_ids=505,506,507,508,509,510,511,512,513";


    print p1($fucker);

$fucker="http://fuse.tv/content/generic_poll/index3.php?poll_ids=532,533,534";

    p2($fucker);

$fucker="http://fuse.tv/content/generic_poll/index3.php?poll_ids=514,515,516,517,518,519";


    p3($fucker);

    sleep(1);
}
