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