| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Search::Torrentz::Result; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 15672 | use 5.014000; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 59 |  | 
| 4 | 2 |  |  | 2 |  | 9 | use strict; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 57 |  | 
| 5 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 54 |  | 
| 6 | 2 |  |  | 2 |  | 419 | use parent qw/WWW::SearchResult/; | 
|  | 2 |  |  |  |  | 240 |  | 
|  | 2 |  |  |  |  | 8 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.001003'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 2 |  |  | 2 |  | 45751 | use HTML::TreeBuilder; | 
|  | 2 |  |  |  |  | 25427 |  | 
|  | 2 |  |  |  |  | 19 |  | 
| 11 | 2 |  |  | 2 |  | 620 | use URI::Escape qw/uri_escape/; | 
|  | 2 |  |  |  |  | 1128 |  | 
|  | 2 |  |  |  |  | 1688 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub new{ | 
| 14 | 1 |  |  | 1 | 1 | 18 | my ($class, %args) = @_; | 
| 15 | 1 |  |  |  |  | 9 | my $self = $class->SUPER::new(@_); | 
| 16 | 1 |  |  |  |  | 15 | $self->{parsed} = 0; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 1 |  |  |  |  | 9 | $self->_elem($_ => $args{$_}) for qw/title verified age size seeders leechers infohash/; | 
| 19 | 1 |  |  |  |  | 34 | $self->{ua} = $args{ua}; | 
| 20 | 1 |  |  |  |  | 6 | $self->add_url("https://torrentz.eu/$args{infohash}"); | 
| 21 | 1 |  |  |  |  | 13 | $self | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 2 |  |  | 2 | 1 | 298 | sub infohash { shift->_elem(infohash => @_) } | 
| 25 | 1 |  |  | 1 | 1 | 467 | sub verified { shift->_elem(verified => @_) } | 
| 26 | 1 |  |  | 1 | 1 | 275 | sub age { shift->_elem(age => @_) } | 
| 27 | 1 |  |  | 1 | 1 | 296 | sub size { shift->_elem(size => @_) } | 
| 28 | 1 |  |  | 1 | 1 | 294 | sub seeders { shift->_elem(seeders => @_) } | 
| 29 | 1 |  |  | 1 | 1 | 292 | sub leechers { shift->_elem(leechers => @_) } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub magnet{ | 
| 32 | 1 |  |  | 1 | 1 | 309 | my ($self, $full) = @_; | 
| 33 | 1 |  |  |  |  | 4 | my $infohash = $self->infohash; | 
| 34 | 1 |  |  |  |  | 11 | my $title = uri_escape $self->title; | 
| 35 | 1 |  |  |  |  | 42 | my $uri = "magnet:?xt=urn:btih:$infohash&dn=$title"; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 | 50 |  |  |  | 4 | $uri .= join '', map { "&tr=$_"} map { uri_escape $_ } $self->trackers if $full; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 1 |  |  |  |  | 4 | $uri | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub parse_page { | 
| 43 | 0 |  |  | 0 | 1 |  | my $self = $_[0]; | 
| 44 | 0 |  |  |  |  |  | my $tree = HTML::TreeBuilder->new; | 
| 45 | 0 |  |  |  |  |  | $tree->utf8_mode(1); | 
| 46 | 0 |  |  |  |  |  | $tree->parse($self->{ua}->get($self->url)->content); | 
| 47 | 0 |  |  |  |  |  | $tree->eof; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 0 |  |  |  |  |  | my $trackers = $tree->look_down(class => 'trackers'); | 
| 50 | 0 |  | 0 |  |  |  | $self->{trackers} //= []; | 
| 51 | 0 |  |  |  |  |  | for my $tracker ($trackers->find('dl')) { | 
| 52 | 0 |  |  |  |  |  | push @{$self->{trackers}}, $tracker->find('a')->as_text; | 
|  | 0 |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | my $files = $tree->look_down(class => 'files'); | 
| 56 | 0 |  | 0 |  |  |  | $self->{files} //= []; | 
| 57 | 0 |  |  |  |  |  | $self->parse_directory(scalar $files->find('li'), ''); | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 0 |  |  |  |  |  | $self->{parsed} = 1; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub parse_directory{ | 
| 63 | 0 |  |  | 0 | 0 |  | my ($self, $directory, $prefix) = @_; | 
| 64 | 0 |  |  |  |  |  | $prefix .= $directory->as_text . '/'; | 
| 65 | 0 |  |  |  |  |  | my $contents_ul = $directory->right->find('ul'); | 
| 66 | 0 | 0 |  |  |  |  | return unless defined $contents_ul; # Empty directory | 
| 67 | 0 |  |  |  |  |  | my @children = $contents_ul->content_list; | 
| 68 | 0 |  |  |  |  |  | my $skip = 0; | 
| 69 | 0 |  |  |  |  |  | for my $child (@children) { | 
| 70 | 0 | 0 |  |  |  |  | if ($skip) { | 
| 71 | 0 |  |  |  |  |  | $skip = 0; | 
| 72 | 0 |  |  |  |  |  | next; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 | 0 | 0 |  |  |  | if (defined $child->attr('class') && $child->attr('class') eq 't') { | 
| 76 | 0 |  |  |  |  |  | $self->parse_directory($child, $prefix); | 
| 77 | 0 |  |  |  |  |  | $skip = 1; | 
| 78 |  |  |  |  |  |  | } else { | 
| 79 | 0 |  |  |  |  |  | $child->objectify_text; | 
| 80 | 0 |  |  |  |  |  | my ($filename, $size) = $child->find('~text'); | 
| 81 | 0 |  |  |  |  |  | push @{$self->{files}}, +{ | 
|  | 0 |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | path => $prefix.$filename->attr('text'), | 
| 83 |  |  |  |  |  |  | size => $size->attr('text') | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub trackers{ | 
| 90 | 0 |  |  | 0 | 1 |  | my $self = $_[0]; | 
| 91 | 0 | 0 |  |  |  |  | $self->parse_page unless $self->{parsed}; | 
| 92 | 0 |  |  |  |  |  | @{$self->{trackers}} | 
|  | 0 |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub files{ | 
| 96 | 0 |  |  | 0 | 1 |  | my $self = $_[0]; | 
| 97 | 0 | 0 |  |  |  |  | $self->parse_page unless $self->{parsed}; | 
| 98 | 0 |  |  |  |  |  | @{$self->{files}} | 
|  | 0 |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub torrent{ | 
| 102 | 0 |  |  | 0 | 1 |  | my $self = $_[0]; | 
| 103 | 0 |  |  |  |  |  | my $torrage = 'http://torrage.com/torrent/' . uc $self->infohash . '.torrent'; | 
| 104 | 0 |  |  |  |  |  | my $torrent = $self->{ua}->get($torrage)->content; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 0 |  |  |  |  |  | $torrent; # TODO: if this is undef, download metadata with magnet link | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | 1; | 
| 110 |  |  |  |  |  |  | __END__ |