/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package DataAcquisition;
import com.google.code.bing.search.client.BingSearchClient;
import com.google.code.bing.search.client.BingSearchClient.SearchRequestBuilder;
import com.google.code.bing.search.client.BingSearchServiceClientFactory;
import com.google.code.bing.search.schema.AdultOption;
import com.google.code.bing.search.schema.SearchOption;
import com.google.code.bing.search.schema.SearchRequest;
import com.google.code.bing.search.schema.SearchResponse;
import com.google.code.bing.search.schema.SourceType;
import com.google.code.bing.search.schema.web.WebResult;
import com.google.code.bing.search.schema.web.WebSearchOption;
import java.util.ArrayList;
/**
*
* @author Muhammad Zahran
*/
public class Bing
{
/** The Constant APPLICATION_KEY_OPTION. */
private static final String APPLICATION_KEY_OPTION = "appid";
/** The Constant QUERY_OPTION. */
private static String QUERY_OPTION = "";
//private static String QUERY_OPTION = "Alienware MX11 site:http://www.amazon.com/";
/** The Constant HELP_OPTION. */
private static final String HELP_OPTION = "help";
public static SearchResponse response = new SearchResponse();
public static void setQuery(String q, String site)
{
String webSite = "http://"+site;
QUERY_OPTION = q+" site:"+webSite;
}
public static void doWork()
{
try
{
BingSearchServiceClientFactory factory = BingSearchServiceClientFactory.newInstance();
BingSearchClient client = factory.createBingSearchClient();
//SearchResponse response = client.search(createSearchRequest(client, line.getOptionValue(APPLICATION_KEY_OPTION), line.getOptionValue(QUERY_OPTION)));
SearchRequest sr = createSearchRequest(client, "4F2CCC4044C7CC88B0BAB03636FD8625FADE6F5A", QUERY_OPTION);
response = client.search(sr);
//printResponse(response);
}
catch(Exception e)
{
System.out.println(e.getMessage());
//System.out.println(e.getStackTrace());
}
}
public static void printResponse()
{
System.out.println("Bing API Version " + response.getVersion());
System.out.println("Web results for " + response.getQuery().getSearchTerms());
System.out.println("Hits: "+response.getWeb().getResults().size());
System.out.println("--------------------------------------");
for (WebResult result : response.getWeb().getResults())
{
System.out.println("Title: "+result.getTitle());
System.out.println("Description: "+result.getDescription());
System.out.println("URL: "+result.getUrl());
System.out.println("Result: "+result.getDateTime());
System.out.println("--------------------------------------");
}
}
public static ArrayList<String> getURLs()
{
ArrayList<String> urls = new ArrayList<String>();
for (WebResult result : response.getWeb().getResults())
{
urls.add(result.getUrl());
}
return urls;
}
/**
* Creates the search request.
*
* @param client the client
* @param applicationId the application id
* @param query the query
*
* @return the search request
*/
private static SearchRequest createSearchRequest
(BingSearchClient client, String applicationId, String query)
{
SearchRequestBuilder builder = client.newSearchRequestBuilder();
builder.withAppId(applicationId);
builder.withQuery(query);
builder.withSourceType(SourceType.WEB);
builder.withVersion("2.0");
builder.withMarket("en-us");
builder.withAdultOption(AdultOption.STRICT);
builder.withSearchOption(SearchOption.ENABLE_HIGHLIGHTING);
builder.withWebRequestCount(50L);
builder.withWebRequestOffset(0L);
builder.withWebRequestSearchOption(WebSearchOption.DISABLE_HOST_COLLAPSING);
builder.withWebRequestSearchOption(WebSearchOption.DISABLE_QUERY_ALTERATIONS);
return builder.getResult();
}
}