Reindent lib/ and t/
[www-search-torrentz.git] / lib / WWW / Search / Torrentz.pm
CommitLineData
e605ae0f
MG
1package WWW::Search::Torrentz;
2
3use 5.014000;
4use strict;
5use warnings;
d54e3343 6no if $] >= 5.018, warnings => 'experimental::smartmatch';
e605ae0f
MG
7use parent qw/WWW::Search/;
8
d54e3343 9our $VERSION = '0.001001';
e605ae0f
MG
10our $MAINTAINER = 'Marius Gavrilescu <marius@ieval.ro>';
11
12use WWW::Search::Torrentz::Result;
13
14sub gui_query{ shift->native_query(@_) }
15
16sub _native_setup_search{
7dcf8cda
MG
17 my ($self, $native_query, $options) = @_;
18 $self->agent_email('marius@ieval.ro');
19 $options //= {};
20 my $base_url = $options->{search_url} // 'https://torrentz.eu/search';
21 $self->{search_debug} = $options->{search_debug};
22 $self->{_next_url} = "$base_url?f=$native_query";
23 $self->user_agent->delay(2/60);
e605ae0f
MG
24}
25
26sub _parse_tree{
7dcf8cda
MG
27 my ($self, $tree) = @_;
28 my $found = 0;
29
30 my @potential_results = $tree->find('dl');
31 my $result_count = $tree->find('h2')->as_text;
32 if (defined $result_count && $result_count ne 'No Torrents Found') {
33 $result_count =~ s/orrents.*//;
34 $result_count =~ y/0-9//cd;
35 $self->approximate_result_count(int $result_count);
e605ae0f
MG
36 }
37
7dcf8cda
MG
38 for my $node (@potential_results) {
39 my $a = $node->find('a');
40 next unless defined $a;
41
42 my $infohash = substr $a->attr('href'), 1;
43 next unless $infohash =~ m,^[a-f0-9]{40}$,;
44 my $title = $a->as_text;
45 my ($verified, $age, $size, $seeders, $leechers);
46 $verified = 0;
47 for my $span ($node->find('span')) {
48 given($span->attr('class')){
49 $verified = int ($span->as_text =~ m,^\d+,) when 'v';
50 $age = $span->as_text when 'a';
51 $size = $span->as_text when 's';
52 $seeders = int $span->as_text when 'u';
53 $leechers = int $span->as_text when 'd';
54 }
55 }
56
57 push @{$self->{cache}}, WWW::Search::Torrentz::Result->new(infohash => $infohash, title => $title, verified => $verified, age => $age, size => $size, seeders => $seeders, leechers => $leechers, ua => $self->user_agent);
58 say STDERR "infohash => $infohash, title => $title, verified => $verified, age => $age, size => $size, seeders => $seeders, leechers => $leechers" if $self->{search_debug};
59 $found++;
60 }
61
62 my $url = $tree->look_down(rel => 'next');
63 if (defined $url) {
64 my $prev = $self->{_prev_url} =~ s,/[^/]+$,,r;
65 $self->{_next_url} = $prev . $url->attr('href')
66 }
67 say STDERR "Found: $found" if $self->{search_debug};
68 return $found;
e605ae0f
MG
69}
70
711;
72__END__
73
74=encoding utf-8
75
76=head1 NAME
77
d54e3343 78WWW::Search::Torrentz - search torrentz.eu with WWW::Search
e605ae0f
MG
79
80=head1 SYNOPSIS
81
82 use WWW::Search;
83 my $search = WWW::Search->new('Torrentz');
84 $search->gui_query('query');
85 say $_->title while $_ = $search->next_result;
86
87=head1 DESCRIPTION
88
89WWW::Search::Torrentz is a subclass of WWW::Search that searches the L<https://torrentz.eu> search aggregator.
90
91To use this module, read the L<WWW::Search> documentation.
92
93Search results are instances of the L<WWW::Search::Torrentz::Result> class.
94
95Available optional L<WWW::Search> methods:
96
97=over
98
99=item B<gui_query>
100
101Identical to B<native_query>.
102
103=item B<approximate_result_count>
104
105Returns the exact result count, as indicated by Torrentz.
106
107=back
108
109=head1 SEE ALSO
110
111L<https://torrentz.eu/help>, L<WWW::Search>, L<WWW::Search::Torrentz::Result>
112
113=head1 AUTHOR
114
115Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
116
117=head1 COPYRIGHT AND LICENSE
118
119Copyright (C) 2013 by Marius Gavrilescu
120
121This library is free software; you can redistribute it and/or modify
122it under the same terms as Perl itself, either Perl version 5.18.1 or,
123at your option, any later version of Perl 5 you may have available.
124
125
126=cut
This page took 0.017684 seconds and 4 git commands to generate.