| 1 | package WWW::BitTorrent; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | WWW::BitTorrent - Search and Download .torrent(s) files from BitTorrents boards. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | # Creates WWW::BitTorrent Object. | ||
| 12 | my $client = WWW::BitTorrent->new( 'site' => $site, 'user' => $user, 'pass' => $pass); | ||
| 13 | |||
| 14 | # Check for errors. | ||
| 15 | if ($client->{STAT} == 0) { | ||
| 16 | print "ERROR: " . $client->{ERROR}."\n"; | ||
| 17 | exit; | ||
| 18 | } | ||
| 19 | |||
| 20 | # return all rows (from page 1) and prints it | ||
| 21 | my @page = $client->browse(1); | ||
| 22 | die("Error occured calling browse or page is empty\n") if ($#page == -1); | ||
| 23 | |||
| 24 | foreach my $row (@page) { | ||
| 25 | print "Row: " . $row->{name} . "- id: " . $row->{id} ."\n"; | ||
| 26 | } | ||
| 27 | |||
| 28 | # Search for a row | ||
| 29 | @page = $client->search('DiAMOND'); | ||
| 30 | die("No Results.\n") if ($#page == -1); | ||
| 31 | |||
| 32 | my $download_row; | ||
| 33 | |||
| 34 | foreach my $row (@page) { | ||
| 35 | print "Row: " . $row->{name} . "- id: " . $row->{id} ."\n"; | ||
| 36 | $download_row = $row if ($row->{name} =~ /my.favorite.movie/); | ||
| 37 | } | ||
| 38 | |||
| 39 | # download .torrent file | ||
| 40 | $client->download_torrent($download_row, '/tmp/' . $download_row->{id} . '.torrent'); | ||
| 41 | |||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | "WWW::BitTorrent" provides an object interface to using two | ||
| 46 | BitTorrent based boards: "TBSource", "BrokenStone" (tell me about more). | ||
| 47 | This boards are being update every day with files. | ||
| 48 | This module allows you to search for files in a couple of ways. | ||
| 49 | |||
| 50 | NOTE: any use of this module that is illegal is out of my responsibilty. | ||
| 51 | |||
| 52 | This module requires B<LWP::UserAgent> and B<LWP::Simple>. | ||
| 53 | |||
| 54 | =head1 INTERFACE | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | use strict; | ||
| 59 | |||
| 60 | use vars qw/$VERSION/; | ||
| 61 | $VERSION = "0.01"; | ||
| 62 | |||
| 63 | use LWP::UserAgent; | ||
| 64 | use LWP::Simple; | ||
| 65 | |||
| 66 | # fast create for the user agent object with the ident cookie | ||
| 67 | sub _agent { | ||
| 68 | my $self = shift; | ||
| 69 | |||
| 70 | # creates LWP::UserAgent object | ||
| 71 | my $agent = LWP::UserAgent->new; | ||
| 72 | $agent->timeout(10); | ||
| 73 | |||
| 74 | if ($self->{COOKIE}) { | ||
| 75 | $agent->default_header('Cookie' => $self->{COOKIE}); | ||
| 76 | } | ||
| 77 | |||
| 78 | $agent->agent('Mozilla/5.0'); # fake agent | ||
| 79 | $agent->default_header('Keep-Alive' => 300); | ||
| 80 | $agent->default_header('Connection' => 'keep-alive'); | ||
| 81 | |||
| 82 | $self->{AGENT} = $agent; | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _parse { | ||
| 88 | my $self = shift; | ||
| 89 | my $content = shift; | ||
| 90 | |||
| 91 | my @page; | ||
| 92 | # parse the current page and create a hash with the information | ||
| 93 | while ($content =~ /<td align=left><a href=\"details\.php\?id=(\d+)&hit=1\"><b>([a-zA-Z0-9-_.]+)<\/b><\/a>/g) { | ||
| 94 | my $row; | ||
| 95 | $row->{name} = $2; | ||
| 96 | $row->{id} = $1; | ||
| 97 | $row->{torrent} = $self->{SITE} . "/download.php/" . $1 ."/" . $2 . ".torrent"; | ||
| 98 | |||
| 99 | push(@page, $row); | ||
| 100 | } | ||
| 101 | |||
| 102 | return @page; | ||
| 103 | } | ||
| 104 | |||
| 105 | =pod | ||
| 106 | |||
| 107 | new( [ARGS] ) | ||
| 108 | |||
| 109 | Used to login the Board with the username and password | ||
| 110 | of the board (all arguments are required) and create a new Object of | ||
| 111 | the WWW::BitTorrent module. | ||
| 112 | |||
| 113 | Arguments: | ||
| 114 | |||
| 115 | site- The BitTorrents board URL address. | ||
| 116 | user- the username for login the board. | ||
| 117 | pass- the password for login the board. | ||
| 118 | |||
| 119 | NOTES: Returns a blessed object anyways. | ||
| 120 | object->{STAT}: 0 - for error (object->{ERROR} - http code) | ||
| 121 | 1 - for success | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub new { | ||
| 126 | my $class = shift; | ||
| 127 | my %args = @_; | ||
| 128 | my $self; | ||
| 129 | |||
| 130 | # inserts args into class | ||
| 131 | foreach (keys %args) { | ||
| 132 | $self->{uc($_)} = $args{$_}; | ||
| 133 | } | ||
| 134 | |||
| 135 | my $agent = LWP::UserAgent->new; | ||
| 136 | $agent->timeout(10); | ||
| 137 | |||
| 138 | $agent->agent('Mozilla/5.0'); # fake agent | ||
| 139 | $agent->default_header('Keep-Alive' => 300); | ||
| 140 | $agent->default_header('Connection' => 'keep-alive'); | ||
| 141 | |||
| 142 | # Create and send the request | ||
| 143 | my $req = HTTP::Request->new( POST => $self->{SITE}."/".'takelogin.php'); | ||
| 144 | $req->content_type('application/x-www-form-urlencoded'); | ||
| 145 | $req->content('username='.$self->{USER}.'&password='.$self->{PASS}); | ||
| 146 | my $res = $agent->request($req); | ||
| 147 | |||
| 148 | if ($res->status_line =~ RC_FOUND || $res->status_line =~ RC_OK) { | ||
| 149 | |||
| 150 | my $cookies = $res->header("Set-Cookie"); | ||
| 151 | |||
| 152 | # parse cookies | ||
| 153 | $self->{uid} = $1 if ($cookies =~ /uid=(\d+);/g); | ||
| 154 | $self->{pass} = $1 if ($cookies =~ /pass=(\w+);/g); | ||
| 155 | $self->{punbb_cookie} = $1 if ($cookies =~ /punbb_cookie=([a-zA-Z0-9%]+);/g); | ||
| 156 | |||
| 157 | |||
| 158 | # cookie for ident | ||
| 159 | $self->{COOKIE} = 'punbb_cookie=' . $self->{punbb_cookie} .'; ' . | ||
| 160 | 'uid=' . $self->{uid} . '; ' . | ||
| 161 | 'pass=' . $self->{pass}; | ||
| 162 | |||
| 163 | delete $self->{PUNBB_COOKIE}; | ||
| 164 | delete $self->{uid}; | ||
| 165 | delete $self->{pass}; | ||
| 166 | |||
| 167 | $self->{STAT} = 1; | ||
| 168 | } | ||
| 169 | else { | ||
| 170 | $self->{STAT} = 0; # error occured | ||
| 171 | $self->{ERROR} = $res->status_line; # the error: status number and text | ||
| 172 | } | ||
| 173 | |||
| 174 | return bless $self, $class; | ||
| 175 | |||
| 176 | } | ||
| 177 | |||
| 178 | =pod | ||
| 179 | |||
| 180 | browse( $page_number ) | ||
| 181 | |||
| 182 | Returns a list(array) of all rows(ref) from the the '$page_number' page. | ||
| 183 | C<page_number>: number between 0 (first page) and max number of pages. | ||
| 184 | |||
| 185 | Arguments: | ||
| 186 | |||
| 187 | page_number- the number of the page you want the rows from. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub browse { | ||
| 192 | my $self = shift; | ||
| 193 | my $num = shift; | ||
| 194 | |||
| 195 | my $url = $self->{SITE}; | ||
| 196 | |||
| 197 | if ($num == 0) { $url .= "/browse.php"; } else { $url .= "/browse.php?page=" . $num; } | ||
| 198 | |||
| 199 | # create new agent with cookie into $self | ||
| 200 | $self->_agent(); | ||
| 201 | |||
| 202 | # create the request | ||
| 203 | my $req = HTTP::Request->new(GET => $url); | ||
| 204 | my $res = $self->{AGENT}->request($req); | ||
| 205 | |||
| 206 | # status ok (200) | ||
| 207 | if ($res->status_line =~ RC_OK) { | ||
| 208 | return $self->_parse($res->content); | ||
| 209 | } | ||
| 210 | |||
| 211 | return 0; | ||
| 212 | } | ||
| 213 | |||
| 214 | =pod | ||
| 215 | |||
| 216 | search( $keyword ) | ||
| 217 | |||
| 218 | This method do the same as browse just by the search option at the board. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub search { | ||
| 223 | my $self = shift; | ||
| 224 | my $keyword = shift; | ||
| 225 | |||
| 226 | $keyword =~ s/ /+/; | ||
| 227 | |||
| 228 | my $url = $self->{SITE} . "/browse.php?search=".$keyword; | ||
| 229 | |||
| 230 | $self->_agent; | ||
| 231 | my $req = HTTP::Request->new(GET => $url); | ||
| 232 | my $res = $self->{AGENT}->request($req); | ||
| 233 | |||
| 234 | if ($res->status_line =~ RC_OK) { | ||
| 235 | return $self->_parse($res->content); | ||
| 236 | } | ||
| 237 | |||
| 238 | return 0; | ||
| 239 | } | ||
| 240 | |||
| 241 | =pod | ||
| 242 | |||
| 243 | download_torrent($row, $path) | ||
| 244 | |||
| 245 | Download the .torrent file using the LWP::Simple module. | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | sub download_torrent { | ||
| 250 | my $self = shift; | ||
| 251 | my $row = shift; | ||
| 252 | my $path = shift; | ||
| 253 | |||
| 254 | # using the LWP::Simple function to download the .torrent file | ||
| 255 | mirror($row->{torrent}, $path); | ||
| 256 | |||
| 257 | return $self; | ||
| 258 | } | ||
| 259 | |||
| 260 | =pod | ||
| 261 | |||
| 262 | =head1 AUTHOR | ||
| 263 | |||
| 264 | This module was written by | ||
| 265 | Amit Sides C<< <amit.sides@gmail.com> >> | ||
| 266 | |||
| 267 | =head1 Copyright | ||
| 268 | |||
| 269 | Copyright (c) 2006 Amit Sides. All rights reserved. | ||
| 270 | |||
| 271 | =head1 LICENSE | ||
| 272 | |||
| 273 | This program is free software; you can redistribute it | ||
| 274 | and/or modify it under the same terms as Perl itself. | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | 1; |