Bump version and update Changes
[webservice-strike.git] / lib / WebService / Strike.pm
CommitLineData
27da1d36
MG
1package WebService::Strike;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
7
0eff1663
MG
8our @EXPORT = qw/strike strike_search strike_imdb/; ## no critic (ProhibitAutomaticExportation)
9our @EXPORT_OK = (@EXPORT, 'strike_query');
4757c737 10our $VERSION = '0.004';
0eff1663 11our $BASE_URL = 'https://getstrike.net/api/v2';
27da1d36
MG
12
13use JSON::MaybeXS qw/decode_json/;
14use HTTP::Tiny;
15use Sort::ByExample qw/sbe/;
16use WebService::Strike::Torrent;
17
fceb2d41 18sub _ht { HTTP::Tiny->new(agent => "WebService-Strike/$VERSION", verify_SSL => 1) }
27da1d36 19
9373f0ad
MG
20sub _query {
21 my ($url) = @_;
22
27da1d36
MG
23 my $ht = _ht;
24 my $response = $ht->get($url);
25 die $response unless $response->{success}; ## no critic (RequireCarping)
26 $response = decode_json $response->{content};
27
95b1c120 28 map { WebService::Strike::Torrent->new($_) } @{$response->{torrents}};
9373f0ad
MG
29}
30
31sub strike_query {
32 my (@hashes) = @_;
0e74b120
MG
33 if (@hashes > 50) {
34 return strike_query (@hashes[0 .. 49]), strike_query (@hashes[50 .. $#hashes]);
35 }
0eff1663 36 my $url = "$BASE_URL/torrents/info/?hashes=" . join ',', map { uc } @hashes;
9373f0ad 37
27da1d36 38 my $sorter = sbe(\@hashes, {xform => sub { $_[0]->hash }});
9373f0ad
MG
39 my @torrents = $sorter->(_query $url);
40 wantarray ? @torrents : $torrents[0]
41}
42
43sub strike_search {
44 my ($query, $full, %args) = @_;
95b1c120 45 $args{phrase} = $query;
0eff1663 46 my $url = "$BASE_URL/torrents/search/?" . HTTP::Tiny->www_form_urlencode(\%args);
9373f0ad
MG
47
48 my @torrents = _query $url;
49 @torrents = $torrents[0] unless wantarray;
50 @torrents = strike_query map { $_->hash } @torrents if $full;
27da1d36
MG
51 wantarray ? @torrents : $torrents[0]
52}
53
0eff1663
MG
54sub strike_imdb {
55 my ($id) = @_;
56 my $url = "$BASE_URL/media/imdb/?imdbid=$id";
57 my $response = _ht->get($url);
58 return unless $response->{success};
59 decode_json $response->{content}
60}
61
27da1d36
MG
62BEGIN { *strike = \&strike_query }
63
641;
65__END__
66
67=encoding utf-8
68
69=head1 NAME
70
71WebService::Strike - Get torrent info from getstrike.net API
72
73=head1 SYNOPSIS
74
75 use WebService::Strike;
76 my $t = strike 'B425907E5755031BDA4A8D1B6DCCACA97DA14C04';
77 say $t->title; # Arch Linux 2015.01.01 (x86\/x64)
78 say $t->magnet; # Returns a magnet link
79 my $torrent = $t->torrent; # Returns the torrent file
80 $t->torrent('file.torrent'); # Downloads the torrent file to 'file.torrent'
81
9373f0ad
MG
82 my @debian = strike_search 'Debian';
83 say 'Found ' . @debian . ' torrents matching "Debian"';
84 say 'First torrent has info hash ' . $debian[0]->hash;
85
95b1c120 86 my $mp = strike_search 'Modern perl', 1, category => 'Books';
9373f0ad
MG
87 say 'Torrent has ' . $mp->count . ' files. They are:';
88 say join ' ', @{$mp->file_names};
89
0eff1663
MG
90 my $info = strike_imdb 'tt1520211';
91 say 'IMDB ID ', $info->{imdbID}, ' is ', $info->{title}, ' (', $info->{year}, ')';
92 say 'Plot (short): ', $info->{shortPlot};
93
27da1d36
MG
94=head1 DESCRIPTION
95
96Strike API is a service for getting information about a torrent given
97its info hash. WebService::Strike is a wrapper for this service.
98
99=over
100
101=item B<strike>(I<@info_hashes>)
102
9373f0ad
MG
103Returns a list of L<WebService::Strike::Torrent> objects in list
104context or the first such object in scalar context. Dies in case of
105error.
27da1d36
MG
106
107=item B<strike_query>
108
109Alias for B<strike>. Not exported by default.
110
95b1c120 111=item B<strike_search>(I<$phrase>, [I<$full>, [ key => value ... ]])
9373f0ad 112
95b1c120 113Searches for torrents given a phrase. Returns a list of
9373f0ad
MG
114L<WebService::Strike::Torrent> objects in list context or the first
115such object in scalar context.
116
117If I<$full> is false (default), the returned objects will be
118incomplete: their B<file_names> and B<file_lengths> accessors will
119return undef.
120
121If I<$full> is true, B<strike> will be called with the info hashes of
122the found torrents, thus obtaining complete objects.
123
95b1c120
MG
124You can filter the search by appending key => value pairs to the call.
125For example:
126
127 strike_search 'windows', 0, category => 'Applications', sub_category => 'Windows';
128
0eff1663
MG
129=item B<strike_imdb>(I<$imdb_id>)
130
131Get informaton about a movie from IMDB. Takes an IMDB ID and returns a
132hashef of unspecified format.
133
27da1d36
MG
134=back
135
136=head1 SEE ALSO
137
fceb2d41 138L<WebService::Strike::Torrent>, L<https://getstrike.net/api/>, L<WWW::Search::Torrentz>
27da1d36
MG
139
140=head1 AUTHOR
141
142Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
143
144=head1 COPYRIGHT AND LICENSE
145
146Copyright (C) 2015 by Marius Gavrilescu
147
148This library is free software; you can redistribute it and/or modify
149it under the same terms as Perl itself, either Perl version 5.20.2 or,
150at your option, any later version of Perl 5 you may have available.
151
152
153=cut
This page took 0.019863 seconds and 4 git commands to generate.