}
@SuppressWarnings("unchecked")
public DiggSearchStoryResults getStoriesList(
List<ServiceParameter> parameters) throws Exception {
DiggSearchStoryResults results = new DiggSearchStoryResults();
StringBuilder builder = new StringBuilder();
/* Prepare search story parameters */
parameters = prepareSearchStoryQueryParameters(parameters);
/* Safely prepare the search URL and make connection */
URL diggSearchUrl = new URL(DIGG_SEARCH_SERVICE_URL_V2
+ DIGG_SEARCH_STORY_METHOD_V2 /* digg Apis v.2.0 search method */
+ "?" + ServiceParametersUtility.toParametersString(parameters));
URLConnection connection = diggSearchUrl.openConnection();
System.out.println("diggSearchUrl = "+ diggSearchUrl.toString());
//connection.addRequestProperty(REFERER, REFERER_VALUE);
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line).append('\n');
}
JSONObject rootResults = new JSONObject(builder.toString());
// Check for invalid result or returned an error
if (! rootResults.has(DIGG_STORIES_ELEMENT) ) {
throw new InvalidResultException(
"Error in Results: The stories root element is not available ");
}
JSONArray storiesElement = rootResults.getJSONArray(DIGG_STORIES_ELEMENT);
/* Parse the Results as long there is no returned Error code */
// Start parsing for general results information
results.setCount(rootResults.getString(STORIES_COUNT_ATTRIBUTE));
results.setTimestamp(rootResults.getString(STORIES_TIME_ATTRIBUTE));
results.setTotal(rootResults.getString(STORIES_TOTAL_ATTRIBUTE));
//@Deprecated in JSON v2.0
if(rootResults.has(STORIES_OFFSET_ATTRIBUTE))
results.setOffset(rootResults.getString(STORIES_OFFSET_ATTRIBUTE));
if(rootResults.has(STORIES_VERSION_ATTRIBUTE))
results.setVersion(rootResults.getString(STORIES_VERSION_ATTRIBUTE));
System.out.println("results Count = "+results.getCount());
System.out.println("results Total = "+results.getTotal());
// Start collecting the list of results
List<DiggSearchStoryResultItem> searchResultList = new ArrayList<DiggSearchStoryResultItem>();
// Add each story
for (int i = 0; i < storiesElement.length(); ++i) {
JSONObject keyElement = storiesElement.getJSONObject(i);
// Cursor element
JSONObject cur = null;
DiggSearchStoryResultItem resultItem = new DiggSearchStoryResultItem();
// Story Element
resultItem.setStoryComments(keyElement.getString(STORY_COMMENTS_ATTRIBUTE));
resultItem.setDescription(keyElement.getString(DIGG_DESCRIPTION_ELEMENT));
resultItem.setStoryDiggs(keyElement.getString(STORY_DIGGS_ATTRIBUTE));
resultItem.setStoryHref(keyElement.getString(STORY_HREF_ATTRIBUTE));
resultItem.setStoryId(keyElement.getString(STORY_ID_ATTRIBUTE));
resultItem.setStoryLink(keyElement.getString(STORY_LINK_ATTRIBUTE));
resultItem.setStoryMedia(keyElement.getString(STORY_MEDIA_ATTRIBUTE));
resultItem.setStoryStatus(keyElement.getString(STORY_STATUS_ATTRIBUTE));
resultItem.setStorySubmitDate(keyElement.getString(STORY_SUBMITDATE_ATTRIBUTE));
resultItem.setTitle(keyElement.getString(DIGG_TITLE_ELEMENT));
// User Info
cur = (JSONObject)getJSONElementChecked(keyElement,DIGG_USER_ELEMENT);
if(cur!=null){
resultItem.setUserIcon(cur.getString(USER_ICON_ATTRIBUTE));
resultItem.setUserName(cur.getString(USER_NAME_ATTRIBUTE));
resultItem.setUserProfileviews(cur.getString(USER_PROFILEVIEWS_ATTRIBUTE));
resultItem.setUserRegistered(cur.getString(USER_REGISTERED_ATTRIBUTE));
/* @since digg Apis v2.0*/
resultItem.setUserFullName(cur.getString(USER_FULLNAME_ATTRIBUTE));
// TODO The links can be commented because it is irrelevant to the Stories data.
if(cur.getJSONArray(USER_LINKS_ATTRIBUTE)!=null)
resultItem.setUserLinks(cur.getJSONArray(USER_LINKS_ATTRIBUTE).toString());
}
// Topic
cur = (JSONObject)getJSONElementChecked(keyElement,DIGG_TOPIC_ELEMENT);
if(cur!=null){
resultItem.setTopicName(cur.getString(TOPIC_NAME_ATTRIBUTE));
resultItem.setTopicShortName(cur.getString(TOPIC_SHORTNAME_ATTRIBUTE));
}
// Container
cur = (JSONObject)getJSONElementChecked(keyElement,DIGG_CONTAINER_ELEMENT);
if(cur!=null){
resultItem.setContainerName(cur.getString(CONTAINER_NAME_ATTRIBUTE));
resultItem.setContainerShortName(cur.getString(CONTAINER_SHORTNAME_ATTRIBUTE));
}
// Short_url
cur = (JSONObject)getJSONElementChecked(keyElement,DIGG_SHORTURL_ELEMENT);
if(cur!=null){
resultItem.setShorturl(cur.getString(SHORTURL_URL_ATTRIBUTE));
resultItem.setShorturlViewCount(cur.getString(SHORTURL_VIEW_COUNT_ATTRIBUTE));
}
/* @since digg Apis v2.0*/
// Thumbnail
cur = keyElement.getJSONObject(DIGG_THUMBNAIL_ELEMENT);
if(cur!=null){
resultItem.setThumbnailContentType(cur.getString(THUMBNAIL_CONTENT_TYPE_ATTRIBUTE));
resultItem.setThumbnailSrc(cur.getString(THUMBNAIL_SRC_ATTRIBUTE));
resultItem.setThumbnailHeight(cur.getString(THUMBNAIL_HEIGHT_ATTRIBUTE));
resultItem.setThumbnailWidth(cur.getString(THUMBNAIL_WIDTH_ATTRIBUTE));
resultItem.setThumbnailOriginalHeight(cur.getString(THUMBNAIL_ORIGINAL_HEIGHT_ATTRIBUTE));
resultItem.setThumbnailOriginalWidth(cur.getString(THUMBNAIL_ORIGINAL_WIDTH_ATTRIBUTE));
}
// Add the populated item to the resultList
searchResultList.add(resultItem);
}
results.setSearchResultList(searchResultList);
return results;
}