Add search support
authorMarius Gavrilescu <marius@ieval.ro>
Tue, 17 Mar 2015 20:49:20 +0000 (22:49 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Tue, 17 Mar 2015 20:49:20 +0000 (22:49 +0200)
lib/WebService/Strike.pm
lib/WebService/Strike/Torrent.pm
t/WebService-Strike.t

index 56d1c474d5dafe04de1ae05c4f2ef954d5126494..f11d077f8dd53c490476feaaa70c9a9452f808fc 100644 (file)
@@ -5,8 +5,8 @@ use strict;
 use warnings;
 use parent qw/Exporter/;
 
-our @EXPORT    = qw/strike/; ## no critic (ProhibitAutomaticExportation)
-our @EXPORT_OK = qw/strike_query strike/;
+our @EXPORT    = qw/strike strike_search/; ## no critic (ProhibitAutomaticExportation)
+our @EXPORT_OK = qw/strike_query strike strike_search/;
 our $VERSION = '0.001002';
 our $BASE_URL = 'http://getstrike.net/api/torrents/';
 
@@ -17,18 +17,35 @@ use WebService::Strike::Torrent;
 
 sub _ht { HTTP::Tiny->new(agent => "WebService-Strike/$VERSION") }
 
-sub strike_query {
-       my (@hashes) = @_;
-       my $url = "$BASE_URL/info/?hashes=" . join ',', map { uc } @hashes;
+sub _query {
+       my ($url) = @_;
+
        my $ht = _ht;
        my $response = $ht->get($url);
        die $response unless $response->{success}; ## no critic (RequireCarping)
        $response = decode_json $response->{content};
 
        die $response unless ref $response eq 'ARRAY'; ## no critic (RequireCarping)
+       map { WebService::Strike::Torrent->new($_) } @{$response->[1]};
+}
+
+sub strike_query {
+       my (@hashes) = @_;
+       my $url = "$BASE_URL/info/?hashes=" . join ',', map { uc } @hashes;
+
        my $sorter = sbe(\@hashes, {xform => sub { $_[0]->hash }});
-       my @torrents = map { WebService::Strike::Torrent->new($_) } @{$response->[1]};
-       @torrents = $sorter->(@torrents);
+       my @torrents = $sorter->(_query $url);
+       wantarray ? @torrents : $torrents[0]
+}
+
+sub strike_search {
+       my ($query, $full, %args) = @_;
+       $args{q} = $query;
+       my $url = "$BASE_URL/search/?" . HTTP::Tiny->www_form_urlencode(\%args);
+
+       my @torrents = _query $url;
+       @torrents = $torrents[0] unless wantarray;
+       @torrents = strike_query map { $_->hash } @torrents if $full;
        wantarray ? @torrents : $torrents[0]
 }
 
@@ -52,6 +69,14 @@ WebService::Strike - Get torrent info from getstrike.net API
   my $torrent = $t->torrent;   # Returns the torrent file
   $t->torrent('file.torrent'); # Downloads the torrent file to 'file.torrent'
 
+  my @debian = strike_search 'Debian';
+  say 'Found ' . @debian . ' torrents matching "Debian"';
+  say 'First torrent has info hash ' . $debian[0]->hash;
+
+  my $mp = strike_search 'Modern perl', 1;
+  say 'Torrent has ' . $mp->count . ' files. They are:';
+  say join ' ', @{$mp->file_names};
+
 =head1 DESCRIPTION
 
 Strike API is a service for getting information about a torrent given
@@ -61,14 +86,27 @@ its info hash. WebService::Strike is a wrapper for this service.
 
 =item B<strike>(I<@info_hashes>)
 
-Returns a list of hashrefs, one for each info hash. The hashrefs are
-blessed into the L<WebService::Strike::Torrent> package. Dies in case
-of error.
+Returns a list of L<WebService::Strike::Torrent> objects in list
+context or the first such object in scalar context. Dies in case of
+error.
 
 =item B<strike_query>
 
 Alias for B<strike>. Not exported by default.
 
+=item B<strike_search>(I<$search_term>, [I<$full>])
+
+Searches for torrents given a search term. Returns a list of
+L<WebService::Strike::Torrent> objects in list context or the first
+such object in scalar context.
+
+If I<$full> is false (default), the returned objects will be
+incomplete: their B<file_names> and B<file_lengths> accessors will
+return undef.
+
+If I<$full> is true, B<strike> will be called with the info hashes of
+the found torrents, thus obtaining complete objects.
+
 =back
 
 =head1 SEE ALSO
index b8520005327fb96263ed8070fa12d0a7bf020fd5..82179fba3f57e4432737bc36af9f2810ada9b6c4 100644 (file)
@@ -30,8 +30,10 @@ sub new{
        $self = $self->SUPER::new(@args);
        $self->{torrent_hash} = uc $self->hash;
        $self->{upload_date} = str2time $self->date, 'UTC';
-       $self->{file_names} = $self->file_info->[0]->{file_names};
-       $self->{file_lengths} = $self->file_info->[0]->{file_lengths};
+       if ($self->file_info) {
+               $self->{file_names} = $self->file_info->[0]->{file_names};
+               $self->{file_lengths} = $self->file_info->[0]->{file_lengths};
+       }
        $self
 }
 
index ce3c39e20a4f947bf3e29d845504b01b0e1a8305..42825e19196a469ed639a02d937b50487f544db1 100644 (file)
@@ -3,7 +3,7 @@ use strict;
 use warnings;
 
 use Test::RequiresInternet qw/getstrike.net 80/;
-use Test::More tests => 6;
+use Test::More tests => 9;
 use Try::Tiny;
 BEGIN { use_ok('WebService::Strike') };
 
@@ -25,3 +25,14 @@ try {
 } catch {
        is $_->{status}, 404, 'non-existent torrent status is 404';
 };
+
+my @debian = strike_search 'Debian';
+ok @debian > 10, 'search for Debian returned more than 10 results';
+try {
+       strike_search "nosuchstring$$";
+} catch {
+       is $_->{status}, 404, "search for nosuchstring$$ returned 404"
+};
+
+my $p = strike_search 'Perl', 1;
+is @{$p->file_names}, $p->count, 'file_names has count elements';
This page took 0.014572 seconds and 4 git commands to generate.