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
|
package org.psesquared.server.util;
import com.rometools.rome.feed.synd.SyndEnclosure;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.DateTimeException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.jdom2.Content;
import org.jdom2.Element;
import org.psesquared.server.episode.actions.api.data.access.EpisodeDao;
import org.psesquared.server.model.Episode;
import org.psesquared.server.model.Subscription;
import org.psesquared.server.subscriptions.api.data.access.SubscriptionDao;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
* The class responsible for fetching data from RSS feeds.
*/
@Component
@RequiredArgsConstructor
public class RssParser {
/**
* The Index of the Map in the List of Maps that uses GUIDs as keys.
*/
private static final int GUID_KEY_MAP_INDEX = 0;
/**
* The Index of the Map in the List of Maps that uses URLs as keys.
*/
private static final int URL_KEY_MAP_INDEX = 1;
/**
* The JPA repository that handles all episode related database requests.
*/
private final EpisodeDao episodeDao;
/**
* The JPA repository that handles all subscription related database requests.
*/
private final SubscriptionDao subscriptionDao;
/**
* Validates that the RSS-Feed associated with the Subscription is of the
* expected Format and that the Episodes of the Subscription are part of the
* feed.
* If the Feed is invalid the Subscription is deleted using the
* SubscriptionDao.
* Otherwise, the Episodes that are Part of the Feed are saved using the
* EpisodeDao, those that are not are deleted using the EpisodeDao.
*
* @param subscription The Subscription to validate
*/
@Async
@Transactional
public void validate(final Subscription subscription) {
if (subscription == null) {
return;
}
List<Map<String, Episode>> fetchedData
= fetchSubscriptionFeed(subscription);
if (fetchedData.get(URL_KEY_MAP_INDEX).isEmpty()) {
subscriptionDao.deleteById(subscription.getId());
return;
}
Subscription retrievedSubscription = subscriptionDao.save(subscription);
List<Episode> subscriptionEpisodes = retrievedSubscription.getEpisodes();
if (subscriptionEpisodes == null
|| subscriptionEpisodes.isEmpty()) {
return;
}
List<Episode> invalidEpisodes = new ArrayList<>();
List<Episode> validEpisodes = new ArrayList<>();
for (Episode episode : subscriptionEpisodes) {
Episode fetchedEpisode
= getFetchedEpisode(episode, fetchedData);
if (fetchedEpisode == null) {
invalidEpisodes.add(episode);
} else {
fetchedEpisode.setId(episode.getId());
validEpisodes.add(fetchedEpisode);
}
}
if (!invalidEpisodes.isEmpty()) {
episodeDao.deleteAll(invalidEpisodes);
}
if (!validEpisodes.isEmpty()) {
episodeDao.saveAll(validEpisodes);
}
}
private List<Map<String, Episode>> fetchSubscriptionFeed(
final Subscription subscription) {
final List<Map<String, Episode>> empty
= List.of(new HashMap<>(), new HashMap<>());
if (subscription.getUrl() == null) {
return empty;
}
// fetch feed
URL feedUrl;
try {
feedUrl = new URL(subscription.getUrl());
} catch (MalformedURLException e) {
return empty;
}
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed;
try {
feed = input.build(new XmlReader(feedUrl));
} catch (FeedException | IOException | IllegalArgumentException e) {
return empty;
}
String subscriptionTitle = feed.getTitle();
if (subscriptionTitle == null) {
return empty;
}
subscription.setTitle(subscriptionTitle);
Map<String, Episode> episodesByGuid = new HashMap<>();
Map<String, Episode> episodesByUrl = new HashMap<>();
List<SyndEntry> entries = feed.getEntries();
for (SyndEntry syndEntry : entries) {
// parse syndEntry to Episode
Episode episode = parseEpisode(syndEntry, subscription);
if (episode == null) {
return empty;
}
if (episode.getGuid() != null) {
episodesByGuid.put(episode.getGuid(), episode);
}
episodesByUrl.put(episode.getUrl(), episode);
}
return List.of(episodesByGuid, episodesByUrl);
}
private Episode parseEpisode(final SyndEntry syndEntry,
final Subscription subscription) {
if (syndEntry == null) {
return null;
}
final String title = syndEntry.getTitle();
final String guid = syndEntry.getUri();
List<SyndEnclosure> enclosureList = syndEntry.getEnclosures();
if (enclosureList.size() != 1) {
return null;
}
SyndEnclosure enclosure = enclosureList.get(0);
String url = enclosure.getUrl();
if (title == null || url == null) {
return null;
}
int total = 0;
List<Element> itunesTags = syndEntry.getForeignMarkup();
for (Element element : itunesTags) {
if (!element.getName().equals("duration")) {
continue;
}
List<Content> content = element.getContent();
if (content.size() != 1) {
return null;
}
String timeString = content.get(0).getValue();
total = parseTimeToSeconds(timeString);
}
return Episode.builder()
.guid(guid)
.url(url)
.title(title)
.total(total)
.subscription(subscription)
.build();
}
private Episode getFetchedEpisode(
final Episode episode,
final List<Map<String, Episode>> fetchedData) {
final String episodeUrl = episode.getUrl();
if (episodeUrl == null) {
return null;
}
final String episodeGuid = episode.getGuid();
if (fetchedData.get(GUID_KEY_MAP_INDEX).containsKey(episodeGuid)) {
return fetchedData.get(GUID_KEY_MAP_INDEX).get(episodeGuid);
}
return fetchedData.get(URL_KEY_MAP_INDEX).get(episodeUrl);
}
private static int parseTimeToSeconds(final String time) {
final String delim = ":";
if (time == null) {
// Returning default value
return 0;
}
if (!time.contains(delim)) {
try {
return Integer.parseInt(time);
} catch (NumberFormatException e) {
return 0;
}
}
StringBuilder formattedTime = new StringBuilder();
String[] datetimeStrings = time.split(delim);
if (datetimeStrings.length == 2) {
formattedTime.append("00" + delim);
}
for (int i = 0; i < datetimeStrings.length; i++) {
String part = datetimeStrings[i];
if (part.length() < 2) {
String toAdd = "0";
part = toAdd.repeat(2 - part.length()) + part;
}
formattedTime.append(part);
if (i + 1 < datetimeStrings.length) {
formattedTime.append(delim);
}
}
int toReturn = 0;
try {
toReturn = LocalTime.parse(formattedTime.toString()).toSecondOfDay();
} catch (DateTimeException e) {
// Do nothing, default value has already been set
}
return toReturn;
}
}
|