1
0
Fork 0

data fetcher has been implemented

This commit is contained in:
sab 2024-07-31 18:05:11 +02:00
parent d20a5e020a
commit 7f39375ba8
2 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package eu.dnetlib.dhp.transformation.xslt;
import com.mongodb.util.JSON;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
/**
* This class fetches JSON from a provided link and returns
* a Dublin Core. This functionality is particularly needed for OSF Preprints
*/
/**
* this method fetches JSON from a provided URL and returns it as JSON Object
*/
public class DataFetcher {
// fetch data
static JSONObject getJson(URL url) throws IOException {
String json = IOUtils.toString(url);
return new JSONObject(json);
}
/**
* This method extracts authors (contributors) from a given JSON
* @param jsonObject
* @return
*/
static List<String> getContributorsFromJson(JSONObject jsonObject){
List<String> contributors;
// count of authors
int countAuthors = jsonObject.getJSONArray("data").length();
// jsonObject.getJSONArray("data").forEach(json1 -> System.out.println(json1));
// String x = testJsonObj
// .getJSONArray("data")
// .getJSONObject(0)
// .getJSONObject("embeds")
// .getJSONObject("users")
// .getJSONObject("data")
// .getJSONObject("attributes")
// .getString("full_name");
return null;
}
// transform it into Dublin Core
private void transformToDublinCore(JSON jsonData) {
System.out.println(jsonData);
}
}

View File

@ -0,0 +1,51 @@
package eu.dnetlib.dhp.transformation.xslt;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
class DataFetcherTest {
@BeforeEach
void setUp() {
}
@AfterEach
void tearDown() {
}
@Test
void getJson() throws IOException, URISyntaxException {
URL contributorsUrl = new URI("https://api.osf.io/v2/preprints/mrwqb/contributors/?format=json").toURL();
JSONObject testJsonObj = DataFetcher.getJson(contributorsUrl);
String x = testJsonObj
.getJSONArray("data")
.getJSONObject(0)
.getJSONObject("embeds")
.getJSONObject("users")
.getJSONObject("data")
.getJSONObject("attributes")
.getString("full_name");
System.out.println(x);
System.out.println(testJsonObj.getJSONArray("data").length());
testJsonObj.getJSONArray("data").forEach(json1 -> System.out.println(json1));
}
}