File Coverage

blib/lib/WebService/Strike.pm
Criterion Covered Total %
statement 48 58 82.7
branch 7 14 50.0
condition n/a
subroutine 14 15 93.3
pod 3 3 100.0
total 72 90 80.0


line stmt bran cond sub pod time code
1             package WebService::Strike;
2              
3 2     2   59821 use 5.014000;
  2         7  
4 2     2   10 use strict;
  2         4  
  2         48  
5 2     2   9 use warnings;
  2         3  
  2         59  
6 2     2   669 use parent qw/Exporter/;
  2         284  
  2         12  
7              
8             our @EXPORT = qw/strike strike_search strike_imdb/;
9             our @EXPORT_OK = (@EXPORT, 'strike_query');
10             our $VERSION = '0.005';
11             our $BASE_URL = 'https://getstrike.net/api/v2';
12              
13 2     2   970 use JSON::MaybeXS qw/decode_json/;
  2         6148  
  2         96  
14 2     2   37184 use HTTP::Tiny;
  2         109880  
  2         87  
15 2     2   1694 use Sort::ByExample qw/sbe/;
  2         26926  
  2         13  
16 2     2   1026 use WebService::Strike::Torrent;
  2         5  
  2         21  
17              
18 5     5   43 sub _ht { HTTP::Tiny->new(agent => "WebService-Strike/$VERSION", verify_SSL => 1) }
19              
20             sub _query {
21 4     4   10 my ($url) = @_;
22              
23 4         11 my $ht = _ht;
24 4         417 my $response = $ht->get($url);
25 4 100       1748345 die $response unless $response->{success}; ## no critic (RequireCarping)
26 2         1198 $response = decode_json $response->{content};
27              
28 2         25 map { WebService::Strike::Torrent->new($_) } @{$response->{torrents}};
  103         457  
  2         12  
29             }
30              
31             sub strike_query {
32 2     2 1 4159 my (@hashes) = @_;
33 2 50       10 if (@hashes > 50) {
34 0         0 return strike_query (@hashes[0 .. 49]), strike_query (@hashes[50 .. $#hashes]);
35             }
36 2         9 my $url = "$BASE_URL/torrents/info/?hashes=" . join ',', map { uc } @hashes;
  4         20  
37              
38 2     3   22 my $sorter = sbe(\@hashes, {xform => sub { $_[0]->hash }});
  3         2970  
39 2         93 my @torrents = $sorter->(_query $url);
40 1 50       129 wantarray ? @torrents : $torrents[0]
41             }
42              
43             sub strike_search {
44 2     2 1 4066 my ($query, $full, %args) = @_;
45 2         7 $args{phrase} = $query;
46 2         19 my $url = "$BASE_URL/torrents/search/?" . HTTP::Tiny->www_form_urlencode(\%args);
47              
48 2         141 my @torrents = _query $url;
49 1 50       2094 @torrents = $torrents[0] unless wantarray;
50 1 50       5 @torrents = strike_query map { $_->hash } @torrents if $full;
  0         0  
51 1 50       26 wantarray ? @torrents : $torrents[0]
52             }
53              
54             sub strike_imdb {
55 0     0 1   my ($id) = @_;
56 0           my $url = "$BASE_URL/media/imdb/?imdbid=$id";
57 0           my $response = _ht->get($url);
58 0 0         return unless $response->{success};
59 0           my %imdb = %{decode_json $response->{content}};
  0            
60 0           $imdb{lc $_} = delete $imdb{$_} for keys %imdb; ## no critic (ProhibitUselessTopic)
61 0           \%imdb
62             }
63              
64 2     2   1223 BEGIN { *strike = \&strike_query }
65              
66             1;
67             __END__