forked from thepeopleseason/olga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimdb_queries.pl
73 lines (54 loc) · 1.56 KB
/
imdb_queries.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Response;
my (@agents) = qw(Slicey Linkovich PeteShablatch Olga Beanfield);
my (@versions) = qw(0.4b 0.28 1.3 3.9a 2.0 0.1.1);
sub title_search
{
my ($ua) = LWP::UserAgent->new ();
my ($keywords) = join (' ', @_);
my ($response, $url);
$ua->agent ($agents[rand (@agents)] . '/' . $versions[rand (@versions)]);
$response = $ua->request (POST 'http://us.imdb.com/Lookup',
['for' => $keywords, 'type' => 'title']);
if (!$response->is_success () && $response->code () eq '302') {
$url = $response->header ('location');
$url = 'http://us.imdb.com' . $url unless $url =~ m!^http://!;
$response = fetch_page ($url);
}
return ($response);
}
sub movie_titles
{
my (@query) = @_;
my ($response) = title_search (@query);
my ($content, $title);
if ($response->is_success) {
($title) = $response->content () =~ m!<title>(.+?)</title>!is;
if ($title =~ /imdb\s*title\s*search/) {
($content) = $response->content =~
m!<h2>movies</h2>.*?(<ol>.+?</ol>)!is;
$content =~ s/<BR>/\n/isg;
$content =~ s/<.+?>//sg;
$content =~ s/\(.+?\)$//mg;
}
else {
$content = $title;
}
}
else {
die $response->error_as_HTML (), "\n";
}
return ($content);
}
sub fetch_page
{
my ($url) = shift;
my ($ua, $response);
$ua = LWP::UserAgent->new ();
$ua->agent ($agents[rand (@agents)] . '/' .
$versions[rand (@versions)]);
$response = $ua->request (GET $url);
return ($response);
}