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