X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FWebService%2FStrike.pm;h=58d43c25c55cd046240a6d6b442b0d009f8f92ba;hb=543685fd4bd3f38835f9269d5a6b51ae39076787;hp=56d1c474d5dafe04de1ae05c4f2ef954d5126494;hpb=57a92368ec29dcc77fafe5b46b960919fbed7d81;p=webservice-strike.git diff --git a/lib/WebService/Strike.pm b/lib/WebService/Strike.pm index 56d1c47..58d43c2 100644 --- a/lib/WebService/Strike.pm +++ b/lib/WebService/Strike.pm @@ -5,33 +5,62 @@ use strict; use warnings; use parent qw/Exporter/; -our @EXPORT = qw/strike/; ## no critic (ProhibitAutomaticExportation) -our @EXPORT_OK = qw/strike_query strike/; -our $VERSION = '0.001002'; -our $BASE_URL = 'http://getstrike.net/api/torrents/'; +our @EXPORT = qw/strike strike_search strike_imdb/; ## no critic (ProhibitAutomaticExportation) +our @EXPORT_OK = (@EXPORT, 'strike_query'); +our $VERSION = '0.004003'; +our $BASE_URL = 'https://getstrike.net/api/v2'; use JSON::MaybeXS qw/decode_json/; use HTTP::Tiny; use Sort::ByExample qw/sbe/; use WebService::Strike::Torrent; -sub _ht { HTTP::Tiny->new(agent => "WebService-Strike/$VERSION") } +sub _ht { HTTP::Tiny->new(agent => "WebService-Strike/$VERSION", verify_SSL => 1) } + +sub _query { + my ($url) = @_; -sub strike_query { - my (@hashes) = @_; - my $url = "$BASE_URL/info/?hashes=" . join ',', map { uc } @hashes; 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->{torrents}}; +} + +sub strike_query { + my (@hashes) = @_; + if (@hashes > 50) { + return strike_query (@hashes[0 .. 49]), strike_query (@hashes[50 .. $#hashes]); + } + my $url = "$BASE_URL/torrents/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{phrase} = $query; + my $url = "$BASE_URL/torrents/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] } +sub strike_imdb { + my ($id) = @_; + my $url = "$BASE_URL/media/imdb/?imdbid=$id"; + my $response = _ht->get($url); + return unless $response->{success}; + my %imdb = %{decode_json $response->{content}}; + $imdb{lc $_} = delete $imdb{$_} for keys %imdb; ## no critic (ProhibitUselessTopic) + \%imdb +} + BEGIN { *strike = \&strike_query } 1; @@ -52,6 +81,18 @@ 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, category => 'Books'; + say 'Torrent has ' . $mp->count . ' files. They are:'; + say join ' ', @{$mp->file_names}; + + my $info = strike_imdb 'tt1520211'; + say 'IMDB ID ', $info->{imdbid}, ' is ', $info->{title}, ' (', $info->{year}, ')'; + say 'Plot (short): ', $info->{shortplot}; + =head1 DESCRIPTION Strike API is a service for getting information about a torrent given @@ -61,19 +102,42 @@ its info hash. WebService::Strike is a wrapper for this service. =item B(I<@info_hashes>) -Returns a list of hashrefs, one for each info hash. The hashrefs are -blessed into the L package. Dies in case -of error. +Returns a list of L objects in list +context or the first such object in scalar context. Dies in case of +error. =item B Alias for B. Not exported by default. +=item B(I<$phrase>, [I<$full>, [ key => value ... ]]) + +Searches for torrents given a phrase. Returns a list of +L 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 and B accessors will +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'; + +=item B(I<$imdb_id>) + +Get informaton about a movie from IMDB. Takes an IMDB ID and returns a +hashref of unspecified format. All keys are lowercased. + =back =head1 SEE ALSO -L, L, L +L, L, L =head1 AUTHOR