Amazon Web Services

This example demonstrates the Amazon Web Services.  The Amazon Web Services are good demenstrations of using Web Services to retrieve data.  This example searches the Amazon databases for books written by certain authors, published by a certain manufacturer, or containing a certain keyword.  The example code then places the results in a new notebook.  The example has one option.  Setting the ShowPictures option to True will force the code to retrieve and display the cover image of the book.

To try this example, evaluate all the initialization cells (you can do this with the menu command Evaluation ▶ Evaluate Initialization Cells). Then go to the Examples section.

Code

BeginPackage["WebServices`Examples`Amazon`",{"WebServices`","JLink`"}];

AuthorSearch;
ManufacturerSearch;
KeywordSearch;
ShowPictures;

Begin["`Private`"];

InstallService["http://soap.amazon.com/schemas3/AmazonWebServices.wsdl"];

Options[AuthorSearch]={ShowPictures->False};

AuthorSearch[keyword_String,pages_Integer,options___?OptionQ]:=Module[{result},result=AuthorSearchRequest[{"author"->keyword,"page"->pages,"mode"->"books","tag"->"webservices-20","type"->"lite","devtag"->"D3VZJS46JD5U9D","version"->"1.0"}];
If[result===$Failed,Return[$Failed]];
result="Details"/.result;
NotebookPut[
resultNotebook["Amazon Author Search",keyword,result,options]]
]

ManufacturerSearch[keyword_String,pages_Integer,options___?OptionQ]:=Module[{result},result=ManufacturerSearchRequest[{"manufacturer"->keyword,"page"->pages,"mode"->"books","tag"->"webservices-20","type"->"lite","devtag"->"D3VZJS46JD5U9D","version"->"1.0"}];
If[result===$Failed,Return[$Failed]];
result="Details"/.result;
NotebookPut[
resultNotebook["Amazon Manufacturer Search",keyword,result,options]]
];

KeywordSearch[keyword_String,pages_Integer,options___?OptionQ]:=Module[{result},result=KeywordSearchRequest[{"keyword"->keyword,"page"->pages,"mode"->"books","tag"->"webservices-20","type"->"lite","devtag"->"D3VZJS46JD5U9D","version"->"1.0"}];
If[result===$Failed,Return[$Failed]];
result="Details"/.result;
NotebookPut[
resultNotebook["Amazon Keyword Search",keyword,result,options]]
];

resultNotebook[title_String, keyword_String,result_List,options___?OptionQ]:=
Notebook[Flatten[{
Cell[title,"Subtitle"],
Cell["Results for: "<>keyword,"Section"],
resultCells[#,options]& /@ result
}],CellGrouping->Manual,WindowTitle->(title<>": "<>keyword)]

resultCells[d_List,options___?OptionQ]:=Module[{url,asin,productName,authorNames,releaseDate,manufacturer,imageUrlMedium,listPrice,ourPrice,usedPrice,showPictures,cellList},{showPictures}={ShowPictures}/.Flatten[{options}]/.Options[AuthorSearch];
url="Url"/.d;
asin="Asin"/.d;
productName="ProductName"/.d;
authorNames="Authors"/.d;
releaseDate="ReleaseDate"/.d;
manufacturer="Manufacturer"/.d;
imageUrlMedium="ImageUrlMedium"/.d;
listPrice="ListPrice"/.d;
ourPrice="OurPrice"/.d;
usedPrice="UsedPrice"/.d;
authorNames=ToString[authorNames];
authorNames=StringDrop[authorNames,1];
authorNames=StringDrop[authorNames,-1];
If[TrueQ[showPictures],imageUrlMedium=getImageData[imageUrlMedium];
If[StringMatchQ[imageUrlMedium,"GIF*"],imageCell=ToExpression[ExportString[ImportString[imageUrlMedium,"GIF"],"MGFCell"]],imageCell=ToExpression[ExportString[ImportString[imageUrlMedium,"JPEG"],"MGFCell"]]];];
{Cell[CellGroupData[Flatten[{
Cell[TextData[{
ButtonBox[StyleBox[productName,FontWeight->"Bold"],ButtonData->{URL[url],None},ButtonStyle->"Hyperlink"]," by ",StyleBox[authorNames,FontSlant->"Italic"]}],"Text",ShowGroupOpenCloseIcon->True],If[TrueQ[showPictures],Cell[TextData[imageCell],"Text"],{}],Cell[TextData[{StyleBox["Release Date: ",FontWeight->"Bold"],releaseDate,"\n",
StyleBox["ASIN: ",FontWeight->"Bold"],asin,"\n",
StyleBox["List Price: ",FontWeight->"Bold"],listPrice,"\n",
StyleBox["Our Price: ",FontWeight->"Bold"],ourPrice}],"Text"]}],Closed]]}]

getImageData[url_String]:=JavaBlock[Module[{u,connection,stream,input="",numRead=0,bytesRead=0,buf},u=JavaNew["java.net.URL",url];
connection=u@openConnection[];
stream=connection@getInputStream[];
If[stream==$Failed,Return[URLError["Failed to open URL stream"]]];
buf=JavaNew["[B",5000];
While[(numRead=stream@read[buf])>0,AddTo[bytesRead,numRead];
input=StringJoin[input,ToString[FromCharacterCode[If[#<0,#+256,#]&/@Take[JavaObjectToExpression[buf],numRead]]]];];
stream@close[];
input]]

End[];
EndPackage[];

Examples

AuthorSearch["Stephen Wolfram",1]

ManufacturerSearch["Addison-Wesley",1]

KeywordSearch["Mathematica",1,ShowPictures->True]