1
|
package eu.dnetlib.data.collector.plugins.HTTPWithFileName;
|
2
|
|
3
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
4
|
import org.apache.commons.logging.Log;
|
5
|
import org.apache.commons.logging.LogFactory;
|
6
|
import org.json.JSONObject;
|
7
|
import org.json.XML;
|
8
|
import org.jsoup.Jsoup;
|
9
|
import org.jsoup.nodes.Document;
|
10
|
import org.jsoup.nodes.Element;
|
11
|
import org.jsoup.select.Elements;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Iterator;
|
15
|
|
16
|
import java.util.concurrent.ArrayBlockingQueue;
|
17
|
import java.util.function.Consumer;
|
18
|
|
19
|
/**
|
20
|
* Created by miriam on 04/05/2018.
|
21
|
*/
|
22
|
public class HTTPWithFileNameCollectorIterable implements Iterable<String> {
|
23
|
private static final Log log = LogFactory.getLog(HTTPWithFileNameCollectorIterable.class);
|
24
|
|
25
|
private final ArrayList<String> urls = new ArrayList<>();
|
26
|
private final ArrayList<String> metas = new ArrayList<String>();
|
27
|
|
28
|
|
29
|
public HTTPWithFileNameCollectorIterable(String startUrl){
|
30
|
|
31
|
urls.add(startUrl);
|
32
|
}
|
33
|
|
34
|
private String addFilePath(String meta,String url, boolean isJson){
|
35
|
String path = url.replace("metadata", "pdf");
|
36
|
try {
|
37
|
if(isJson)
|
38
|
meta = meta.substring(0, meta.length() - 1) + ",'downloadFileUrl':'" + path.substring(0, path.indexOf(".json")) + ".pdf'}";
|
39
|
else{
|
40
|
int index = meta.lastIndexOf("</");
|
41
|
meta = meta.substring(0, index) + "<downloadFileUrl>" + path.substring(0, path.indexOf(".xml")) + ".pdf</downloadFileUrl>" + meta.substring(index);
|
42
|
}
|
43
|
|
44
|
}catch(Exception ex){
|
45
|
log.info("not file with extension .json or .xml");
|
46
|
}
|
47
|
|
48
|
//JSONObject jsonobj = new JSONObject("{'metadata':"+json+"}");
|
49
|
if(isJson){
|
50
|
JSONObject jsonobj = new JSONObject(meta);
|
51
|
return XML.toString(jsonobj);
|
52
|
}
|
53
|
return meta;
|
54
|
}
|
55
|
|
56
|
private void recurFolder(String text, String url){
|
57
|
Document doc = Jsoup.parse(text);
|
58
|
Elements links = doc.select("a");
|
59
|
for(Element e:links){
|
60
|
if (!e.text().equals("../")){
|
61
|
String file = e.attr("href");
|
62
|
if(file.endsWith(".json") || file.endsWith(".xml"))
|
63
|
metas.add(url+file);
|
64
|
else
|
65
|
urls.add(url+file);
|
66
|
}
|
67
|
}
|
68
|
}
|
69
|
|
70
|
|
71
|
@Override
|
72
|
public Iterator<String> iterator() {
|
73
|
final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(100);
|
74
|
|
75
|
|
76
|
return new Iterator<String>(){
|
77
|
|
78
|
public void fillQueue() {
|
79
|
Connector c = new Connector();
|
80
|
String url;
|
81
|
while((metas.size()>0 || urls.size() > 0 ) && queue.size()<100){
|
82
|
if (metas.size() > 0){
|
83
|
url = metas.remove(0);
|
84
|
try {
|
85
|
c.get(url);
|
86
|
} catch (CollectorServiceException e) {
|
87
|
log.error("Impossible to collect url: " + url + " error: " + e.getMessage());
|
88
|
}
|
89
|
if(c.isStatusOk()){
|
90
|
try {
|
91
|
String ret = c.getResponse();
|
92
|
if (ret != null && ret.length()>0)
|
93
|
queue.put(addFilePath(ret,url,url.endsWith(".json")));
|
94
|
} catch (InterruptedException e) {
|
95
|
log.error("not inserted in queue element associate to url " + url + " error: " + e.getMessage() );
|
96
|
|
97
|
}
|
98
|
}
|
99
|
}else{
|
100
|
url = urls.remove(0);
|
101
|
try {
|
102
|
c.get(url);
|
103
|
} catch (CollectorServiceException e) {
|
104
|
log.error("Impossible to collect url: " + url + " error: " + e.getMessage());
|
105
|
}
|
106
|
if(c.isStatusOk()){
|
107
|
if (c.responseTypeContains("text/html")){
|
108
|
recurFolder(c.getResponse(),url);
|
109
|
}
|
110
|
else if(c.responseTypeContains("application/json") || c.responseTypeContains("application/xml")){
|
111
|
try {
|
112
|
queue.put(addFilePath(c.getResponse(),url, c.responseTypeContains("application/json")));
|
113
|
} catch (InterruptedException e) {
|
114
|
log.error("not inserted in queue element associate to url " + url + " error: " + e.getMessage() );
|
115
|
}
|
116
|
}
|
117
|
}
|
118
|
|
119
|
}
|
120
|
|
121
|
}
|
122
|
|
123
|
}
|
124
|
@Override
|
125
|
public boolean hasNext() {
|
126
|
if (queue.isEmpty()){
|
127
|
fillQueue();
|
128
|
}
|
129
|
return (!queue.isEmpty());
|
130
|
}
|
131
|
|
132
|
@Override
|
133
|
public String next() {
|
134
|
return queue.poll();
|
135
|
}
|
136
|
|
137
|
@Override
|
138
|
public void remove() {
|
139
|
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public void forEachRemaining(Consumer<? super String> action) {
|
144
|
|
145
|
}
|
146
|
};
|
147
|
}
|
148
|
|
149
|
|
150
|
}
|