Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/dd9846ef1cb2826f59da to your computer and use it in GitHub Desktop.
Save anonymous/dd9846ef1cb2826f59da to your computer and use it in GitHub Desktop.
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