New York Times API access coding help
in
Programming Questions
•
2 years ago
I've adapted code from Jer Thorp's NYT API Classes (
http://blog.blprnt.com/blog/blprnt/multi-faceted-searching-with-the-nytimes-apis) to try access the Most Popular articles API. However I keep getting an
unexpected token: void error. I have posted the code below, I apologize for sloppy labeling. Can anyone help me figure out what is wrong?
- import org.json.*;
import java.util.ArrayList;
void setup() {
NYTMostPopular p = new NYTMostPopular();
NYTMostPopularResults r = p.runSearch();
println ("There were" + r + "results");
}
public class NYTPopularObject {
String url;
String column;
String section;
String byline;
String title;
String _abstract;
String date;
String facets;
NYTPopularObject() {};
}
//call to run search
public class NYTMostPopular {
ArrayList fields = new ArrayList();
String baseURL = "http://api.nytimes.com/svc/mostpopular/";
String apiKey = "apikey" ; YOUR API KEY GOES HERE
String version = "v2";
String resourcetype = "mostviewed";
String section = "all-sections";
String timeperiod = "1"; //1 ,7 or 30
String defaultFields = "url,column,section,byline,title,_abstract,date,des_facet,org_facet,per_facet,geo_facet";
//Constructor
NYTMostPopular() {
};
//Runs search
NYTMostPopularResults runSearch() {
NYTMostPopularResults mp = new NYTMostPopularResults();
try {
String url = constructURL();
println(url);
String JSONStr = join(loadStrings(url), "");
JSONObject nytPopular = new JSONObject(JSONStr);
mp.processResults(nytPopular.getJSONArray("results"), getFields());
mp.total = int(nytPopular.getInt("total"));
}
catch (JSONException e) {
println (e.toString());
}
return(mp);
}
String getFields() {
String q = "";
for (int i = 0; i < fields.size(); i++) {
q += (String) fields.get(i);
if (i < fields.size() - 1) q += ",";
}
if (q == "") {
q = defaultFields;
return(q);
}
//build URL to access NYT
String constructURL() {
String url = baseURL + version + "/" +resourcetype + "/" + section + "/" +timeperiod + "?api-key=" + apiKey;
return(url);
println(url);
}
}- //organizes search results
public class NYTMostPopularResults {
String cmonth;
String cyear;
NYTPopularObject[] results;
NYTMostPopularResults() {}
void processResults(JSONArray e, String f) {
println("PROCESS");
results = new NYTPopularObject[e.length()];
for (int i=0; i < e.length(); i++) {
String[] fields = split(f, ",");
NYTPopularObject r = new NYTPopularObject();
JSONObject o;
try {
o = e.getJSONObject(i);
for (int j = 0; j < fields.length; j++) {
String s = fields[j];
if (match(s, "url") != null) {
//println("SET BODY");
r.body = o.getString("url");
}
else if (match(s, "column") != null) {
//println("SET TITLE");
//println(":" + o.getString("title"));
r.title = o.getString("column");
}
else if (match(s, "section") != null) {
r._abstract = o.getString("section");
}
else if (match(s, "byline") != null) {
r.url = o.getString("byline");
}
else if (match(s, "title") != null) {
r.author = o.getString("title");
}
else if (match(s, "_abstract") != null) {
r.date = o.getString("_abstract");
}
else if (match(s, "date") != null) {
r.byline = o.getString("date");
}
else if (match(s, "des_facet")||match(s, "org_facet")||match(s, "per_facet")||match(s, "geo_facet")) != null) {
r.byline = o.getString("facets");
}
}
catch (JSONException je) {}
results[i] = r;
}
}
}
}
1