Rarbg-torrentapi

Perl Module to interact with https://torrentapi.org
git clone https://git.onna.be/Rarbg-torrentapi.git
Log | Files | Refs | README | LICENSE

torrentapi.pm (6076B)


      1 package Rarbg::torrentapi;
      2 
      3 use strict;
      4 use 5.008_005;
      5 our $VERSION = 'v0.1.8';
      6 use LWP::UserAgent;
      7 use JSON;
      8 use Carp;
      9 use Rarbg::torrentapi::Res;
     10 use Rarbg::torrentapi::Error;
     11 use Moose;
     12 
     13 our $BASEURL = 'https://torrentapi.org/pubapi_v2.php?';
     14 
     15 has [qw(search_string search_imdb search_themoviedb search_tvdb category)] => (
     16     is  => 'rw',
     17     isa => 'Str'
     18 );
     19 
     20 has limit => (
     21     is      => 'rw',
     22     isa     => 'Int',
     23     default => 25
     24 );
     25 
     26 has sort => (
     27     is      => 'rw',
     28     isa     => 'Str',
     29     default => 'last'
     30 );
     31 
     32 has [qw(min_seeders min_leechers)] => (
     33     is  => 'rw',
     34     isa => 'Int'
     35 );
     36 
     37 has ranked => (
     38     is      => 'rw',
     39     isa     => 'Int',
     40     default => 0
     41 );
     42 
     43 has mode => (
     44     is      => 'rw',
     45     isa     => 'Str',
     46     default => 'list'
     47 );
     48 
     49 has app_id => (
     50     is      => 'rw',
     51     isa     => 'Str',
     52     default => 'p5-Rarbg-torrentapi'
     53 );
     54 
     55 has _format => (
     56     is      => 'ro',
     57     isa     => 'Str',
     58     default => 'json_extended'
     59 );
     60 
     61 has _ua => (
     62     is      => 'ro',
     63     default => sub {
     64         LWP::UserAgent->new( agent => 'curl/7.44.0' );
     65     }
     66 );
     67 
     68 has _token => (
     69     is      => 'rw',
     70     isa     => 'Str',
     71     default => sub {
     72         my $self = shift;
     73         $self->_renew_token();
     74     },
     75     required => 1,
     76     lazy     => 1
     77 );
     78 
     79 has _token_time => (
     80     is      => 'rw',
     81     isa     => 'Int',
     82     default => -1,
     83 );
     84 
     85 sub _renew_token {
     86     my $self = shift;
     87     my $url  = $BASEURL . "get_token=get_token&app_id=" . $self->app_id;
     88     my $res_json = $self->_ua->get($url);
     89     sleep 1;
     90     if ( $res_json->is_success ) {
     91         $self->_token_time(time);
     92         my $res = decode_json( $res_json->decoded_content );
     93         return $res->{token};
     94     }
     95     else {
     96         confess "Cannot get token " . $res_json->status_line;
     97     }
     98 }
     99 
    100 sub _token_valid {
    101     my $self = shift;
    102     ( time - $self->_token_time ) < 890;
    103 }
    104 
    105 sub _make_request {
    106     my $self = shift;
    107     unless ( $self->_token_valid ) {
    108         $self->_token( $self->_renew_token );
    109     }
    110     my $url = $BASEURL;
    111     foreach my $attribute ( $self->meta->get_attribute_list ) {
    112         next if $attribute =~ /^_/;
    113         if ( $self->$attribute ) {
    114             $url .= "$attribute=" . $self->$attribute . "&";
    115         }
    116     }
    117     $url .= "format=" . $self->_format . "&";
    118     $url .= "ranked=" . $self->ranked . "&";
    119     $url .= "token=" . $self->_token;
    120     my $res_json = $self->_ua->get($url);
    121     if ( $res_json->is_success ) {
    122         my $tresults = decode_json( $res_json->decoded_content );
    123         my @res;
    124         if ( $tresults->{torrent_results}
    125             && scalar( @{ $tresults->{torrent_results} } ) > 1 )
    126         {
    127             foreach my $t ( @{ $tresults->{torrent_results} } ) {
    128                 my $t_obj = Rarbg::torrentapi::Res->new($t);
    129                 push @res, $t_obj;
    130             }
    131             return \@res;
    132         }
    133         else {
    134             return Rarbg::torrentapi::Error->new($tresults);
    135         }
    136     }
    137     else {
    138         confess "Cannot execute Call " . $res_json->status_line;
    139     }
    140 }
    141 
    142 foreach my $method (qw/list search/) {
    143     __PACKAGE__->meta->add_method(
    144         $method,
    145         sub {
    146             my $self = shift;
    147             my $args = shift;
    148             foreach my $key ( keys %{$args} ) {
    149                 $self->$key( $args->{$key} );
    150             }
    151             $self->mode("$method");
    152             return $self->_make_request;
    153         }
    154     );
    155 }
    156 
    157 no Moose;
    158 __PACKAGE__->meta->make_immutable;
    159 1;
    160 __END__
    161 
    162 =encoding utf-8
    163 
    164 =head1 NAME
    165 
    166 Rarbg::torrentapi - Wrapper around Rarbg torrentapi (L<https://torrentapi.org/apidocs_v2.txt>)
    167 
    168 =head1 SYNOPSIS
    169 
    170   use Rarbg::torrentapi;
    171   my $tapi = Rarbg::torrentapi->new();
    172   
    173   # list lastest torrents
    174   my $last_added = $tapi->list();
    175   
    176   # list torrents sorted by seeders
    177   my $last_added = $tapi->list({
    178       sort => 'seeders',
    179       limit => 50,
    180       category => 'tv'
    181   });
    182 
    183   # search by string
    184   # You can use all attributes also
    185   my $search = $tapi->search({
    186       search_string => 'the beatles',
    187       category => '23;24;25;26',
    188       min_seeders => 20
    189   });
    190 
    191   # search by imdb id
    192   my $search = $tapi->search({
    193       search_imdb => 'tt123456'
    194   });
    195 
    196   # search by themoviedb id
    197   my $search = $tapi->search({
    198       search_themoviedb => '123456'
    199   });
    200 
    201   # search by tvdb id
    202   my $search = $tapi->search({
    203       search_tvdb => '123456'
    204   });
    205 
    206 
    207 =head1 DESCRIPTION
    208 
    209 Rarbg::torrentapi is a simple wrapper around Rarbg's torrentapi.
    210 
    211 =head1 ATTRIBUTES
    212 
    213 Those attributes can be used on all public methods. In fact you can use them also when creating the object. Some of them make more sense at creation time, some others when calling the method. It's your call.
    214 
    215 The only difference is that you should pass them as an anonymous hash if you pass them to a method.
    216 
    217 You can find more info about their values at L<https://torrentapi.org/apidocs_v2.txt>
    218 
    219 =head2 search_string
    220 
    221 =head2 search_imdb
    222 
    223 This is the Imdb id (http://imdb.com) in the form 'tt123456'
    224 
    225 =head2 search_themoviedb
    226 
    227 =head2 search_tvdb
    228 
    229 =head2 category
    230 
    231 Category can be quite confusing.
    232 It accepts 'tv' and 'movies'. But, for the rest of categories only accepts its id numbers (or a semi-colon separated list of them).
    233 Check Rarbg website to see what those are. They are not documented anywhere.
    234 
    235 =head2 limit
    236 
    237 It can be 25, 50 or 100.
    238 
    239 =head2 sort
    240 
    241 It can be seeders, leechers or last
    242 
    243 =head2 min_seeders
    244 
    245 =head2 min_leechers
    246 
    247 =head2 ranked
    248 
    249 This marks if you want to get all indexed torrents or just the ones from rarbg team.
    250 Defaults to all (0).
    251 
    252 =head1 METHODS
    253 
    254 =head2 new
    255 
    256 Just a simple constructor.
    257 
    258 =head2 search
    259 
    260 Makes a call to the API in 'search' mode. It returns either a Rarbg::torrentapi::Error or an array of L<Rarbg::torrentapi::Res>.
    261 
    262 =head2 list
    263 
    264 Makes a call to the API in 'list' mode. It returns either a Rarbg::torrentapi::Error or an array of L<Rarbg::torrentapi::Res>.
    265 
    266 =head1 AUTHOR
    267 
    268 Paco Esteban E<lt>paco@onna.beE<gt>
    269 
    270 =head1 COPYRIGHT
    271 
    272 Copyright 2015- Paco Esteban
    273 
    274 =head1 LICENSE
    275 
    276 This library is free software; you can redistribute it and/or modify
    277 it under the same terms as Perl itself.
    278 
    279 =cut