Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

bitingduck's scratchpad

by bitingduck (Hermit)
on May 19, 2012 at 01:38 UTC ( #971379=scratchpad: print w/ replies, xml ) Need Help??

here's a simple example of looking into a remote database to find a book and getting a piece of information (the url for the book) from the record that is returned. Adding a new record could be as simple as checking to see if anything is returned, and if nothing, construct a new http request to submit the information to create a new record.

#!/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 browse +r. 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=''; #connect to the database my $dbh=DBI->connect("DBI:mysql:host=localhost;database=boson_db","roo +t","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"; }

Newest Nodes
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2015-03-03 21:13 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (85 votes), past polls