]>
Commit | Line | Data |
---|---|---|
27da1d36 MG |
1 | package WebService::Strike; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Exporter/; | |
7 | ||
94cc52f9 | 8 | our @EXPORT = qw/strike strike_search strike_imdb/; |
0eff1663 | 9 | our @EXPORT_OK = (@EXPORT, 'strike_query'); |
acdd89e2 | 10 | our $VERSION = '0.005'; |
0eff1663 | 11 | our $BASE_URL = 'https://getstrike.net/api/v2'; |
27da1d36 MG |
12 | |
13 | use JSON::MaybeXS qw/decode_json/; | |
14 | use HTTP::Tiny; | |
15 | use Sort::ByExample qw/sbe/; | |
16 | use WebService::Strike::Torrent; | |
17 | ||
fceb2d41 | 18 | sub _ht { HTTP::Tiny->new(agent => "WebService-Strike/$VERSION", verify_SSL => 1) } |
27da1d36 | 19 | |
9373f0ad MG |
20 | sub _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 | ||
31 | sub 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 | ||
43 | sub 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 |
54 | sub 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}; | |
35da92a0 MG |
59 | my %imdb = %{decode_json $response->{content}}; |
60 | $imdb{lc $_} = delete $imdb{$_} for keys %imdb; ## no critic (ProhibitUselessTopic) | |
61 | \%imdb | |
0eff1663 MG |
62 | } |
63 | ||
27da1d36 MG |
64 | BEGIN { *strike = \&strike_query } |
65 | ||
66 | 1; | |
67 | __END__ | |
68 | ||
69 | =encoding utf-8 | |
70 | ||
71 | =head1 NAME | |
72 | ||
73 | WebService::Strike - Get torrent info from getstrike.net API | |
74 | ||
75 | =head1 SYNOPSIS | |
76 | ||
77 | use WebService::Strike; | |
78 | my $t = strike 'B425907E5755031BDA4A8D1B6DCCACA97DA14C04'; | |
79 | say $t->title; # Arch Linux 2015.01.01 (x86\/x64) | |
80 | say $t->magnet; # Returns a magnet link | |
81 | my $torrent = $t->torrent; # Returns the torrent file | |
82 | $t->torrent('file.torrent'); # Downloads the torrent file to 'file.torrent' | |
83 | ||
9373f0ad MG |
84 | my @debian = strike_search 'Debian'; |
85 | say 'Found ' . @debian . ' torrents matching "Debian"'; | |
86 | say 'First torrent has info hash ' . $debian[0]->hash; | |
87 | ||
95b1c120 | 88 | my $mp = strike_search 'Modern perl', 1, category => 'Books'; |
9373f0ad MG |
89 | say 'Torrent has ' . $mp->count . ' files. They are:'; |
90 | say join ' ', @{$mp->file_names}; | |
91 | ||
0eff1663 | 92 | my $info = strike_imdb 'tt1520211'; |
35da92a0 MG |
93 | say 'IMDB ID ', $info->{imdbid}, ' is ', $info->{title}, ' (', $info->{year}, ')'; |
94 | say 'Plot (short): ', $info->{shortplot}; | |
0eff1663 | 95 | |
27da1d36 MG |
96 | =head1 DESCRIPTION |
97 | ||
98 | Strike API is a service for getting information about a torrent given | |
99 | its info hash. WebService::Strike is a wrapper for this service. | |
100 | ||
101 | =over | |
102 | ||
103 | =item B<strike>(I<@info_hashes>) | |
104 | ||
9373f0ad MG |
105 | Returns a list of L<WebService::Strike::Torrent> objects in list |
106 | context or the first such object in scalar context. Dies in case of | |
107 | error. | |
27da1d36 MG |
108 | |
109 | =item B<strike_query> | |
110 | ||
111 | Alias for B<strike>. Not exported by default. | |
112 | ||
95b1c120 | 113 | =item B<strike_search>(I<$phrase>, [I<$full>, [ key => value ... ]]) |
9373f0ad | 114 | |
95b1c120 | 115 | Searches for torrents given a phrase. Returns a list of |
9373f0ad MG |
116 | L<WebService::Strike::Torrent> objects in list context or the first |
117 | such object in scalar context. | |
118 | ||
119 | If I<$full> is false (default), the returned objects will be | |
120 | incomplete: their B<file_names> and B<file_lengths> accessors will | |
121 | return undef. | |
122 | ||
123 | If I<$full> is true, B<strike> will be called with the info hashes of | |
124 | the found torrents, thus obtaining complete objects. | |
125 | ||
95b1c120 MG |
126 | You can filter the search by appending key => value pairs to the call. |
127 | For example: | |
128 | ||
129 | strike_search 'windows', 0, category => 'Applications', sub_category => 'Windows'; | |
130 | ||
0eff1663 MG |
131 | =item B<strike_imdb>(I<$imdb_id>) |
132 | ||
133 | Get informaton about a movie from IMDB. Takes an IMDB ID and returns a | |
35da92a0 | 134 | hashref of unspecified format. All keys are lowercased. |
0eff1663 | 135 | |
27da1d36 MG |
136 | =back |
137 | ||
138 | =head1 SEE ALSO | |
139 | ||
fceb2d41 | 140 | L<WebService::Strike::Torrent>, L<https://getstrike.net/api/>, L<WWW::Search::Torrentz> |
27da1d36 MG |
141 | |
142 | =head1 AUTHOR | |
143 | ||
144 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
145 | ||
146 | =head1 COPYRIGHT AND LICENSE | |
147 | ||
acdd89e2 | 148 | Copyright (C) 2015-2016 by Marius Gavrilescu |
27da1d36 MG |
149 | |
150 | This library is free software; you can redistribute it and/or modify | |
151 | it under the same terms as Perl itself, either Perl version 5.20.2 or, | |
152 | at your option, any later version of Perl 5 you may have available. | |
153 | ||
154 | ||
155 | =cut |