Add description support
[webservice-strike.git] / lib / WebService / Strike / Torrent.pm
1 package WebService::Strike::Torrent;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 use parent qw/Class::Accessor::Fast/;
7
8 our $VERSION = '0.003';
9
10 use Date::Parse qw/str2time/;
11 use JSON::MaybeXS qw/decode_json/;
12 use MIME::Base64;
13 use WebService::Strike;
14
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/);
16
17 BEGIN {
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 *magnet = *magnet_uri;
27 };
28
29 sub new{
30 my ($self, @args) = @_;
31 $self = $self->SUPER::new(@args);
32 $self->{torrent_hash} = uc $self->hash;
33 $self->{upload_date} = str2time $self->date, 'UTC';
34 if ($self->file_info) {
35 $self->{file_names} = $self->file_info->{file_names};
36 $self->{file_lengths} = $self->file_info->{file_lengths};
37 }
38 $self
39 }
40
41 sub torrent{
42 my ($self, $file) = @_;
43 my $url = $WebService::Strike::BASE_URL . '/download/?hash=' . $self->hash;
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
59 sub description{
60 my ($self) = @_;
61 return $self->{description} if $self->{description};
62 my $url = $WebService::Strike::BASE_URL . '/descriptions/?hash=' . $self->hash;
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
69 1;
70 __END__
71
72 =encoding utf-8
73
74 =head1 NAME
75
76 WebService::Strike::Torrent - Class representing information about a torrent
77
78 =head1 SYNOPSIS
79
80 use WebService::Strike;
81 my $t = strike 'B425907E5755031BDA4A8D1B6DCCACA97DA14C04';
82 say $t->hash; # B425907E5755031BDA4A8D1B6DCCACA97DA14C04
83 say $t->title; # Arch Linux 2015.01.01 (x86/x64)
84 say $t->category; # Applications
85 say $t->sub_category; # '' (empty string)
86 say $t->seeds;
87 say $t->leeches;
88 say $t->count; # 1
89 say $t->size; # 615514112
90 say $t->date; # 1420502400
91 say $t->uploader; # The_Doctor-
92 say @{$t->names}; # archlinux-2015.01.01-dual.iso
93 say @{$t->lengths}; # 615514112
94 say $t->magnet; # magnet:?xt=urn:btih:B425907E5755031BDA4A8D1B6DCCACA97DA14C04&dn=Arch%20Linux%202015.01.01%20%28x86%2Fx64%29
95 my $tor = $t->torrent; # $torrent contains the torrent file contents
96 $t->torrent('x.torrent'); # Download torrent file to x.torrent
97 say $t->description; # <HTML fragment describing Arch Linux>
98
99 =head1 DESCRIPTION
100
101 WebService::Strike::Torrent is a class that represents information
102 about a torrent.
103
104 Methods:
105
106 =over
107
108 =item B<hash>, B<torrent_hash>
109
110 The info_hash of the torrent.
111
112 =item B<title>, B<torrent_title>
113
114 The title of the torrent.
115
116 =item B<category>, B<torrent_category>
117
118 The category of the torrent.
119
120 =item B<sub_category>
121
122 The subcategory of the torrent.
123
124 =item B<seeds>
125
126 The number of seeders.
127
128 =item B<leeches>
129
130 The number of leechers.
131
132 =item B<count>, B<file_count>
133
134 The number of files contained in the torrent.
135
136 =item B<size>
137
138 The total size of the files in the torrent in bytes.
139
140 =item B<date>, B<upload_date>
141
142 Unix timestamp when the torrent was uploaded, with precision of one day.
143
144 =item B<uploader>, B<uploader_username>
145
146 Username of the user who uploaded the torrent.
147
148 =item B<file_names>
149
150 Arrayref of paths of files in the torrent.
151
152 =item B<file_lengths>
153
154 Arrayref of lengths of files in the torrent, in bytes.
155
156 =item B<magnet>, B<magnet_uri>
157
158 Magnet link for the torrent.
159
160 =item B<torrent>([I<$filename>])
161
162 Downloads the torrent from Strike. With no arguments, returns the
163 contents of the torrent file. With an argument, stores the torrent in
164 I<$filename>.
165
166 Both forms return a true value for success and false for failure.
167
168 =item B<description>
169
170 The description of the torrent. This method sends an extra request to
171 Strike. Successful responses are cached.
172
173 =back
174
175 =head1 SEE ALSO
176
177 L<WebService::Strike>, L<https://getstrike.net/api/>
178
179 =head1 AUTHOR
180
181 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
182
183 =head1 COPYRIGHT AND LICENSE
184
185 Copyright (C) 2015 by Marius Gavrilescu
186
187 This library is free software; you can redistribute it and/or modify
188 it under the same terms as Perl itself, either Perl version 5.20.2 or,
189 at your option, any later version of Perl 5 you may have available.
190
191
192 =cut
This page took 0.030492 seconds and 4 git commands to generate.