1
|
package eu.dnetlib.data.collector.plugins.schemaorg.httpapi.kaggle;
|
2
|
|
3
|
import eu.dnetlib.data.collector.plugins.schemaorg.RepositoryIterable;
|
4
|
import eu.dnetlib.data.collector.plugins.schemaorg.RepositoryQueueIterator;
|
5
|
import eu.dnetlib.data.collector.plugins.schemaorg.httpapi.HttpApiRepositoryIterable;
|
6
|
import org.apache.commons.io.IOUtils;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.json.JSONArray;
|
10
|
import org.json.JSONObject;
|
11
|
|
12
|
import java.net.URL;
|
13
|
import java.nio.charset.Charset;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.concurrent.ArrayBlockingQueue;
|
16
|
import java.util.concurrent.ExecutorService;
|
17
|
import java.util.concurrent.Executors;
|
18
|
|
19
|
public class KaggleRepositoryIterable implements HttpApiRepositoryIterable {
|
20
|
private static final Log log = LogFactory.getLog(KaggleRepositoryIterable.class);
|
21
|
|
22
|
public static class Options {
|
23
|
private String queryUrl;
|
24
|
private String queryPagePlaceholder;
|
25
|
private Charset charset;
|
26
|
private String responsePropertyTotalDataset;
|
27
|
private String responsePropertyDatasetList;
|
28
|
private String responsePropertyDatasetUrl;
|
29
|
private String responseBaseDatasetUrl;
|
30
|
|
31
|
private RepositoryQueueIterator.Options repositoryQueueIteratorOptions;
|
32
|
|
33
|
private int queueSize;
|
34
|
|
35
|
public int getQueueSize() {
|
36
|
return queueSize;
|
37
|
}
|
38
|
|
39
|
public void setQueueSize(int queueSize) {
|
40
|
this.queueSize = queueSize;
|
41
|
}
|
42
|
|
43
|
public String getResponseBaseDatasetUrl() {
|
44
|
return responseBaseDatasetUrl;
|
45
|
}
|
46
|
|
47
|
public void setResponseBaseDatasetUrl(String responseBaseDatasetUrl) {
|
48
|
this.responseBaseDatasetUrl = responseBaseDatasetUrl;
|
49
|
}
|
50
|
|
51
|
public RepositoryQueueIterator.Options getRepositoryQueueIteratorOptions() {
|
52
|
return repositoryQueueIteratorOptions;
|
53
|
}
|
54
|
|
55
|
public void setRepositoryQueueIteratorOptions(RepositoryQueueIterator.Options repositoryQueueIteratorOptions) {
|
56
|
this.repositoryQueueIteratorOptions = repositoryQueueIteratorOptions;
|
57
|
}
|
58
|
|
59
|
public String getResponsePropertyDatasetUrl() {
|
60
|
return responsePropertyDatasetUrl;
|
61
|
}
|
62
|
|
63
|
public void setResponsePropertyDatasetUrl(String responsePropertyDatasetUrl) {
|
64
|
this.responsePropertyDatasetUrl = responsePropertyDatasetUrl;
|
65
|
}
|
66
|
|
67
|
public String getResponsePropertyDatasetList() {
|
68
|
return responsePropertyDatasetList;
|
69
|
}
|
70
|
|
71
|
public void setResponsePropertyDatasetList(String responsePropertyDatasetList) {
|
72
|
this.responsePropertyDatasetList = responsePropertyDatasetList;
|
73
|
}
|
74
|
|
75
|
public String getResponsePropertyTotalDataset() {
|
76
|
return responsePropertyTotalDataset;
|
77
|
}
|
78
|
|
79
|
public void setResponsePropertyTotalDataset(String responsePropertyTotalDataset) {
|
80
|
this.responsePropertyTotalDataset = responsePropertyTotalDataset;
|
81
|
}
|
82
|
|
83
|
public Charset getCharset() {
|
84
|
return charset;
|
85
|
}
|
86
|
|
87
|
public void setCharset(Charset charset) {
|
88
|
this.charset = charset;
|
89
|
}
|
90
|
|
91
|
public String getQueryPagePlaceholder() {
|
92
|
return queryPagePlaceholder;
|
93
|
}
|
94
|
|
95
|
public void setQueryPagePlaceholder(String queryPagePlaceholder) {
|
96
|
this.queryPagePlaceholder = queryPagePlaceholder;
|
97
|
}
|
98
|
|
99
|
public String getQueryUrl() {
|
100
|
return queryUrl;
|
101
|
}
|
102
|
|
103
|
public void setQueryUrl(String queryUrl) {
|
104
|
this.queryUrl = queryUrl;
|
105
|
}
|
106
|
}
|
107
|
|
108
|
private Options options;
|
109
|
private ArrayBlockingQueue<String> queue;
|
110
|
|
111
|
public KaggleRepositoryIterable(Options options) {
|
112
|
this.options = options;
|
113
|
// this.currentPage = 1;
|
114
|
// this.terminated = false;
|
115
|
}
|
116
|
|
117
|
public void bootstrap() {
|
118
|
this.queue = new ArrayBlockingQueue<>(this.options.getQueueSize());
|
119
|
|
120
|
Thread ft = new Thread(new Harvester() );
|
121
|
ft.start();
|
122
|
// ExecutorService executor = Executors.newSingleThreadExecutor();
|
123
|
// executor.execute(new Harvester());
|
124
|
// executor.shutdown();
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public Iterator<String> iterator() {
|
129
|
return new RepositoryQueueIterator(this.options.getRepositoryQueueIteratorOptions(), this.queue);
|
130
|
}
|
131
|
|
132
|
private class Harvester implements Runnable{
|
133
|
|
134
|
@Override
|
135
|
public void run() {
|
136
|
this.execute();
|
137
|
}
|
138
|
private void execute() {
|
139
|
try {
|
140
|
int currentPage = 1;
|
141
|
int totalDatasets = 0;
|
142
|
int readDatasets = 0;
|
143
|
while (true) {
|
144
|
String query = options.getQueryUrl().replace(options.getQueryPagePlaceholder(), Integer.toString(currentPage));
|
145
|
String response = IOUtils.toString(new URL(query), options.getCharset());
|
146
|
currentPage += 1;
|
147
|
|
148
|
JSONObject pageObject = new JSONObject(response);
|
149
|
totalDatasets = pageObject.optInt(options.getResponsePropertyTotalDataset());
|
150
|
JSONArray datasets = pageObject.optJSONArray(options.getResponsePropertyDatasetList());
|
151
|
|
152
|
if (datasets == null || datasets.length() == 0) break;
|
153
|
|
154
|
readDatasets += datasets.length();
|
155
|
|
156
|
for (int i = 0; i < datasets.length(); i += 1) {
|
157
|
JSONObject item = datasets.optJSONObject(i);
|
158
|
String urlFragment = item.optString(options.getResponsePropertyDatasetUrl());
|
159
|
if (urlFragment == null || urlFragment.trim().length() == 0) continue;
|
160
|
String endpoint = String.format("%s%s", options.getResponseBaseDatasetUrl(), urlFragment);
|
161
|
|
162
|
log.debug("adding endpoint in queue");
|
163
|
log.debug("queue size: " + queue.size());
|
164
|
queue.put(endpoint);
|
165
|
log.debug("endpoint added in queue");
|
166
|
log.debug("queue size: " + queue.size());
|
167
|
}
|
168
|
|
169
|
if (readDatasets >= totalDatasets) break;
|
170
|
}
|
171
|
} catch (Exception ex) {
|
172
|
log.error("problem execution harvesting", ex);
|
173
|
} finally {
|
174
|
try {
|
175
|
queue.put(RepositoryIterable.TerminationHint);
|
176
|
} catch (Exception ex) {
|
177
|
log.fatal("could not add termination hint. the process will not terminate gracefully", ex);
|
178
|
}
|
179
|
}
|
180
|
}
|
181
|
}
|
182
|
}
|