-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoller-what.java
289 lines (263 loc) · 7.77 KB
/
Poller-what.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* NewsMonitor
*
* Poller.java
*
* This class is responsible for managing and refreshing a list of feed URLs.
* It provides functionality to start, stop, and run the polling process,
* and handle feeds according to their content and relevance.
*
* @author danja
* @version 1.20.23
* @date 2023-08-14
*/
package it.danja.newsmonitor.main;
import it.danja.newsmonitor.io.HttpMessage;
import it.danja.newsmonitor.model.Feed;
import it.danja.newsmonitor.model.FeedList;
import it.danja.newsmonitor.model.Link;
import it.danja.newsmonitor.model.impl.FeedImpl;
import it.danja.newsmonitor.model.impl.FeedListImpl;
import it.danja.newsmonitor.sparql.SparqlTemplater;
import it.danja.newsmonitor.utils.ContentType;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Poller implements Runnable {
private static Logger log = LoggerFactory.getLogger(Poller.class);
private List<String> feedUrls = null;
private FeedList feedList = null;
/**
* Returns the feed list managed by the Poller.
*
* @return the feedList
*/
public FeedList getFeedList() {
return feedList;
}
private boolean running = false;
private Thread thread;
private int loopCount = 0;
private boolean stopped = false;
private SparqlTemplater sparqlTemplater = null;
private Properties config = null;
/**
* Constructor to initialize the Poller with configuration properties and SPARQL templater.
*
* @param config Configuration properties for the Poller
* @param sparqlTemplater SPARQL templater for handling feed data
*/
public Poller(Properties config, SparqlTemplater sparqlTemplater) {
this.sparqlTemplater = sparqlTemplater;
this.config = config;
feedList = new FeedListImpl(config);
}
/**
* Initializes feeds by creating Feed objects for each URL and adding them to the FeedList.
*
* @return the initialized FeedList
*/
public FeedList initFeeds() {
for (int i = 0; i < feedUrls.size(); i++) {
Feed feed = new FeedImpl(config);
feed.setUrl(feedUrls.get(i));
boolean redirect = feed.init();
if (redirect) {
String location = feed.getLocation();
feed = new FeedImpl(config);
feed.setUrl(location);
feed.init();
}
feedList.addFeed(feed);
}
log.info("==== FeedList ====");
log.info(feedList.toString());
return feedList;
}
/**
* Starts the polling process by creating a new thread.
*/
public void start() {
stopped = false;
thread = new Thread(this);
thread.start();
}
/**
* Stops the polling process.
*/
public void stop() {
running = false;
}
/**
* Main run method for the polling process. Continuously refreshes the feeds until stopped.
*/
@Override
public void run() {
running = true;
while (running) {
if (feedList.size() == 0) {
log.warn("No valid feeds, stopping poller...");
running = false;
break;
}
log.info("\n*** Starting loop #" + (++loopCount) + " ***");
log.info("Refreshing " + feedList.size() + " feeds...");
refreshFeeds();
}
log.info("Poller stopped.");
stopped = true;
}
/**
* Refreshes the feeds, handling expirations, relevancy, and other conditions.
*/
public synchronized void refreshFeeds() {
Set<Feed> expiring = new HashSet<Feed>();
Iterator<Feed> iterator = feedList.getList().iterator();
// feedQueue.iterator();
Feed feed = null;
while (iterator.hasNext()) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
feed = iterator.next();
// log.info("feed.getLives() = "+feed.getLives());
// log.info("feed.isDead() = "+feed.isDead());
// if(!feed.isDead() && feed.getLives() < Config.MAX_LIVES) {
// log.info("Less than max lives, re-initializing...");
// feed.init();
// // feed.setFirstCall(true);
// }
log.info("\nRefreshing : " + feed.getUrl());
// feed.setFirstCall(firstCall);
feed.refresh();
// log.info("\nin poller feed.getLinks() : " + feed.getLinks());
Set<Link> links = feed.getLinks();
for (Link link : links) {
feedList.addFeed(link.getHref());
}
if (feed.getFormatHint() == ContentType.HTML) { // shouldn't be
// needed
// feed.setDead(true);
log.info("Is HTML...");
}
if (feed.getLives() < 1) {
log.info("Lives gone...");
feed.setDead(true);
}
if (
feed.getRelevance() <
Float.parseFloat(config.getProperty("UNSUBSCRIBE_RELEVANCE_THRESHOLD"))
) {
log.info("Now below relevance threshold...");
feed.setDead(true);
}
if (feed.isDead()) {
log.info("Flagging as dead, skipping.");
// TODO is duplicated below
log.info("Unsubscribing from " + feed.getUrl());
feedList.remove(feed);
continue;
}
if (feed.shouldExpire()) {
expiring.add(feed);
}
try {
Thread.sleep(
Integer.parseInt(config.getProperty("PER_FEED_SLEEP_PERIOD"))
);
} catch (InterruptedException e) {
log.error(e.getMessage());
}
if (feed.isNew()) {
pushFeed(feed);
} else {
log.info("No changes to : " + feed.getUrl());
}
}
if ("true".equals(config.getProperty("POLLER_NO_LOOP"))) {
System.exit(1);
}
iterator = expiring.iterator();
while (iterator.hasNext()) {
feed = iterator.next();
log.info("Unsubscribing from " + feed.getUrl());
feedList.remove(feed);
}
try {
Thread.sleep(Integer.parseInt(config.getProperty("REFRESH_PERIOD")));
} catch (InterruptedException e) {
log.error(e.getMessage());
}
}
/**
* Pushes a feed to the server using SPARQL.
*
* @param feed the Feed object to be pushed
*/
private void pushFeed(Feed feed) {
System.out.println(
"<\n\n--------------------------------POST-------------------------->"
);
System.out.println("Uploading SPARQL for : " + feed.getUrl());
log.info("Uploading SPARQL for : " + feed.getUrl());
HttpMessage message = sparqlTemplater.uploadFeed(feed);
feed.clean();
log.info(
"SPARQL response = " +
message.getStatusCode() +
" " +
message.getStatusMessage()
);
if (message.getStatusCode() >= 400) {
log.info("\n" + message + "\n");
}
}
/**
* Displays the list of feeds to the logger.
*/
public void displayFeeds() {
Iterator<Feed> feedIterator = feedList.getList().iterator();
while (feedIterator.hasNext()) {
log.info(feedIterator.next().toString());
}
log.info("---------------");
// for (int i = 0; i < entries.size(); i++) {
// log.info(entries.getEntry(i));
// }
}
/**
* Sets the feed URLs to be managed by the Poller.
*
* @param feedUrls the list of feed URLs
*/
public void setFeedUrls(List<String> feedUrls) {
this.feedUrls = feedUrls;
List<String> tweakedUrls = new ArrayList<String>(); // little temp workaround for some old feeds
for (int i = 0; i < feedUrls.size(); i++) {
String url = feedUrls.get(i);
if (url.startsWith("http:")) {
String tweaked = "https:" + url.substring(5);
log.info("tweaked = " + tweaked);
tweakedUrls.add(tweaked);
}
}
this.feedUrls.addAll(tweakedUrls);
}
/**
* Checks if the Poller has stopped.
*
* @return true if the Poller has stopped, false otherwise
*/
public boolean isStopped() {
return stopped;
}
}