Update to API v2
authorMarius Gavrilescu <marius@ieval.ro>
Sat, 28 Mar 2015 12:38:07 +0000 (14:38 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Sat, 28 Mar 2015 12:43:28 +0000 (14:43 +0200)
Makefile.PL
lib/WebService/Strike.pm
lib/WebService/Strike/Torrent.pm
t/WebService-Strike-Torrent.t

index 7e87bbcd9f529ca932f54e98e11ad6d25a3fc4b9..a3d233e6e229269ce50138097fcddc1d6e5cf575 100644 (file)
@@ -17,8 +17,7 @@ WriteMakefile(
                qw/Class::Accessor::Fast 0
                   Date::Parse           0
                   JSON::MaybeXS         0
-                  Sort::ByExample       0
-                  URI::Escape           0/,
+                  Sort::ByExample       0/,
        },
        META_ADD          => {
                dynamic_config => 0,
index 0b32f1170b3ced717e871b15c9f79f30171bae98..396ec48676ba61990710417fc734ec9d17032f99 100644 (file)
@@ -8,7 +8,7 @@ use parent qw/Exporter/;
 our @EXPORT    = qw/strike strike_search/; ## no critic (ProhibitAutomaticExportation)
 our @EXPORT_OK = qw/strike_query strike strike_search/;
 our $VERSION = '0.002';
-our $BASE_URL = 'http://getstrike.net/api/torrents/';
+our $BASE_URL = 'http://getstrike.net/api/v2/torrents';
 
 use JSON::MaybeXS qw/decode_json/;
 use HTTP::Tiny;
@@ -25,8 +25,7 @@ sub _query {
        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]};
+       map { WebService::Strike::Torrent->new($_) } @{$response->{torrents}};
 }
 
 sub strike_query {
@@ -40,7 +39,7 @@ sub strike_query {
 
 sub strike_search {
        my ($query, $full, %args) = @_;
-       $args{q} = $query;
+       $args{phrase} = $query;
        my $url = "$BASE_URL/search/?" . HTTP::Tiny->www_form_urlencode(\%args);
 
        my @torrents = _query $url;
@@ -73,7 +72,7 @@ WebService::Strike - Get torrent info from getstrike.net API
   say 'Found ' . @debian . ' torrents matching "Debian"';
   say 'First torrent has info hash ' . $debian[0]->hash;
 
-  my $mp = strike_search 'Modern perl', 1;
+  my $mp = strike_search 'Modern perl', 1, category => 'Books';
   say 'Torrent has ' . $mp->count . ' files. They are:';
   say join ' ', @{$mp->file_names};
 
@@ -94,9 +93,9 @@ error.
 
 Alias for B<strike>. Not exported by default.
 
-=item B<strike_search>(I<$search_term>, [I<$full>])
+=item B<strike_search>(I<$phrase>, [I<$full>, [ key => value ... ]])
 
-Searches for torrents given a search term. Returns a list of
+Searches for torrents given a phrase. Returns a list of
 L<WebService::Strike::Torrent> objects in list context or the first
 such object in scalar context.
 
@@ -107,6 +106,11 @@ return undef.
 If I<$full> is true, B<strike> will be called with the info hashes of
 the found torrents, thus obtaining complete objects.
 
+You can filter the search by appending key => value pairs to the call.
+For example:
+
+  strike_search 'windows', 0, category => 'Applications', sub_category => 'Windows';
+
 =back
 
 =head1 SEE ALSO
index 33d4b515f9f747759204d6ed9f9da3dfe565639a..1f0dae5e359b9c883dc31fba5738a4b43fc8ced5 100644 (file)
@@ -9,10 +9,9 @@ our $VERSION = '0.002';
 
 use Date::Parse qw/str2time/;
 use JSON::MaybeXS qw/decode_json/;
-use URI::Escape qw/uri_escape/;
 use WebService::Strike;
 
-__PACKAGE__->mk_ro_accessors(qw/torrent_hash torrent_title torrent_category sub_category seeds leeches file_count size upload_date uploader_username file_info file_names file_lengths/);
+__PACKAGE__->mk_ro_accessors(qw/torrent_hash torrent_title torrent_category sub_category seeds leeches file_count size upload_date uploader_username file_info file_names file_lengths magnet_uri/);
 
 BEGIN {
        *hash     = *torrent_hash;
@@ -23,6 +22,7 @@ BEGIN {
        *uploader = *uploader_username;
        *names    = *file_names;
        *lengths  = *file_lengths;
+       *magnet   = *magnet_uri;
 };
 
 sub new{
@@ -31,20 +31,15 @@ sub new{
        $self->{torrent_hash} = uc $self->hash;
        $self->{upload_date} = str2time $self->date, 'UTC';
        if ($self->file_info) {
-               $self->{file_names} = $self->file_info->[0]->{file_names};
-               $self->{file_lengths} = $self->file_info->[0]->{file_lengths};
+               $self->{file_names} = $self->file_info->{file_names};
+               $self->{file_lengths} = $self->file_info->{file_lengths};
        }
        $self
 }
 
-sub magnet{
-       my ($self) = @_;
-       'magnet:?xt=urn:btih:' . $self->hash . '&dn=' . uri_escape $self->title
-}
-
 sub torrent{
        my ($self, $file) = @_;
-       my $url = $WebService::Strike::BASE_URL . '/downloads/?hash=' . $self->hash;
+       my $url = $WebService::Strike::BASE_URL . '/download/?hash=' . $self->hash;
        my $ht = WebService::Strike::_ht(); ## no critic (ProtectPrivate)
        my $response = $ht->get($url);
        return unless $response->{success};
@@ -80,7 +75,7 @@ WebService::Strike::Torrent - Class representing information about a torrent
   say $t->seeds;
   say $t->leeches;
   say $t->count;            # 1
-  say $t->size;             # 587 MB
+  say $t->size;             # 615514112
   say $t->date;             # 1420502400
   say $t->uploader;         # The_Doctor-
   say @{$t->names};         # archlinux-2015.01.01-dual.iso
@@ -128,8 +123,7 @@ The number of files contained in the torrent.
 
 =item B<size>
 
-The total size of the files in the torrent as a human-readable string.
-See B<file_lengths> for exact sizes.
+The total size of the files in the torrent in bytes.
 
 =item B<date>, B<upload_date>
 
@@ -147,7 +141,7 @@ Arrayref of paths of files in the torrent.
 
 Arrayref of lengths of files in the torrent, in bytes.
 
-=item B<magnet>
+=item B<magnet>, B<magnet_uri>
 
 Magnet link for the torrent.
 
index ce57b0b347164db04c1c2fccdf07bc043ae6a85d..cde5c7f3cdd0844ef0bfa6a0e027375c57ce559e 100644 (file)
@@ -7,18 +7,19 @@ BEGIN { use_ok('WebService::Strike::Torrent') }
 
 my %data = (
        leeches => 13,
-       size => '587 MB',
-       torrent_hash => 'b425907e5755031bda4a8d1b6dccaca97da14c04',
+       size => 615514112,
+       torrent_hash => 'B425907E5755031BDA4A8D1B6DCCACA97DA14C04',
        file_count => 1,
        sub_category => '',
        torrent_category => 'Applications',
-       file_info => [{
+       file_info => {
                file_names => [ 'archlinux-2015.01.01-dual.iso' ],
                file_lengths => [ 615514112 ],
-       }],
+       },
        upload_date => 'Jan  6, 2015',
        seeds => 645,
        uploader_username => 'The_Doctor-',
+       magnet_uri => 'magnet:?xt=urn:btih:B425907E5755031BDA4A8D1B6DCCACA97DA14C04&dn=Arch+Linux+2015.01.01+%28x86%2Fx64%29&tr=udp://open.demonii.com:1337&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://exodus.desync.com:6969',
        torrent_title => 'Arch Linux 2015.01.01 (x86/x64)'
 );
 
@@ -31,9 +32,9 @@ is $t->sub_category, '', 'sub_category';
 is $t->seeds, 645, 'seeds';
 is $t->leeches, 13, 'leeches';
 is $t->count, 1, 'count';
-is $t->size, '587 MB', 'size';
+is $t->size, 615514112, 'size';
 is $t->date, 1420502400,'date';
 is $t->uploader, 'The_Doctor-', 'uploader';
 is $t->names->[0], 'archlinux-2015.01.01-dual.iso', 'names';
 is $t->lengths->[0], 615514112, 'lengths';
-is $t->magnet, 'magnet:?xt=urn:btih:B425907E5755031BDA4A8D1B6DCCACA97DA14C04&dn=Arch%20Linux%202015.01.01%20%28x86%2Fx64%29', 'magnet';
+is $t->magnet, 'magnet:?xt=urn:btih:B425907E5755031BDA4A8D1B6DCCACA97DA14C04&dn=Arch+Linux+2015.01.01+%28x86%2Fx64%29&tr=udp://open.demonii.com:1337&tr=udp://tracker.coppersurfer.tk:6969&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://exodus.desync.com:6969', 'magnet';
This page took 0.01758 seconds and 4 git commands to generate.