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