cd5920dd64a6f323c04955d5333743ab1a7c89f7
[www-search-torrentz.git] / lib / WWW / Search / Torrentz.pm
1 package WWW::Search::Torrentz;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 no warnings 'experimental::smartmatch';
7 use parent qw/WWW::Search/;
8
9 our $VERSION = '0.001';
10 our $MAINTAINER = 'Marius Gavrilescu <marius@ieval.ro>';
11
12 use WWW::Search::Torrentz::Result;
13
14 sub gui_query{ shift->native_query(@_) }
15
16 sub _native_setup_search{
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);
24 }
25
26 sub _parse_tree{
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);
36 }
37
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;
69 }
70
71 1;
72 __END__
73
74 =encoding utf-8
75
76 =head1 NAME
77
78 WWW::Search::Torrentz - search torrentz.eu with WWW:Search
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
89 WWW::Search::Torrentz is a subclass of WWW::Search that searches the L<https://torrentz.eu> search aggregator.
90
91 To use this module, read the L<WWW::Search> documentation.
92
93 Search results are instances of the L<WWW::Search::Torrentz::Result> class.
94
95 Available optional L<WWW::Search> methods:
96
97 =over
98
99 =item B<gui_query>
100
101 Identical to B<native_query>.
102
103 =item B<approximate_result_count>
104
105 Returns the exact result count, as indicated by Torrentz.
106
107 =back
108
109 =head1 SEE ALSO
110
111 L<https://torrentz.eu/help>, L<WWW::Search>, L<WWW::Search::Torrentz::Result>
112
113 =head1 AUTHOR
114
115 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
116
117 =head1 COPYRIGHT AND LICENSE
118
119 Copyright (C) 2013 by Marius Gavrilescu
120
121 This library is free software; you can redistribute it and/or modify
122 it under the same terms as Perl itself, either Perl version 5.18.1 or,
123 at your option, any later version of Perl 5 you may have available.
124
125
126 =cut
This page took 0.028321 seconds and 3 git commands to generate.