1
|
package eu.dnetlib.efg.download.plugin;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.List;
|
6
|
import java.util.stream.Collectors;
|
7
|
import java.util.stream.Stream;
|
8
|
|
9
|
import com.google.gson.Gson;
|
10
|
import eu.dnetlib.enabling.tools.DnetStreamSupport;
|
11
|
import eu.dnetlib.rmi.data.AbstractDownloadPlugin;
|
12
|
import eu.dnetlib.rmi.data.DownloadItem;
|
13
|
import eu.dnetlib.rmi.data.DownloadPlugin;
|
14
|
import eu.dnetlib.rmi.data.DownloadPluginException;
|
15
|
|
16
|
public class EFGDownloadPlugin extends AbstractDownloadPlugin implements DownloadPlugin {
|
17
|
|
18
|
private final static List<String> suffix = Arrays.asList("jpg", "jpeg", "gif", "png", "pdf", "ps", "djvu", "scanpix");
|
19
|
|
20
|
@Override
|
21
|
public String extractURL(final String baseURL) throws DownloadPluginException {
|
22
|
if (baseURL == null)
|
23
|
return null;
|
24
|
List<String> collect = suffix.stream().filter(s -> baseURL.toLowerCase().trim().endsWith(s)).collect(Collectors.toList());
|
25
|
if (collect.size() > 0)
|
26
|
return baseURL;
|
27
|
return null;
|
28
|
}
|
29
|
|
30
|
@Override
|
31
|
public void setBasePath(final String basePath) {
|
32
|
|
33
|
}
|
34
|
|
35
|
@Override
|
36
|
public DownloadItem retrieveUrl(final DownloadItem input) throws DownloadPluginException {
|
37
|
try {
|
38
|
String url = input.getOriginalUrl();
|
39
|
if (url == null || url.trim().length() == 0) {
|
40
|
return input;
|
41
|
}
|
42
|
List<String> urls = new Gson().fromJson(url, ArrayList.class);
|
43
|
if (urls == null || urls.size() == 0) {
|
44
|
input.setUrl(null);
|
45
|
input.setOriginalUrl(null);
|
46
|
return input;
|
47
|
}
|
48
|
if (checkUrlsNotNull(input, urls))
|
49
|
return input;
|
50
|
input.setOriginalUrl(null);
|
51
|
input.setUrl(null);
|
52
|
return input;
|
53
|
} catch (Throwable e) {
|
54
|
throw new DownloadPluginException("error on retrieve correct Url");
|
55
|
}
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
public Iterable<DownloadItem> retrieveUrls(final Iterable<DownloadItem> items) throws DownloadPluginException {
|
60
|
return DnetStreamSupport.generateStreamFromIterator(items.iterator()).map(it -> retrieveUrl(it))::iterator;
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public String getPluginName() {
|
65
|
return "EFGDownloadPlugin";
|
66
|
}
|
67
|
}
|