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
b1241fec 8our $VERSION = '0.003';
27da1d36
MG
9
10use Date::Parse qw/str2time/;
11use JSON::MaybeXS qw/decode_json/;
27da1d36
MG
12use WebService::Strike;
13
95b1c120 14__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/);
27da1d36
MG
15
16BEGIN {
17 *hash = *torrent_hash;
18 *title = *torrent_title;
19 *category = *torrent_category;
20 *count = *file_count;
21 *date = *upload_date;
22 *uploader = *uploader_username;
23 *names = *file_names;
24 *lengths = *file_lengths;
95b1c120 25 *magnet = *magnet_uri;
27da1d36
MG
26};
27
28sub new{
29 my ($self, @args) = @_;
30 $self = $self->SUPER::new(@args);
31 $self->{torrent_hash} = uc $self->hash;
f447922d 32 $self->{upload_date} = str2time $self->date, 'UTC';
9373f0ad 33 if ($self->file_info) {
95b1c120
MG
34 $self->{file_names} = $self->file_info->{file_names};
35 $self->{file_lengths} = $self->file_info->{file_lengths};
9373f0ad 36 }
27da1d36
MG
37 $self
38}
39
27da1d36
MG
40sub torrent{
41 my ($self, $file) = @_;
95b1c120 42 my $url = $WebService::Strike::BASE_URL . '/download/?hash=' . $self->hash;
27da1d36
MG
43 my $ht = WebService::Strike::_ht(); ## no critic (ProtectPrivate)
44 my $response = $ht->get($url);
45 return unless $response->{success};
46 $response = decode_json $response->{content};
47 $url = $response->{message};
48
49 if (defined $file) {
50 $response = $ht->mirror($url, $file);
51 return $response->{success}
52 } else {
53 $response = $ht->get($url);
54 return $response->{success} && $response->{content}
55 }
56}
57
581;
59__END__
60
61=encoding utf-8
62
63=head1 NAME
64
65WebService::Strike::Torrent - Class representing information about a torrent
66
67=head1 SYNOPSIS
68
69 use WebService::Strike;
70 my $t = strike 'B425907E5755031BDA4A8D1B6DCCACA97DA14C04';
71 say $t->hash; # B425907E5755031BDA4A8D1B6DCCACA97DA14C04
72 say $t->title; # Arch Linux 2015.01.01 (x86/x64)
73 say $t->category; # Applications
74 say $t->sub_category; # '' (empty string)
75 say $t->seeds;
76 say $t->leeches;
77 say $t->count; # 1
95b1c120 78 say $t->size; # 615514112
f447922d 79 say $t->date; # 1420502400
27da1d36
MG
80 say $t->uploader; # The_Doctor-
81 say @{$t->names}; # archlinux-2015.01.01-dual.iso
82 say @{$t->lengths}; # 615514112
83 say $t->magnet; # magnet:?xt=urn:btih:B425907E5755031BDA4A8D1B6DCCACA97DA14C04&dn=Arch%20Linux%202015.01.01%20%28x86%2Fx64%29
84 my $tor = $t->torrent; # $torrent contains the torrent file contents
85 $t->torrent('x.torrent'); # Download torrent file to x.torrent
86
87=head1 DESCRIPTION
88
89WebService::Strike::Torrent is a class that represents information
90about a torrent.
91
92Methods:
93
94=over
95
96=item B<hash>, B<torrent_hash>
97
98The info_hash of the torrent.
99
100=item B<title>, B<torrent_title>
101
102The title of the torrent.
103
104=item B<category>, B<torrent_category>
105
106The category of the torrent.
107
108=item B<sub_category>
109
110The subcategory of the torrent.
111
112=item B<seeds>
113
114The number of seeders.
115
116=item B<leeches>
117
118The number of leechers.
119
120=item B<count>, B<file_count>
121
122The number of files contained in the torrent.
123
124=item B<size>
125
95b1c120 126The total size of the files in the torrent in bytes.
27da1d36
MG
127
128=item B<date>, B<upload_date>
129
130Unix timestamp when the torrent was uploaded, with precision of one day.
131
132=item B<uploader>, B<uploader_username>
133
134Username of the user who uploaded the torrent.
135
136=item B<file_names>
137
138Arrayref of paths of files in the torrent.
139
140=item B<file_lengths>
141
142Arrayref of lengths of files in the torrent, in bytes.
143
95b1c120 144=item B<magnet>, B<magnet_uri>
27da1d36
MG
145
146Magnet link for the torrent.
147
148=item B<torrent>([I<$filename>])
149
150Downloads the torrent from Strike. With no arguments, returns the
151contents of the torrent file. With an argument, stores the torrent in
152I<$filename>.
153
154Both forms return a true value for success and false for failure.
155
156=back
157
158=head1 SEE ALSO
159
160L<WebService::Strike>, L<http://getstrike.net/api/>
161
162=head1 AUTHOR
163
164Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
165
166=head1 COPYRIGHT AND LICENSE
167
168Copyright (C) 2015 by Marius Gavrilescu
169
170This library is free software; you can redistribute it and/or modify
171it under the same terms as Perl itself, either Perl version 5.20.2 or,
172at your option, any later version of Perl 5 you may have available.
173
174
175=cut
This page took 0.02003 seconds and 4 git commands to generate.