X

Geertjan's Blog

  • September 11, 2010

Yahoo News on the Command Line

Geertjan Wielenga
Product Manager
One of my current favorite features in the IDE is the Web Service Registry:

You can register web services there yourself and/or use the defaults provided by the IDE. Expand a node and you will end up with the specific method on the web service you'd like to invoke. Then you can drag that node into the Java editor. When you do so, all the background files are created (e.g., even including the file that will hold a registration key, if any, such as required by Yahoo, for example). Plus, you get a chunk of code in your Java class for invoking and processing the method on the web service.

And so that's how I ended up with the code below. Most of it comes from the node I dropped into the Java class, the rest of it is from the NetBeans Command Line Parsing API:

@ServiceProvider(service = OptionProcessor.class)
public class YahooNewsOptionProcessor extends OptionProcessor {
private Option news = Option.requiredArgument('n', "news");
private Option language = Option.requiredArgument('l', "language");
@Override
protected Set<Option> getOptions() {
return Collections.singleton(OptionGroups.allOf(news, language));
}
@Override
protected void process(Env env, Map<Option, String[]> values) throws CommandException {
String[] args1 = (String[]) values.get(news);
String[] args2 = (String[]) values.get(language);
if (args1.length > 0) {
try {
String query = args1[0];
String type = "all";
java.lang.Integer results = 10;
java.lang.Integer start = 1;
String sort = "rank";
String language = args2[0];
String output = "xml";
String callback = null;
RestResponse result = YahooNewsSearchService.search(query, type, results, start, sort, language, output, callback);
if (result.getDataAsObject(yahoo.newssearchservice.newssearchresponse.ResultSet.class) instanceof yahoo.newssearchservice.newssearchresponse.ResultSet) {
yahoo.newssearchservice.newssearchresponse.ResultSet resultObj = result.getDataAsObject(yahoo.newssearchservice.newssearchresponse.ResultSet.class);
List<ResultType> result1 = resultObj.getResult();
for (ResultType resultType : result1) {
String summary = resultType.getSummary();
System.out.println("--->");
System.out.println(summary);
}
}
LifecycleManager.getDefault().exit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}

I then created a ZIP distribution of the application. Then I unzipped that and went to the command line. I typed this:

yahoonews --news "world peace" --language "en"

And then I had Yahoo News on the command line:

I intend to include this demo, or one very much like it, in a BOF I am doing with Milan Kuchtiak on Wednesday, 22 September, at 14:15, entitled "Hello RRCD: Rapid REST Client Development".

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.