/
sip-map.pl
executable file
·83 lines (59 loc) · 1.97 KB
/
sip-map.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
73
74
75
76
77
78
79
80
81
82
83
# sip-map.pl
# 26 apr 2006
#
# wj turkel
# http://digitalhistoryhacks.blogspot.com
#
# Given the ISBN of a book, scrapes its SIPs
# (statistically improbable phrases) from Amazon.com
# and plots them with the GraphViz package.
# Outputs a PNG file.
# Load necessary modules
use WWW::Mechanize;
use GraphViz;
# Given an ISBN, get the starting page from Amazon.com
my $startbook = 'Diamond, Guns, Germs and Steel';
my $startisbn = "0393317552";
# Initialize output graph
my $g = GraphViz->new(layout => 'fdp');
$g->add_node($startbook, shape => 'box');
my $start = "http://www.amazon.com/gp/product/" . $startisbn . "/";
my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech->get( $start );
# Extract the SIPs for the starting book
my %sipurls = ();
my @links = $mech->find_all_links( );
for my $link ( @links ) {
my $url = $link->url_abs;
my $siptext = $link->text;
if($url =~ m/sip_bod/) {
# print "SIP", "\t", $siptext, "\n\n";
$g->add_node($siptext);
$g->add_edge($startbook => $siptext);
# Hash the SIP URL and associated SIP to process later
# print "URL", "\t", $url, "\n";
$sipurl{ $url } = $siptext;
}
}
# Extract the books for each SIP
for my $url ( keys %sipurl ) {
my $siptext = $sipurl{ $url };
print "URL", "\t", $url, "\n";
print "SIP", "\t", $siptext, "\n\n";
$mech->get( $url );
my @furtherlinks = $mech->find_all_links( );
for my $link ( @furtherlinks ) {
my $newurl = $link->url_abs;
my $booktext = $link->text;
if($newurl =~ m/sip_pdp_dp/) {
# print "SIP", "\t", $siptext, "\n\n";
$g->add_node($booktext, shape => 'box');
$g->add_edge($siptext => $booktext);
}
}
}
# Create the graph
open(OUTPUT, ">$startisbn.png");
binmode OUTPUT;
print OUTPUT $g->as_png;
close OUTPUT;