From 95b1c120488fa6a756ab596060099f85921dae73 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 28 Mar 2015 14:38:07 +0200 Subject: [PATCH] Update to API v2 --- Makefile.PL | 3 +-- lib/WebService/Strike.pm | 18 +++++++++++------- lib/WebService/Strike/Torrent.pm | 22 ++++++++-------------- t/WebService-Strike-Torrent.t | 13 +++++++------ 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 7e87bbc..a3d233e 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -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, diff --git a/lib/WebService/Strike.pm b/lib/WebService/Strike.pm index 0b32f11..396ec48 100644 --- a/lib/WebService/Strike.pm +++ b/lib/WebService/Strike.pm @@ -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. Not exported by default. -=item B(I<$search_term>, [I<$full>]) +=item B(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 objects in list context or the first such object in scalar context. @@ -107,6 +106,11 @@ return undef. If I<$full> is true, B 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 diff --git a/lib/WebService/Strike/Torrent.pm b/lib/WebService/Strike/Torrent.pm index 33d4b51..1f0dae5 100644 --- a/lib/WebService/Strike/Torrent.pm +++ b/lib/WebService/Strike/Torrent.pm @@ -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 -The total size of the files in the torrent as a human-readable string. -See B for exact sizes. +The total size of the files in the torrent in bytes. =item B, B @@ -147,7 +141,7 @@ Arrayref of paths of files in the torrent. Arrayref of lengths of files in the torrent, in bytes. -=item B +=item B, B Magnet link for the torrent. diff --git a/t/WebService-Strike-Torrent.t b/t/WebService-Strike-Torrent.t index ce57b0b..cde5c7f 100644 --- a/t/WebService-Strike-Torrent.t +++ b/t/WebService-Strike-Torrent.t @@ -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'; -- 2.39.2