]>
Commit | Line | Data |
---|---|---|
1 | package WebService::Strike; | |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Exporter/; | |
7 | ||
8 | our @EXPORT = qw/strike/; ## no critic (ProhibitAutomaticExportation) | |
9 | our @EXPORT_OK = qw/strike_query strike/; | |
10 | our $VERSION = '0.001001'; | |
11 | our $BASE_URL = 'http://getstrike.net/api/torrents/'; | |
12 | ||
13 | use JSON::MaybeXS qw/decode_json/; | |
14 | use HTTP::Tiny; | |
15 | use Sort::ByExample qw/sbe/; | |
16 | use WebService::Strike::Torrent; | |
17 | ||
18 | sub _ht { HTTP::Tiny->new(agent => "WebService-Strike/$VERSION") } | |
19 | ||
20 | sub strike_query { | |
21 | my (@hashes) = @_; | |
22 | my $url = "$BASE_URL/info/?hashes=" . join ',', map { uc } @hashes; | |
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 | ||
28 | die $response unless ref $response eq 'ARRAY'; ## no critic (RequireCarping) | |
29 | my $sorter = sbe(\@hashes, {xform => sub { $_[0]->hash }}); | |
30 | my @torrents = map { WebService::Strike::Torrent->new($_) } @{$response->[1]}; | |
31 | @torrents = $sorter->(@torrents); | |
32 | wantarray ? @torrents : $torrents[0] | |
33 | } | |
34 | ||
35 | BEGIN { *strike = \&strike_query } | |
36 | ||
37 | 1; | |
38 | __END__ | |
39 | ||
40 | =encoding utf-8 | |
41 | ||
42 | =head1 NAME | |
43 | ||
44 | WebService::Strike - Get torrent info from getstrike.net API | |
45 | ||
46 | =head1 SYNOPSIS | |
47 | ||
48 | use WebService::Strike; | |
49 | my $t = strike 'B425907E5755031BDA4A8D1B6DCCACA97DA14C04'; | |
50 | say $t->title; # Arch Linux 2015.01.01 (x86\/x64) | |
51 | say $t->magnet; # Returns a magnet link | |
52 | my $torrent = $t->torrent; # Returns the torrent file | |
53 | $t->torrent('file.torrent'); # Downloads the torrent file to 'file.torrent' | |
54 | ||
55 | =head1 DESCRIPTION | |
56 | ||
57 | Strike API is a service for getting information about a torrent given | |
58 | its info hash. WebService::Strike is a wrapper for this service. | |
59 | ||
60 | =over | |
61 | ||
62 | =item B<strike>(I<@info_hashes>) | |
63 | ||
64 | Returns a list of hashrefs, one for each info hash. The hashrefs are | |
65 | blessed into the L<WebService::Strike::Torrent> package. Dies in case | |
66 | of error. | |
67 | ||
68 | =item B<strike_query> | |
69 | ||
70 | Alias for B<strike>. Not exported by default. | |
71 | ||
72 | =back | |
73 | ||
74 | =head1 SEE ALSO | |
75 | ||
76 | L<WebService::Strike::Torrent>, L<http://getstrike.net/api/>, L<WWW::Search::Torrentz> | |
77 | ||
78 | =head1 AUTHOR | |
79 | ||
80 | Marius Gavrilescu, E<lt>marius@ieval.roE<gt> | |
81 | ||
82 | =head1 COPYRIGHT AND LICENSE | |
83 | ||
84 | Copyright (C) 2015 by Marius Gavrilescu | |
85 | ||
86 | This library is free software; you can redistribute it and/or modify | |
87 | it under the same terms as Perl itself, either Perl version 5.20.2 or, | |
88 | at your option, any later version of Perl 5 you may have available. | |
89 | ||
90 | ||
91 | =cut |