2022-06-06 09:37:29 +02:00
|
|
|
package eu.dnetlib.bmuse_webapp.publisher;
|
|
|
|
|
|
|
|
import eu.dnetlib.bmuse_webapp.ServiceScrapeDriver;
|
|
|
|
import eu.dnetlib.common.controller.AbstractDnetController;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.apache.commons.io.LineIterator;
|
2022-06-07 13:02:36 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2022-06-06 09:37:29 +02:00
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author enrico.ottonello
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/api")
|
|
|
|
public class BMUSEWebappController extends AbstractDnetController {
|
|
|
|
|
2022-06-07 13:02:36 +02:00
|
|
|
@Value("${outputFolder}")
|
|
|
|
private String outputFolder;
|
|
|
|
@Value("${outputDataPattern}")
|
|
|
|
private String outputDataPattern;
|
2022-06-06 09:37:29 +02:00
|
|
|
|
2022-06-07 13:02:36 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(BMUSEWebappController.class);
|
|
|
|
|
|
|
|
@RequestMapping(value = "/startScraping", method = RequestMethod.GET)
|
|
|
|
public String startScraping(@RequestParam final String datasourceKey, @RequestParam final String sitemapUrl) throws BMUSEWebappException, IOException {
|
2022-06-06 09:37:29 +02:00
|
|
|
|
2022-06-07 13:02:36 +02:00
|
|
|
logger.info("<STARTSCRAPING> datasourceKey: "+datasourceKey+" sitemapUrl:"+sitemapUrl);
|
2022-06-06 09:37:29 +02:00
|
|
|
|
|
|
|
String sitemapUrlKey = "loc";
|
2022-06-07 13:02:36 +02:00
|
|
|
String outputFilename = datasourceKey.concat(getOutputDataPattern());
|
2022-06-06 09:37:29 +02:00
|
|
|
ServiceScrapeDriver service = new ServiceScrapeDriver(sitemapUrl, sitemapUrlKey, null, outputFilename);
|
|
|
|
service.start();
|
|
|
|
return "started";
|
|
|
|
}
|
|
|
|
|
2022-06-07 13:02:36 +02:00
|
|
|
@RequestMapping(value = "/getNQuads", method = RequestMethod.GET)
|
|
|
|
public String getNQuads(@RequestParam final String datasourceKey, HttpServletResponse response) throws BMUSEWebappException, IOException {
|
|
|
|
|
|
|
|
logger.info("<GETNQUADS> datasourceKey: "+datasourceKey);
|
|
|
|
|
|
|
|
LineIterator it = FileUtils.lineIterator(new File(getOutputFolder().concat("/").concat(datasourceKey).concat(getOutputDataPattern())), "UTF-8");
|
2022-06-06 09:37:29 +02:00
|
|
|
try {
|
|
|
|
while (it.hasNext()) {
|
|
|
|
String line = it.nextLine();
|
|
|
|
response.getOutputStream().write(line.getBytes(StandardCharsets.UTF_8));
|
|
|
|
response.getOutputStream().println();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2022-06-07 13:02:36 +02:00
|
|
|
|
|
|
|
public String getOutputFolder() {
|
|
|
|
return outputFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getOutputDataPattern() {
|
|
|
|
return outputDataPattern;
|
|
|
|
}
|
2022-06-06 09:37:29 +02:00
|
|
|
}
|