From 0eff1663d01977542009b247f9632a891d8a6a87 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 11 Apr 2015 12:40:18 +0300 Subject: [PATCH] Add IMDB support --- lib/WebService/Strike.pm | 27 ++++++++++++++++++++++----- lib/WebService/Strike/Torrent.pm | 29 ++++++++++++++++++++++++++--- t/WebService-Strike.t | 5 ++++- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/lib/WebService/Strike.pm b/lib/WebService/Strike.pm index 74a8c2b..001972d 100644 --- a/lib/WebService/Strike.pm +++ b/lib/WebService/Strike.pm @@ -5,10 +5,10 @@ use strict; use warnings; use parent qw/Exporter/; -our @EXPORT = qw/strike strike_search/; ## no critic (ProhibitAutomaticExportation) -our @EXPORT_OK = qw/strike_query strike strike_search/; +our @EXPORT = qw/strike strike_search strike_imdb/; ## no critic (ProhibitAutomaticExportation) +our @EXPORT_OK = (@EXPORT, 'strike_query'); our $VERSION = '0.003'; -our $BASE_URL = 'https://getstrike.net/api/v2/torrents'; +our $BASE_URL = 'https://getstrike.net/api/v2'; use JSON::MaybeXS qw/decode_json/; use HTTP::Tiny; @@ -33,7 +33,7 @@ sub strike_query { if (@hashes > 50) { return strike_query (@hashes[0 .. 49]), strike_query (@hashes[50 .. $#hashes]); } - my $url = "$BASE_URL/info/?hashes=" . join ',', map { uc } @hashes; + my $url = "$BASE_URL/torrents/info/?hashes=" . join ',', map { uc } @hashes; my $sorter = sbe(\@hashes, {xform => sub { $_[0]->hash }}); my @torrents = $sorter->(_query $url); @@ -43,7 +43,7 @@ sub strike_query { sub strike_search { my ($query, $full, %args) = @_; $args{phrase} = $query; - my $url = "$BASE_URL/search/?" . HTTP::Tiny->www_form_urlencode(\%args); + my $url = "$BASE_URL/torrents/search/?" . HTTP::Tiny->www_form_urlencode(\%args); my @torrents = _query $url; @torrents = $torrents[0] unless wantarray; @@ -51,6 +51,14 @@ sub strike_search { wantarray ? @torrents : $torrents[0] } +sub strike_imdb { + my ($id) = @_; + my $url = "$BASE_URL/media/imdb/?imdbid=$id"; + my $response = _ht->get($url); + return unless $response->{success}; + decode_json $response->{content} +} + BEGIN { *strike = \&strike_query } 1; @@ -79,6 +87,10 @@ WebService::Strike - Get torrent info from getstrike.net API say 'Torrent has ' . $mp->count . ' files. They are:'; say join ' ', @{$mp->file_names}; + my $info = strike_imdb 'tt1520211'; + say 'IMDB ID ', $info->{imdbID}, ' is ', $info->{title}, ' (', $info->{year}, ')'; + say 'Plot (short): ', $info->{shortPlot}; + =head1 DESCRIPTION Strike API is a service for getting information about a torrent given @@ -114,6 +126,11 @@ For example: strike_search 'windows', 0, category => 'Applications', sub_category => 'Windows'; +=item B(I<$imdb_id>) + +Get informaton about a movie from IMDB. Takes an IMDB ID and returns a +hashef of unspecified format. + =back =head1 SEE ALSO diff --git a/lib/WebService/Strike/Torrent.pm b/lib/WebService/Strike/Torrent.pm index 5170208..655efed 100644 --- a/lib/WebService/Strike/Torrent.pm +++ b/lib/WebService/Strike/Torrent.pm @@ -12,7 +12,7 @@ use JSON::MaybeXS qw/decode_json/; use MIME::Base64; use WebService::Strike; -__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/); +__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 imdb_id/); BEGIN { *hash = *torrent_hash; @@ -40,7 +40,7 @@ sub new{ sub torrent{ my ($self, $file) = @_; - my $url = $WebService::Strike::BASE_URL . '/download/?hash=' . $self->hash; + my $url = $WebService::Strike::BASE_URL . '/torrents/download/?hash=' . $self->hash; my $ht = WebService::Strike::_ht(); ## no critic (ProtectPrivate) my $response = $ht->get($url); return unless $response->{success}; @@ -59,13 +59,19 @@ sub torrent{ sub description{ my ($self) = @_; return $self->{description} if $self->{description}; - my $url = $WebService::Strike::BASE_URL . '/descriptions/?hash=' . $self->hash; + my $url = $WebService::Strike::BASE_URL . '/torrents/descriptions/?hash=' . $self->hash; my $ht = WebService::Strike::_ht(); ## no critic (ProtectPrivate) my $response = $ht->get($url); return unless $response->{success}; $self->{description} = decode_base64 $response->{content} } +sub imdb { + my ($self) = @_; + return unless $self->imdb_id; + $self->{imdb} //= WebService::Strike::strike_imdb ($self->imdb_id) +} + 1; __END__ @@ -96,6 +102,13 @@ WebService::Strike::Torrent - Class representing information about a torrent $t->torrent('x.torrent'); # Download torrent file to x.torrent say $t->description; # + $t = strike 'ED70C185E3E3246F30B2FDB08D504EABED5EEA3F'; + say $t->title; # The Walking Dead S04E15 HDTV x264-2HD + say $t->imdb_id; # tt1520211 + my $i = $t->imdb; + say $i->{title}, ' (', $i->{year}, ')'; # The Walking Dead (2010) + say $i->{genre}; # Drama, Horror, Thriller + =head1 DESCRIPTION WebService::Strike::Torrent is a class that represents information @@ -170,6 +183,16 @@ Both forms return a true value for success and false for failure. The description of the torrent. This method sends an extra request to Strike. Successful responses are cached. +=item B + +The IMDB ID of the torrent, or undef if the torrent has no associated +IMDB ID. + +=item B + +Calls B from L on B. Caches +the response. Returns undef if the torrent has no associated IMDB ID. + =back =head1 SEE ALSO diff --git a/t/WebService-Strike.t b/t/WebService-Strike.t index 7b7a984..853a98f 100644 --- a/t/WebService-Strike.t +++ b/t/WebService-Strike.t @@ -4,7 +4,7 @@ use warnings; use Data::Dumper qw/Dumper/; use Test::RequiresInternet qw/getstrike.net 443/; -use Test::More tests => 10; +use Test::More tests => 11; use Try::Tiny; BEGIN { use_ok('WebService::Strike') }; @@ -43,3 +43,6 @@ try { my $p = strike_search 'Perl', 1; is @{$p->file_names}, $p->count, 'file_names has count elements'; + +my $imdb = strike('ED70C185E3E3246F30B2FDB08D504EABED5EEA3F')->imdb; +is $imdb->{title}, 'The Walking Dead', 'imdb title'; -- 2.39.2