| 1 | =head1 PACKAGE | ||
| 2 | |||
| 3 | IMDB::JSON | ||
| 4 | |||
| 5 | =head1 DESCRIPTION | ||
| 6 | |||
| 7 | Search IMDB for a specific title, process the result and extract the JSON script within. Process the JSON script and return a hash reference. | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | package IMDB::JSON; | ||
| 12 | |||
| 13 | $IMDB::JSON::VERSION = "0.01"; | ||
| 14 | |||
| 15 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 2 | ▶ | use HTML::TokeParser; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 2 | ▶ | use LWP::Simple qw($ua get); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 2 | ▶ | use IO::Socket::SSL; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 2 | ▶ | use JSON::XS; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use IMDB::JSON; | ||
| 24 | use Data::Dumper; | ||
| 25 | |||
| 26 | my $IMDB = IMDB::JSON->new; | ||
| 27 | |||
| 28 | print Dumper($IMDB->search("The Thing", "1982)); | ||
| 29 | |||
| 30 | exit; | ||
| 31 | |||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =head2 new(opt => value); | ||
| 36 | |||
| 37 | Create a new IMDB::JSON object, options can be passed to the object by specifying them | ||
| 38 | |||
| 39 | =head3 OPTIONS | ||
| 40 | |||
| 41 | =over | ||
| 42 | |||
| 43 | =item base_url | ||
| 44 | |||
| 45 | The base URL to start from. This is usually https://www.imdb.com | ||
| 46 | |||
| 47 | =item raw_json | ||
| 48 | |||
| 49 | If true, returns only raw JSON text, it's not processed into an hash reference | ||
| 50 | |||
| 51 | =item user_agent | ||
| 52 | |||
| 53 | Set the User-Agent you want to send with the request | ||
| 54 | |||
| 55 | =item debug | ||
| 56 | |||
| 57 | If true, print debug messages to STDERR | ||
| 58 | |||
| 59 | =back | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub new { | ||
| 64 | my ($CLASS, %o) = @_; | ||
| 65 | return bless { | ||
| 66 | base_url => ($o{base_url} ? $o{base_url} : 'https://www.imdb.com'), | ||
| 67 | raw_json => ($o{raw_json} ? 1 : 0), | ||
| 68 | user_agent => $o{user_agent}, | ||
| 69 | debug => $o{debug} | ||
| 70 | }; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub _get { | ||
| 75 | my ($self, $URL) = @_; | ||
| 76 | |||
| 77 | $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; | ||
| 78 | my $ua = LWP::UserAgent->new( | ||
| 79 | ssl_opts => { | ||
| 80 | verify_hostname => 0, | ||
| 81 | SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, | ||
| 82 | } | ||
| 83 | ); | ||
| 84 | |||
| 85 | # Set the user agent to something | ||
| 86 | $ua->agent($self->{user_agent}) if $self->{user_agent}; | ||
| 87 | |||
| 88 | print STDERR "DEBUG: fetch URL: $URL\n" if $self->{debug}; | ||
| 89 | |||
| 90 | my $req = HTTP::Request->new( GET => $URL); | ||
| 91 | |||
| 92 | my $response = $ua->request($req); | ||
| 93 | |||
| 94 | return $response->content; | ||
| 95 | } | ||
| 96 | |||
| 97 | #URI encoding | ||
| 98 | sub _enc { | ||
| 99 | my ($self, $data) = @_; | ||
| 100 | |||
| 101 | $data =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg; | ||
| 102 | return $data; | ||
| 103 | } | ||
| 104 | |||
| 105 | # Process IMDB search results | ||
| 106 | sub _result { | ||
| 107 | my ($self, $title, $year) = @_; | ||
| 108 | |||
| 109 | my $data = $self->_get($self->{base_url} . '/search/title?title=' . $self->_enc($title) . '&release_date=' . $year . '-01-01,' . $year . '-12-31&view=simple'); | ||
| 110 | |||
| 111 | print STDERR "DEBUG: " . length($data) . " bytes of data received\n" if $self->{debug}; | ||
| 112 | |||
| 113 | return if !$data; | ||
| 114 | |||
| 115 | my $url; | ||
| 116 | |||
| 117 | # Process the results data (must be reference scalar!) | ||
| 118 | my $p = HTML::TokeParser->new(\$data); | ||
| 119 | |||
| 120 | # Walk down to the results section | ||
| 121 | while(my $t = $p->get_tag('div')){ | ||
| 122 | last if($t->[1]->{class} eq 'lister-item mode-simple'); | ||
| 123 | } | ||
| 124 | |||
| 125 | # Walk through the results and match the correct one | ||
| 126 | while(my $t = $p->get_tag('span')){ | ||
| 127 | # Found a results chunk | ||
| 128 | if($t->[1]->{class} eq "lister-item-header"){ | ||
| 129 | |||
| 130 | # Grab the href and text | ||
| 131 | my $t = $p->get_tag('a'); | ||
| 132 | $url = $t->[1]->{href}; | ||
| 133 | |||
| 134 | # Grab the title and year | ||
| 135 | my $txt = $p->get_trimmed_text; | ||
| 136 | |||
| 137 | my $t = $p->get_tag('span'); | ||
| 138 | my $yr = $p->get_trimmed_text; | ||
| 139 | |||
| 140 | # Check and see if they match | ||
| 141 | if($title eq $txt && "($year)" eq $yr){ | ||
| 142 | return $url; | ||
| 143 | } elsif($self->{debug}){ | ||
| 144 | print STDERR "DEBUG: result miss: $txt / $yr\n"; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub search { | ||
| 154 | my ($self, $title, $year) = @_; | ||
| 155 | |||
| 156 | my $url = $self->_result($title, $year); | ||
| 157 | |||
| 158 | return if !$url; | ||
| 159 | |||
| 160 | my $data = $self->_get($url =~ /^https?:\/\// ? $url : $self->{base_url} . ($url =~ /^\// ? $url : '/' . $url)); | ||
| 161 | |||
| 162 | print STDERR "DEBUG: " . length($data) . " bytes of data received\n" if $self->{debug}; | ||
| 163 | |||
| 164 | return if !$data; | ||
| 165 | |||
| 166 | my $p = HTML::TokeParser->new(\$data); | ||
| 167 | |||
| 168 | while(my $t = $p->get_tag('script')){ | ||
| 169 | last if($t->[1]->{type} eq "application/ld+json"); | ||
| 170 | } | ||
| 171 | |||
| 172 | my $json = $p->get_text; | ||
| 173 | |||
| 174 | if(!$json){ | ||
| 175 | $@ = "JSON script not found!"; | ||
| 176 | print STDERR "DEBUG: $@\n" if $self->{debug}; | ||
| 177 | |||
| 178 | return; | ||
| 179 | } else { | ||
| 180 | return ($self->{raw_json} ? $json : decode_json($json)); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | =head1 AUTHOR | ||
| 187 | |||
| 188 | Colin Faber <cfaber@fpsn.net> | ||
| 189 | |||
| 190 | =head1 BUGS | ||
| 191 | |||
| 192 | Report all bugs on https://rt.cpan.org OR email me directly | ||
| 193 | |||
| 194 | =head1 COPYRIGHT | ||
| 195 | |||
| 196 | IMDB::JSON is Copyright (C) 2018, by Colin Faber. | ||
| 197 | |||
| 198 | It is free software; you can redistribute it and/or modify it under the terms of either: | ||
| 199 | |||
| 200 | a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or | ||
| 201 | |||
| 202 | b) the "Perl Artistic License". | ||