Hi all,
I really appreciate if you take the time and help me solve this issue as I have spent hours and haven’t made any progress.
What’s the project
I’m creating a data visualization project using New York Times Article API and Processing. The idea is to show all images published in NYTimes with a certain keyword. Then the user hovers over each image and gets to see the information about that specific images such as the year, the page it was published and etc.
What’s the problem
With every request, NYTimes API returns only 10 results (not 10 images but 10 articles). In order to be able to show all images in the sketch I need to have a JSON file with all the images’ URLs.
What I’ve tried
I figured maybe making a master JSON file helps. I can copy the first 10 results, then I’ll go to the second page (through a for loop) of the NYT JSON and copy the next 10 results and so on until there are no more articles.
So far I have looked into these functions: saveJSONObject, saveJSONArray, arrayCopy(); and concat();
Questions
- Have you had a similar experience with NYTimes API? How have you solved this problem?
- Is it possible to merge JSON files through Processing?
- Is there another way to do the same thing?
Here’s the code
String apiKey = "PUTYOURAPIKEYSTRINGHERE";
String url = "https://api.nytimes.com/svc/search/v2/articlesearch.json";
String query = "?query=iran";
int page = 0; // each page shows 10 results
String beginDate = "&begin_date=20080101";
String endDate = "&end_date=20181030";
//String filter = "&fl=multimedia";
//String nyURL = "https://www.nytimes.com/";
//String[] multimedia = new String[900];
//String imageURL;
JSONObject json; //nytimes api json
JSONArray master; // the local json file that will be the master json file
JSONArray test; // another local json file, I want to copy every 10 new search results into and then copy this array to the master array, if I understood it well processing differentiates between arrays and json arrays
void setup() {
json = loadJSONObject(url+query+"&page="+page+beginDate+endDate+"&api-key="+apiKey);
for (int i = 0; i < json.getJSONObject ("response").getJSONArray("docs").size(); i++) { // look at all objects in "docs"
//json = loadJSONObject(url+query+"&page="+page+beginDate+endDate+"&api-key="+apiKey);
JSONArray mm = json.getJSONObject("response").getJSONArray("docs").getJSONObject(i).getJSONArray("multimedia"); // find the "multimedia" array in each object
if ( json.getJSONObject("response").getJSONArray("docs").getJSONObject(i).getJSONArray("multimedia") != null) { //if multimedia is not null
saveJSONArray(mm, "data/test.json"); // save multimedia content into test.json
JSONArray test = loadJSONArray("data/test.json"); // load test.json
if(page==0){
arrayCopy(test,master); //copy whatever's in test to master
}
//println(page);
page = page + 1;
if (page != 0) { // if page is not 0, meaning there are already some stuff written in the master file
//arrayCopy(test,0,master,69,test.size()); // I wrote 69 beacuse I know page 0 includes 68 indexes
//master = concat(test, mm);
println(master.size());
}
//saveJSONObject(json, "data/test.json");
////master = loadJSONObject("data/test.json");
//for (int page = 0; json != null; page += 1) {
// master = concat(json,master);
// print(master);
//}
}
}
}