| 1 | package Net::isoHunt::Request; | ||||||||||
| 2 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | $Net::isoHunt::Request::VERSION = '0.102770'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: Populates request fields and executes request | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use JSON qw{ decode_json }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 2 | has 'ihq' => ( | |||||||||
| 16 | is => 'rw', | ||||||||||
| 17 | isa => 'Str', | ||||||||||
| 18 | required => 1, | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | 2 | has 'start' => ( | |||||||||
| 22 | is => 'rw', | ||||||||||
| 23 | isa => 'Int', | ||||||||||
| 24 | default => 1, | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | 2 | has 'rows' => ( | |||||||||
| 28 | is => 'rw', | ||||||||||
| 29 | isa => 'Int', | ||||||||||
| 30 | default => 100, | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 2 | has 'sort' => ( | |||||||||
| 34 | is => 'rw', | ||||||||||
| 35 | isa => enum( [ qw{ seeds age size } ] ), | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 2 | has 'order' => ( | |||||||||
| 39 | is => 'rw', | ||||||||||
| 40 | isa => enum( [ qw{ asc desc } ] ), | ||||||||||
| 41 | default => 'desc', | ||||||||||
| 42 | ); | ||||||||||
| 43 | |||||||||||
| 44 | 1 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: called CC=4 SCAR=17.0
Pod
documented
| |||||||||||
| 45 | 1 | my $self = shift; | |||||||||
| 46 | |||||||||||
| 47 | 1 | my $api_url = 'http://isohunt.com/js/json.php'; | |||||||||
| 48 | 1 | my %parametres = ( | |||||||||
| 49 | 'ihq' => $self->ihq (), | ||||||||||
| 50 | 'start' => $self->start(), | ||||||||||
| 51 | 'rows' => $self->rows (), | ||||||||||
| 52 | 'sort' => $self->sort (), | ||||||||||
| 53 | 'order' => $self->order(), | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | 1 | for my $key ( keys %parametres ) { | |||||||||
| 57 | 5 | ▶ | !defined $parametres{$key} and delete $parametres{$key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | my $uri = URI->new($api_url); | |||||||||
| 61 | 1 | $uri->query_form(%parametres); | |||||||||
| 62 | |||||||||||
| 63 | 1 | my $ua = LWP::UserAgent->new( 'agent' => 'Net::isoHunt' ); | |||||||||
| 64 | 1 | my $json = $ua->get($uri)->decoded_content(); | |||||||||
| 65 | 1 | my $json_decoded = decode_json($json); | |||||||||
| 66 | |||||||||||
| 67 | my $image = Net::isoHunt::Response::Image->new( { | ||||||||||
| 68 | 'title' => $json_decoded->{'image'}->{'title' }, | ||||||||||
| 69 | 'url' => $json_decoded->{'image'}->{'url' }, | ||||||||||
| 70 | 'link' => $json_decoded->{'image'}->{'link' }, | ||||||||||
| 71 | 'width' => $json_decoded->{'image'}->{'width' }, | ||||||||||
| 72 | 0 | 'height' => $json_decoded->{'image'}->{'height' }, | |||||||||
| 73 | } ); | ||||||||||
| 74 | |||||||||||
| 75 | 0 | my @items; | |||||||||
| 76 | 0 | for ( @{ $json_decoded->{'items'}->{'list'} } ) { | |||||||||
| 77 | my $item = Net::isoHunt::Response::Item->new( { | ||||||||||
| 78 | 'title' => $_->{'title' }, | ||||||||||
| 79 | 'link' => $_->{'link' }, | ||||||||||
| 80 | 'guid' => $_->{'guid' }, | ||||||||||
| 81 | 'enclosure_url' => $_->{'enclosure_url'}, | ||||||||||
| 82 | 'length' => $_->{'length' }, | ||||||||||
| 83 | 'tracker' => $_->{'tracker' }, | ||||||||||
| 84 | 'tracker_url' => $_->{'tracker_url' }, | ||||||||||
| 85 | 'kws' => $_->{'kws' }, | ||||||||||
| 86 | 'exempts' => $_->{'exempts' }, | ||||||||||
| 87 | 'category' => $_->{'category' }, | ||||||||||
| 88 | 'original_site' => $_->{'original_site'}, | ||||||||||
| 89 | 'original_link' => $_->{'original_link'}, | ||||||||||
| 90 | 'size' => $_->{'size' }, | ||||||||||
| 91 | 'files' => $_->{'files' }, | ||||||||||
| 92 | 'seeds' => $_->{'Seeds' }, | ||||||||||
| 93 | 'leechers' => $_->{'leechers' }, | ||||||||||
| 94 | 'downloads' => $_->{'downloads' }, | ||||||||||
| 95 | 'votes' => $_->{'votes' }, | ||||||||||
| 96 | 'comments' => $_->{'comments' }, | ||||||||||
| 97 | 'hash' => $_->{'hash' }, | ||||||||||
| 98 | 0 | 'pub_date' => $_->{'pubDate' }, | |||||||||
| 99 | } ); | ||||||||||
| 100 | 0 | push @items, $item; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | my $response = Net::isoHunt::Response->new( { | ||||||||||
| 104 | 'title' => $json_decoded->{'title' }, | ||||||||||
| 105 | 'link' => $json_decoded->{'link' }, | ||||||||||
| 106 | 'description' => $json_decoded->{'description' }, | ||||||||||
| 107 | 'language' => $json_decoded->{'language' }, | ||||||||||
| 108 | 'category' => $json_decoded->{'category' }, | ||||||||||
| 109 | 'max_results' => $json_decoded->{'max_results' }, | ||||||||||
| 110 | 'ttl' => $json_decoded->{'ttl' }, | ||||||||||
| 111 | 'last_build_date' => $json_decoded->{'lastBuildDate'}, | ||||||||||
| 112 | 'pubDate' => $json_decoded->{'pubDate' }, | ||||||||||
| 113 | 'total_results' => $json_decoded->{'total_results'}, | ||||||||||
| 114 | 0 | 'censored' => $json_decoded->{'censored' }, | |||||||||
| 115 | 'image' => $image, | ||||||||||
| 116 | 'items' => \@items, | ||||||||||
| 117 | } ); | ||||||||||
| 118 | |||||||||||
| 119 | 0 | return $response; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 2 | __PACKAGE__->meta()->make_immutable(); | |||||||||
| 123 | |||||||||||
| 124 | 2 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 2 | ▶ | no Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 126 | |||||||||||
| 127 | 2 | 1; | |||||||||
| 128 | |||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | =pod | ||||||||||
| 132 | |||||||||||
| 133 | =head1 NAME | ||||||||||
| 134 | |||||||||||
| 135 | Net::isoHunt::Request - Populates request fields and executes request | ||||||||||
| 136 | |||||||||||
| 137 | =head1 VERSION | ||||||||||
| 138 | |||||||||||
| 139 | version 0.102770 | ||||||||||
| 140 | |||||||||||
| 141 | =head1 ATTRIBUTES | ||||||||||
| 142 | |||||||||||
| 143 | =head2 C<ihq> | ||||||||||
| 144 | |||||||||||
| 145 | Takes URL encoded value as requested search query. | ||||||||||
| 146 | |||||||||||
| 147 | =head2 C<start> | ||||||||||
| 148 | |||||||||||
| 149 | Optional. Starting row number in paging through results set. First page has | ||||||||||
| 150 | C<start=1>, not C<0>. Defaults to C<1>. | ||||||||||
| 151 | |||||||||||
| 152 | =head2 C<rows> | ||||||||||
| 153 | |||||||||||
| 154 | Optional. Results to return, starting from parameter C<start>. Defaults to | ||||||||||
| 155 | C<100>. | ||||||||||
| 156 | |||||||||||
| 157 | =head2 C<sort> | ||||||||||
| 158 | |||||||||||
| 159 | Optional. Defaults to composite ranking (over all factors such as age, query | ||||||||||
| 160 | relevance, seed/leechers counts and votes). Parameter takes only values of | ||||||||||
| 161 | C<seeds>, C<age> or C<size>, where C<seeds> sorting is combination of seeds + | ||||||||||
| 162 | leechers. Sort C<order> defaults to descending. | ||||||||||
| 163 | |||||||||||
| 164 | =head2 C<order> | ||||||||||
| 165 | |||||||||||
| 166 | Optional. Can be either C<asc> or C<desc>. Defaults to descending, in | ||||||||||
| 167 | conjunction with C<sort> parameter. | ||||||||||
| 168 | |||||||||||
| 169 | =head1 METHODS | ||||||||||
| 170 | |||||||||||
| 171 | =head2 C<execute> | ||||||||||
| 172 | |||||||||||
| 173 | Returns a L<Net::isoHunt::Response> object. | ||||||||||
| 174 | |||||||||||
| 175 | =head1 AUTHOR | ||||||||||
| 176 | |||||||||||
| 177 | Alan Haggai Alavi <alanhaggai@alanhaggai.org> | ||||||||||
| 178 | |||||||||||
| 179 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 180 | |||||||||||
| 181 | This software is copyright (c) 2010 by Alan Haggai Alavi. | ||||||||||
| 182 | |||||||||||
| 183 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 184 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 185 | |||||||||||
| 186 | =cut | ||||||||||
| 187 | |||||||||||
| 188 | |||||||||||
| 189 | __END__ | ||||||||||