Created
April 9, 2015 19:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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