]> iEval git - webservice-strike.git/blame - lib/WebService/Strike.pm
Add search support
[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
9373f0ad
MG
8our @EXPORT = qw/strike strike_search/; ## no critic (ProhibitAutomaticExportation)
9our @EXPORT_OK = qw/strike_query strike strike_search/;
57a92368 10our $VERSION = '0.001002';
27da1d36
MG
11our $BASE_URL = 'http://getstrike.net/api/torrents/';
12
13use JSON::MaybeXS qw/decode_json/;
14use HTTP::Tiny;
15use Sort::ByExample qw/sbe/;
16use WebService::Strike::Torrent;
17
18sub _ht { HTTP::Tiny->new(agent => "WebService-Strike/$VERSION") }
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
28 die $response unless ref $response eq 'ARRAY'; ## no critic (RequireCarping)
9373f0ad
MG
29 map { WebService::Strike::Torrent->new($_) } @{$response->[1]};
30}
31
32sub strike_query {
33 my (@hashes) = @_;
34 my $url = "$BASE_URL/info/?hashes=" . join ',', map { uc } @hashes;
35
27da1d36 36 my $sorter = sbe(\@hashes, {xform => sub { $_[0]->hash }});
9373f0ad
MG
37 my @torrents = $sorter->(_query $url);
38 wantarray ? @torrents : $torrents[0]
39}
40
41sub strike_search {
42 my ($query, $full, %args) = @_;
43 $args{q} = $query;
44 my $url = "$BASE_URL/search/?" . HTTP::Tiny->www_form_urlencode(\%args);
45
46 my @torrents = _query $url;
47 @torrents = $torrents[0] unless wantarray;
48 @torrents = strike_query map { $_->hash } @torrents if $full;
27da1d36
MG
49 wantarray ? @torrents : $torrents[0]
50}
51
52BEGIN { *strike = \&strike_query }
53
541;
55__END__
56
57=encoding utf-8
58
59=head1 NAME
60
61WebService::Strike - Get torrent info from getstrike.net API
62
63=head1 SYNOPSIS
64
65 use WebService::Strike;
66 my $t = strike 'B425907E5755031BDA4A8D1B6DCCACA97DA14C04';
67 say $t->title; # Arch Linux 2015.01.01 (x86\/x64)
68 say $t->magnet; # Returns a magnet link
69 my $torrent = $t->torrent; # Returns the torrent file
70 $t->torrent('file.torrent'); # Downloads the torrent file to 'file.torrent'
71
9373f0ad
MG
72 my @debian = strike_search 'Debian';
73 say 'Found ' . @debian . ' torrents matching "Debian"';
74 say 'First torrent has info hash ' . $debian[0]->hash;
75
76 my $mp = strike_search 'Modern perl', 1;
77 say 'Torrent has ' . $mp->count . ' files. They are:';
78 say join ' ', @{$mp->file_names};
79
27da1d36
MG
80=head1 DESCRIPTION
81
82Strike API is a service for getting information about a torrent given
83its info hash. WebService::Strike is a wrapper for this service.
84
85=over
86
87=item B<strike>(I<@info_hashes>)
88
9373f0ad
MG
89Returns a list of L<WebService::Strike::Torrent> objects in list
90context or the first such object in scalar context. Dies in case of
91error.
27da1d36
MG
92
93=item B<strike_query>
94
95Alias for B<strike>. Not exported by default.
96
9373f0ad
MG
97=item B<strike_search>(I<$search_term>, [I<$full>])
98
99Searches for torrents given a search term. Returns a list of
100L<WebService::Strike::Torrent> objects in list context or the first
101such object in scalar context.
102
103If I<$full> is false (default), the returned objects will be
104incomplete: their B<file_names> and B<file_lengths> accessors will
105return undef.
106
107If I<$full> is true, B<strike> will be called with the info hashes of
108the found torrents, thus obtaining complete objects.
109
27da1d36
MG
110=back
111
112=head1 SEE ALSO
113
114L<WebService::Strike::Torrent>, L<http://getstrike.net/api/>, L<WWW::Search::Torrentz>
115
116=head1 AUTHOR
117
118Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
119
120=head1 COPYRIGHT AND LICENSE
121
122Copyright (C) 2015 by Marius Gavrilescu
123
124This library is free software; you can redistribute it and/or modify
125it under the same terms as Perl itself, either Perl version 5.20.2 or,
126at your option, any later version of Perl 5 you may have available.
127
128
129=cut
This page took 0.03695 seconds and 4 git commands to generate.