Skip to content

Commit 3a33cef

Browse files
authored
Merge pull request #1246 from Isira-Seneviratne/BufferedReader-lines
Use BufferedReader#lines()
2 parents d5ade94 + 222d869 commit 3a33cef

2 files changed

Lines changed: 38 additions & 79 deletions

File tree

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

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
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.Objects;
22+
import java.util.stream.Collectors;
2023
import java.util.zip.ZipEntry;
2124
import java.util.zip.ZipInputStream;
2225

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

111114
// Return it only if it has items (it exits early if it's the wrong file
112115
// format), otherwise try the next file
113-
if (csvItems.size() > 0) {
116+
if (!csvItems.isEmpty()) {
114117
return csvItems;
115118
}
116119
} catch (final ExtractionException e) {
@@ -138,69 +141,25 @@ public List<SubscriptionItem> fromCsvInputStream(@Nonnull final InputStream cont
138141
// The first line is always a header
139142
// Header names are different based on the locale
140143
// 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);
144+
try (var reader = new BufferedReader(new InputStreamReader(contentInputStream))) {
145+
return reader.lines()
146+
.skip(1) // ignore header and skip first line
147+
.map(line -> line.split(","))
148+
.filter(values -> values.length >= 3)
149+
.map(values -> {
150+
// Channel URL from second entry
151+
final String channelUrl = values[1].replace("http://", "https://");
152+
return channelUrl.startsWith(BASE_CHANNEL_URL)
153+
? new SubscriptionItem(
154+
service.getServiceId(),
155+
channelUrl,
156+
values[2]) // Channel title from third entry
157+
: null;
158+
})
159+
.filter(Objects::nonNull)
160+
.collect(Collectors.toUnmodifiableList());
161+
} catch (final UncheckedIOException | IOException e) {
162+
throw new InvalidSourceException("Error reading CSV file", e);
204163
}
205164
}
206165
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSubscriptionExtractorTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
/**
2727
* Test for {@link YoutubeSubscriptionExtractor}
2828
*/
29-
public class YoutubeSubscriptionExtractorTest {
30-
29+
class YoutubeSubscriptionExtractorTest {
3130

3231
private static YoutubeSubscriptionExtractor subscriptionExtractor;
3332
private static LinkHandlerFactory urlHandler;
@@ -41,7 +40,7 @@ public static void setupClass() {
4140
}
4241

4342
@Test
44-
public void testFromInputStream() throws Exception {
43+
void testFromInputStream() throws Exception {
4544
final List<SubscriptionItem> subscriptionItems = subscriptionExtractor.fromInputStream(
4645
new FileInputStream(resolveTestResource("youtube_takeout_import_test.json")));
4746
assertEquals(7, subscriptionItems.size());
@@ -55,14 +54,14 @@ public void testFromInputStream() throws Exception {
5554
}
5655

5756
@Test
58-
public void testEmptySourceException() throws Exception {
57+
void testEmptySourceException() throws Exception {
5958
final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(
6059
new ByteArrayInputStream("[]".getBytes(StandardCharsets.UTF_8)));
6160
assertTrue(items.isEmpty());
6261
}
6362

6463
@Test
65-
public void testSubscriptionWithEmptyTitleInSource() throws Exception {
64+
void testSubscriptionWithEmptyTitleInSource() throws Exception {
6665
final String source = "[{\"snippet\":{\"resourceId\":{\"channelId\":\"UCEOXxzW2vU0P-0THehuIIeg\"}}}]";
6766
final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(
6867
new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)));
@@ -74,7 +73,7 @@ public void testSubscriptionWithEmptyTitleInSource() throws Exception {
7473
}
7574

7675
@Test
77-
public void testSubscriptionWithInvalidUrlInSource() throws Exception {
76+
void testSubscriptionWithInvalidUrlInSource() throws Exception {
7877
final String source = "[{\"snippet\":{\"resourceId\":{\"channelId\":\"gibberish\"},\"title\":\"name1\"}}," +
7978
"{\"snippet\":{\"resourceId\":{\"channelId\":\"UCEOXxzW2vU0P-0THehuIIeg\"},\"title\":\"name2\"}}]";
8079
final List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(
@@ -87,8 +86,8 @@ public void testSubscriptionWithInvalidUrlInSource() throws Exception {
8786
}
8887

8988
@Test
90-
public void testInvalidSourceException() {
91-
List<String> invalidList = Arrays.asList(
89+
void testInvalidSourceException() {
90+
final List<String> invalidList = Arrays.asList(
9291
"<xml><notvalid></notvalid></xml>",
9392
"<opml><notvalid></notvalid></opml>",
9493
"{\"a\":\"b\"}",
@@ -100,13 +99,14 @@ public void testInvalidSourceException() {
10099
"\uD83D\uDC28\uD83D\uDC28\uD83D\uDC28",
101100
"gibberish");
102101

103-
for (String invalidContent : invalidList) {
102+
for (final String invalidContent : invalidList) {
104103
try {
105-
byte[] bytes = invalidContent.getBytes(StandardCharsets.UTF_8);
104+
final byte[] bytes = invalidContent.getBytes(StandardCharsets.UTF_8);
106105
subscriptionExtractor.fromInputStream(new ByteArrayInputStream(bytes));
107106
fail("Extracting from \"" + invalidContent + "\" didn't throw an exception");
108107
} catch (final Exception e) {
109-
boolean correctType = e instanceof SubscriptionExtractor.InvalidSourceException;
108+
final boolean correctType =
109+
e instanceof SubscriptionExtractor.InvalidSourceException;
110110
if (!correctType) {
111111
e.printStackTrace();
112112
}
@@ -117,7 +117,7 @@ public void testInvalidSourceException() {
117117

118118
private static void assertSubscriptionItems(final List<SubscriptionItem> subscriptionItems)
119119
throws Exception {
120-
assertTrue(subscriptionItems.size() > 0);
120+
assertTrue(!subscriptionItems.isEmpty());
121121

122122
for (final SubscriptionItem item : subscriptionItems) {
123123
assertNotNull(item.getName());
@@ -128,7 +128,7 @@ private static void assertSubscriptionItems(final List<SubscriptionItem> subscri
128128
}
129129

130130
@Test
131-
public void fromZipInputStream() throws Exception {
131+
void fromZipInputStream() throws Exception {
132132
final List<String> zipPaths = Arrays.asList(
133133
"youtube_takeout_import_test_1.zip",
134134
"youtube_takeout_import_test_2.zip"
@@ -144,13 +144,13 @@ public void fromZipInputStream() throws Exception {
144144
}
145145

146146
@Test
147-
public void fromCsvInputStream() throws Exception {
147+
void fromCsvInputStream() throws Exception {
148148
final List<String> csvPaths = Arrays.asList(
149149
"youtube_takeout_import_test_1.csv",
150150
"youtube_takeout_import_test_2.csv"
151151
);
152152

153-
for (String path : csvPaths)
153+
for (final String path : csvPaths)
154154
{
155155
final File file = resolveTestResource(path);
156156
final FileInputStream fileInputStream = new FileInputStream(file);

0 commit comments

Comments
 (0)