Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

bitingduck's scratchpad

by bitingduck (Friar)
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='http://itunes.apple.com/lookup?isbn='; #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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-07-24 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls