Skip to content

Commit 0352659

Browse files
Isira-Seneviratnelitetex
authored andcommitted
Use BufferedReader#lines()
1 parent d5ade94 commit 0352659

1 file changed

Lines changed: 19 additions & 64 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSubscriptionExtractor.java

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
import java.io.IOException;
1515
import java.io.InputStream;
1616
import java.io.InputStreamReader;
17+
import java.io.UncheckedIOException;
1718
import java.util.ArrayList;
1819
import java.util.Collections;
1920
import java.util.List;
21+
import java.util.stream.Collectors;
2022
import java.util.zip.ZipEntry;
2123
import java.util.zip.ZipInputStream;
2224

@@ -110,7 +112,7 @@ public List<SubscriptionItem> fromZipInputStream(@Nonnull final InputStream cont
110112

111113
// Return it only if it has items (it exits early if it's the wrong file
112114
// format), otherwise try the next file
113-
if (csvItems.size() > 0) {
115+
if (!csvItems.isEmpty()) {
114116
return csvItems;
115117
}
116118
} catch (final ExtractionException e) {
@@ -138,69 +140,22 @@ public List<SubscriptionItem> fromCsvInputStream(@Nonnull final InputStream cont
138140
// The first line is always a header
139141
// Header names are different based on the locale
140142
// 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);
204159
}
205160
}
206161
}

0 commit comments

Comments
 (0)