Bump version and update Changes
[webservice-strike.git] / lib / WebService / Strike / Torrent.pm
CommitLineData
27da1d36
MG
1package WebService::Strike::Torrent;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Class::Accessor::Fast/;
7
acdd89e2 8our $VERSION = 0.005;
27da1d36 9
27da1d36 10use JSON::MaybeXS qw/decode_json/;
d932afd8 11use MIME::Base64;
94cc52f9 12use URI::Escape;
27da1d36
MG
13use WebService::Strike;
14
94cc52f9 15__PACKAGE__->mk_ro_accessors(qw/torrent_hash torrent_title torrent_category sub_category seeds leeches file_count size upload_date uploader_username file_info file_names file_lengths imdb_id/);
27da1d36
MG
16
17BEGIN {
18 *hash = *torrent_hash;
19 *title = *torrent_title;
20 *category = *torrent_category;
21 *count = *file_count;
22 *date = *upload_date;
23 *uploader = *uploader_username;
24 *names = *file_names;
25 *lengths = *file_lengths;
26};
27
94cc52f9
MG
28sub magnet{
29 my ($self) = @_;
30 my $hash = uri_escape $self->hash; # uri_escape is not exactly needed here
31 my $title = uri_escape $self->title;
32 "magnet:?xt=urn:btih:$hash&dn=$title"
33}
34
27da1d36
MG
35sub new{
36 my ($self, @args) = @_;
37 $self = $self->SUPER::new(@args);
38 $self->{torrent_hash} = uc $self->hash;
9373f0ad 39 if ($self->file_info) {
95b1c120
MG
40 $self->{file_names} = $self->file_info->{file_names};
41 $self->{file_lengths} = $self->file_info->{file_lengths};
9373f0ad 42 }
c3c68df9 43 $self->{imdb_id} = $self->{imdbid} if $self->{imdbid};
27da1d36
MG
44 $self
45}
46
27da1d36 47sub torrent{
94cc52f9 48 die "This API call was removed in Strike API V2.1\n"
27da1d36
MG
49}
50
d932afd8
MG
51sub description{
52 my ($self) = @_;
53 return $self->{description} if $self->{description};
0eff1663 54 my $url = $WebService::Strike::BASE_URL . '/torrents/descriptions/?hash=' . $self->hash;
d932afd8
MG
55 my $ht = WebService::Strike::_ht(); ## no critic (ProtectPrivate)
56 my $response = $ht->get($url);
57 return unless $response->{success};
58 $self->{description} = decode_base64 $response->{content}
59}
60
0eff1663
MG
61sub imdb {
62 my ($self) = @_;
94cc52f9 63 return if !$self->imdb_id || $self->imdb_id eq 'none';
0eff1663
MG
64 $self->{imdb} //= WebService::Strike::strike_imdb ($self->imdb_id)
65}
66
27da1d36
MG
671;
68__END__
69
70=encoding utf-8
71
72=head1 NAME
73
74WebService::Strike::Torrent - Class representing information about a torrent
75
76=head1 SYNOPSIS
77
78 use WebService::Strike;
79 my $t = strike 'B425907E5755031BDA4A8D1B6DCCACA97DA14C04';
80 say $t->hash; # B425907E5755031BDA4A8D1B6DCCACA97DA14C04
81 say $t->title; # Arch Linux 2015.01.01 (x86/x64)
82 say $t->category; # Applications
83 say $t->sub_category; # '' (empty string)
84 say $t->seeds;
85 say $t->leeches;
86 say $t->count; # 1
95b1c120 87 say $t->size; # 615514112
f447922d 88 say $t->date; # 1420502400
27da1d36
MG
89 say $t->uploader; # The_Doctor-
90 say @{$t->names}; # archlinux-2015.01.01-dual.iso
91 say @{$t->lengths}; # 615514112
92 say $t->magnet; # magnet:?xt=urn:btih:B425907E5755031BDA4A8D1B6DCCACA97DA14C04&dn=Arch%20Linux%202015.01.01%20%28x86%2Fx64%29
d932afd8 93 say $t->description; # <HTML fragment describing Arch Linux>
27da1d36 94
0eff1663
MG
95 $t = strike 'ED70C185E3E3246F30B2FDB08D504EABED5EEA3F';
96 say $t->title; # The Walking Dead S04E15 HDTV x264-2HD
97 say $t->imdb_id; # tt1520211
98 my $i = $t->imdb;
99 say $i->{title}, ' (', $i->{year}, ')'; # The Walking Dead (2010)
100 say $i->{genre}; # Drama, Horror, Thriller
101
27da1d36
MG
102=head1 DESCRIPTION
103
104WebService::Strike::Torrent is a class that represents information
105about a torrent.
106
107Methods:
108
109=over
110
111=item B<hash>, B<torrent_hash>
112
113The info_hash of the torrent.
114
115=item B<title>, B<torrent_title>
116
117The title of the torrent.
118
119=item B<category>, B<torrent_category>
120
121The category of the torrent.
122
123=item B<sub_category>
124
125The subcategory of the torrent.
126
127=item B<seeds>
128
129The number of seeders.
130
131=item B<leeches>
132
133The number of leechers.
134
135=item B<count>, B<file_count>
136
137The number of files contained in the torrent.
138
139=item B<size>
140
95b1c120 141The total size of the files in the torrent in bytes.
27da1d36
MG
142
143=item B<date>, B<upload_date>
144
145Unix timestamp when the torrent was uploaded, with precision of one day.
146
147=item B<uploader>, B<uploader_username>
148
149Username of the user who uploaded the torrent.
150
151=item B<file_names>
152
153Arrayref of paths of files in the torrent.
154
155=item B<file_lengths>
156
157Arrayref of lengths of files in the torrent, in bytes.
158
95b1c120 159=item B<magnet>, B<magnet_uri>
27da1d36
MG
160
161Magnet link for the torrent.
162
163=item B<torrent>([I<$filename>])
164
94cc52f9
MG
165B<THIS METHOD WAS REMOVED IN STRIKE API V2.1>. Therefore, it simply
166dies. Below is the previous documentation of the method.
167
27da1d36
MG
168Downloads the torrent from Strike. With no arguments, returns the
169contents of the torrent file. With an argument, stores the torrent in
170I<$filename>.
171
172Both forms return a true value for success and false for failure.
173
d932afd8
MG
174=item B<description>
175
176The description of the torrent. This method sends an extra request to
177Strike. Successful responses are cached.
178
0eff1663
MG
179=item B<imdb_id>
180
181The IMDB ID of the torrent, or undef if the torrent has no associated
182IMDB ID.
183
184=item B<imdb>
185
186Calls B<strike_imdb> from L<WebService::Strike> on B<imdb_id>. Caches
187the response. Returns undef if the torrent has no associated IMDB ID.
188
27da1d36
MG
189=back
190
191=head1 SEE ALSO
192
fceb2d41 193L<WebService::Strike>, L<https://getstrike.net/api/>
27da1d36
MG
194
195=head1 AUTHOR
196
197Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
198
199=head1 COPYRIGHT AND LICENSE
200
acdd89e2 201Copyright (C) 2015-2016 by Marius Gavrilescu
27da1d36
MG
202
203This library is free software; you can redistribute it and/or modify
204it under the same terms as Perl itself, either Perl version 5.20.2 or,
205at your option, any later version of Perl 5 you may have available.
206
207
208=cut
This page took 0.025655 seconds and 4 git commands to generate.