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