|
14 | 14 | import java.io.IOException; |
15 | 15 | import java.io.InputStream; |
16 | 16 | import java.io.InputStreamReader; |
| 17 | +import java.io.UncheckedIOException; |
17 | 18 | import java.util.ArrayList; |
18 | 19 | import java.util.Collections; |
19 | 20 | import java.util.List; |
| 21 | +import java.util.stream.Collectors; |
20 | 22 | import java.util.zip.ZipEntry; |
21 | 23 | import java.util.zip.ZipInputStream; |
22 | 24 |
|
@@ -110,7 +112,7 @@ public List<SubscriptionItem> fromZipInputStream(@Nonnull final InputStream cont |
110 | 112 |
|
111 | 113 | // Return it only if it has items (it exits early if it's the wrong file |
112 | 114 | // format), otherwise try the next file |
113 | | - if (csvItems.size() > 0) { |
| 115 | + if (!csvItems.isEmpty()) { |
114 | 116 | return csvItems; |
115 | 117 | } |
116 | 118 | } catch (final ExtractionException e) { |
@@ -138,69 +140,22 @@ public List<SubscriptionItem> fromCsvInputStream(@Nonnull final InputStream cont |
138 | 140 | // The first line is always a header |
139 | 141 | // Header names are different based on the locale |
140 | 142 | // Fortunately the data is always the same order no matter what locale |
141 | | - |
142 | | - int currentLine = 0; |
143 | | - String line = ""; |
144 | | - |
145 | | - try (BufferedReader br = new BufferedReader(new InputStreamReader(contentInputStream))) { |
146 | | - final List<SubscriptionItem> subscriptionItems = new ArrayList<>(); |
147 | | - |
148 | | - // ignore header and skip first line |
149 | | - currentLine = 1; |
150 | | - line = br.readLine(); |
151 | | - |
152 | | - while ((line = br.readLine()) != null) { |
153 | | - currentLine++; |
154 | | - |
155 | | - // Exit early if we've read the first few lines and we haven't added any items |
156 | | - // It's likely we're in the wrong file |
157 | | - if (currentLine > 5 && subscriptionItems.size() == 0) { |
158 | | - break; |
159 | | - } |
160 | | - |
161 | | - // First comma |
162 | | - final int i1 = line.indexOf(","); |
163 | | - if (i1 == -1) { |
164 | | - continue; |
165 | | - } |
166 | | - |
167 | | - // Second comma |
168 | | - final int i2 = line.indexOf(",", i1 + 1); |
169 | | - if (i2 == -1) { |
170 | | - continue; |
171 | | - } |
172 | | - |
173 | | - // Third comma or line length |
174 | | - int i3 = line.indexOf(",", i2 + 1); |
175 | | - if (i3 == -1) { |
176 | | - i3 = line.length(); |
177 | | - } |
178 | | - |
179 | | - // Channel URL from second entry |
180 | | - final String channelUrl = line |
181 | | - .substring(i1 + 1, i2) |
182 | | - .replace("http://", "https://"); |
183 | | - if (!channelUrl.startsWith(BASE_CHANNEL_URL)) { |
184 | | - continue; |
185 | | - } |
186 | | - |
187 | | - // Channel title from third entry |
188 | | - final String channelTitle = line.substring(i2 + 1, i3); |
189 | | - |
190 | | - final SubscriptionItem newItem |
191 | | - = new SubscriptionItem(service.getServiceId(), channelUrl, channelTitle); |
192 | | - subscriptionItems.add(newItem); |
193 | | - } |
194 | | - |
195 | | - return subscriptionItems; |
196 | | - } catch (final IOException e) { |
197 | | - if (line == null) { |
198 | | - line = "<null>"; |
199 | | - } else if (line.length() > 10) { |
200 | | - line = line.substring(0, 10) + "..."; |
201 | | - } |
202 | | - throw new InvalidSourceException("Error reading CSV file on line = \"" + line |
203 | | - + "\", line number = " + currentLine, e); |
| 143 | + try (var reader = new BufferedReader(new InputStreamReader(contentInputStream))) { |
| 144 | + return reader.lines() |
| 145 | + .skip(1) // ignore header and skip first line |
| 146 | + .map(line -> line.split(",")) |
| 147 | + .filter(values -> values.length >= 3) |
| 148 | + .map(values -> { |
| 149 | + // Channel URL from second entry |
| 150 | + final String channelUrl = values[1].replace("http://", "https://"); |
| 151 | + // Channel title from third entry |
| 152 | + final String title = values[2]; |
| 153 | + |
| 154 | + return new SubscriptionItem(service.getServiceId(), channelUrl, title); |
| 155 | + }) |
| 156 | + .collect(Collectors.toUnmodifiableList()); |
| 157 | + } catch (final UncheckedIOException | IOException e) { |
| 158 | + throw new InvalidSourceException("Error reading CSV file", e); |
204 | 159 | } |
205 | 160 | } |
206 | 161 | } |
0 commit comments