#!/usr/bin/perl # simple example program that pulls a list of ISBNs out of a database # then searches the iBookstore to find them # iBookstore returns the data in a JSON record, so we use JSON # to get the piece we want #use strict and warnings so perl will catch errors: use strict; use warnings; use DBI; #DBI is a database interface library use JSON; #JSON is a library for dealing with the returned data use LWP::Simple; #LWP is a very powerful library to simulate a browser. LWP::Simple is a simple interface to it use Data::Dumper; # A lazy way to display test data #base query for getting records from isbn: my $itunesq='http://itunes.apple.com/lookup?isbn='; #connect to the database my $dbh=DBI->connect("DBI:mysql:host=localhost;database=boson_db","root","password",{PrintError=>0, RaiseError=>1}); #This sets up the database query my $sth= $dbh->prepare("SELECT e_isbn13 from metadata where e_isbn13>0"); #then we execute it. $sth then points to the returned object #that contains all the data $sth->fetchrow_array returns a # row from the $sth object as an array, but without me #having to worry about how it's stored in $sth $sth->execute(); while ( my @row = $sth->fetchrow_array ) { my $itunesrec=get("$itunesq@row"); #get the url my $json = JSON->new->allow_nonref; my $out=$json->decode($itunesrec)->{'results'}[0]; #print Dumper($out); print "$out->{'trackViewUrl'}\n"; for (keys %$out) { print "$_ => $out->{$_}\n"; } #print "$itunesq@row\n"; #print $itunesrec."\n"; }