Thursday, July 9, 2009

A Simple example for working using JDOM as a client for Amazon Advertising API

Well the paper is done, and I will post my comments in a while, but for now... a simple example for working with jdom... note that it wont work as posted, because I am taking out the Access Key, also it uses a a custom class called Review which I'll post separately.


import java.net.URL;
import com.sun.syndication.io.XmlReader;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import java.util.*;
import java.io.*;
import org.jdom.filter.*;

public class FeedParserByProduct
{
public static void main(String[] args) throws Exception
{
File f = new File("c:\\temp\\testdata"+System.currentTimeMillis()+".txt");
f.createNewFile();

FileWriter fw=new FileWriter(f);
//ListlRev=getReviewData("B0013O98SW","Windows-OS-Operating System");
//ListlRev=getReviewData("B00022PTT8","Windows-OS-Operating System");
//ListlRev=getReviewData("B000KRG6P6","Wolf-Shirt");
//ListlRev=getReviewData("B00193QFFG","Monitor-3star");
ListlRev=getReviewData(args[0],args[1]);

appendReviewersDataToReviewData(lRev);
fw.write(lRev.get(0).getRowHeaders()+"\r\n");
for(int i=0;i {
fw.write(lRev.get(i).getRawDataRowFormat()+"\r\n");
}
System.out.println("Composite RealScore1 is:"+getMeanRealScore1Composite(lRev));
//System.out.println("done");
fw.flush();
}

public static double getMeanRealScore1Composite(List revs) throws Exception
{
double d=0;
for(int i=0;i {
d+=revs.get(i).getRealScore1();
}
return d/revs.size();


}
static void appendReviewersDataToReviewData(List revList)throws Exception
{
for (int i=0;i {
Review r = revList.get(i);
Document d =getReviewerData(r.ReviewerID);
appendReviewerToReview(d,r);
}
}
private static void appendReviewerToReview(Document d, Review r)
{
int runningStarTotal=0;
int numberOfFives=0;
int keyWordAppears=0;
int totalHelpfulVotes=0;
int totalVotes=0;
int holder=0;
Element e = d.getRootElement();
ElementFilter ef = new ElementFilter( "Customers", null );
List el = e.getContent( ef );
e=(Element)el.get(0);
ef = new ElementFilter( "Customer", null );
el = e.getContent( ef );
e=(Element)el.get(0);

ef = new ElementFilter( "CustomerReviews", null );
el = e.getContent( ef );
e=(Element)el.get(0);

ef = new ElementFilter( "TotalReviews", null );
el = e.getContent( ef );

Element esub=null;
esub=(Element)el.get(0);
String sx =esub.getText();
holder=Integer.parseInt(sx);
r.reviewer_TotalReviews=holder;


ef = new ElementFilter( "Review", null );
el=e.getContent( ef );

//System.out.println(el.size());
for(int i=0;i {
esub=(Element)el.get(i);
//get the rating
ef=new ElementFilter( "Rating", null );
el=esub.getContent(ef);
holder=Integer.parseInt(((Element)el.get(0)).getText());
runningStarTotal=runningStarTotal+holder;
if(holder==5){numberOfFives +=1;}

//get HelpfulVotes
ef=new ElementFilter( "HelpfulVotes", null );
el=esub.getContent(ef);
holder=Integer.parseInt(((Element)el.get(0)).getText());
totalHelpfulVotes=runningStarTotal+holder;

//Get Total Votes
ef=new ElementFilter( "TotalVotes", null );
el=esub.getContent(ef);
holder=Integer.parseInt(((Element)el.get(0)).getText());
totalVotes=runningStarTotal+holder;
//Check to see if any key words appear in review
//deprecated as it seems that multiple reviews on the same subject can be good or bad

}

r.reviewer_TotalReviewPoints=runningStarTotal;
r.reviewer_TotalFives=numberOfFives;
//keyWordAppears=0;
r.reviewer_HelpfulVotes= totalHelpfulVotes;
r.reviewer_TotalVotes=totalVotes;
}




/**
*
* @param Reviews- Modefies review data list to enrich the profile
*/
static Document getReviewerData(String ReviewerID) throws Exception
{

Document doc=getXMLDoc("http://ecs.amazonaws.com/onca/xml?" +
"Service=AWSECommerceService&" +
"AWSAccessKeyId=AKIAJKIZAU3BXLEUC4DA&" +
"AssociateTag=dvshchyokin@hotmail.com&" +
"Operation=CustomerContentLookup&" +
"CustomerId="+ReviewerID+
"&ResponseGroup=CustomerReviews");
return doc;
}
private static Document getXMLDoc(String s) throws Exception
{
//System.out.println(s);
LinkedList ret = new LinkedList();
//String s ="http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=AKIAJKIZAU3BXLEUC4DA&Operation=ItemLookup&ItemId=B0013O98SW&ResponseGroup=Reviews&Version=2008-08-19";
URL feedUrl = new URL(s);

XmlReader xmlr=new XmlReader(feedUrl);

Document doc = null;

SAXBuilder sb = new SAXBuilder();
doc = sb.build(feedUrl);
return doc;
}


static List getReviewData(String productID, String pKeyWords) throws Exception
{
LinkedList ret= new LinkedList();
for(int j=1;j<50;j++)
{
try{
Document doc=getXMLDoc("http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=[USE YOUR OWN ACCESS KEY]&Operation=ItemLookup&ItemId="+ productID +"&ResponseGroup=Reviews&Version=2008-08-19&ReviewPage="+Integer.toString(j));
Element e =doc.getRootElement();

ElementFilter ef = new ElementFilter( "Items", null );
List el = e.getContent( ef );
e=(Element)el.get(0);
ef = new ElementFilter( "Item", null );
el = e.getContent( ef );
e=(Element)el.get(0);
ef = new ElementFilter( "CustomerReviews", null );
el = e.getContent( ef );
e=(Element)el.get(0);
ef = new ElementFilter( "Review", null );
el = e.getContent( ef );
String temp;
for(int i=0;i {
temp=concatReviewData((Element)el.get(i));
ret.add(new Review(temp,pKeyWords));
}
}
catch(Exception ex){break;}
}
return ret;
}

static String concatReviewData(Element e)
{

String temp="";
List el =e.getContent();
for(int i=0;i {
temp=temp+((Element)el.get(i)).getText()+"|";

}
ElementFilter ef = new ElementFilter( "Reviewer", null );
el = e.getContent( ef );
e=(Element)el.get(0);
el=e.getContent();
for(int j=0;j {
temp=temp+((Element)el.get(j)).getText()+"|";
}
//System.out.println(temp);
return temp;
}

}

No comments:

Post a Comment

Followers