Skip to content

Instantly share code, notes, and snippets.

/book_request.erl
Created Apr 9, 2015

Embed
What would you like to do?
Look up book author, title, and publisher by isbn in Library of Congress db.
-module (book_request).
-export([http_loc/1]).
-include_lib("xmerl/include/xmerl.hrl").
%% Look up book author, title, and publisher by isbn in
%% Library of Congress db.
%% http://www.loc.gov/standards/sru/resources/lcServers.html
%% Thanks to erlang-questions responsse frome Darach Ennis and
%% Dave Thomas - http://pragdave.me/blog/2007/04/15/a-first-erlang-program/
%% Use: 1> inets:start().
%% ...
%% 2> book_request:http_loc("9780982589205").
%% {"Prentice, Lloyd Roland","Freein' pancho", "Writers Glen Publications"}
loc(ISBN) ->
"http://lx2.loc.gov:210/lcdb?" ++
"version=1.1" ++
"&operation=searchRetrieve"++
"&query=bath.isbn=" ++
ISBN ++
"&maximumRecords=1" ++
"&recordSchema=mods".
http_loc(ISBN) ->
{ok, {_Status, _Headers, Body}} = httpc:request(loc(ISBN)),
{Xml, _Rest} = xmerl_scan:string(Body),
[ #xmlText{value=Author}] = xmerl_xpath:string("//namePart/text()", Xml),
[ #xmlText{value=Title}] = xmerl_xpath:string("//title/text()", Xml),
[ #xmlText{value=Publisher}] = xmerl_xpath:string("//publisher/text()", Xml),
{Author, Title, Publisher}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.