Skip to content

Commit a5e9eeb

Browse files
committed
[YouTube] Small improvements to subscription import
1 parent 9570882 commit a5e9eeb

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream content
4646
}
4747

4848
@Override
49-
public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream contentInputStream, String contentType)
49+
public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream contentInputStream,
50+
@Nonnull final String contentType)
5051
throws ExtractionException {
5152
switch (contentType) {
5253
case "json":
@@ -100,27 +101,24 @@ public List<SubscriptionItem> fromJsonInputStream(@Nonnull final InputStream con
100101

101102
public List<SubscriptionItem> fromZipInputStream(@Nonnull final InputStream contentInputStream)
102103
throws ExtractionException {
103-
final ZipInputStream zipInputStream = new ZipInputStream(contentInputStream);
104-
105-
try {
104+
try (final ZipInputStream zipInputStream = new ZipInputStream(contentInputStream)) {
106105
ZipEntry zipEntry;
107106
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
108107
if (zipEntry.getName().toLowerCase().endsWith(".csv")) {
109108
try {
110109
final List<SubscriptionItem> csvItems = fromCsvInputStream(zipInputStream);
111110

112-
// Return it only if it has items (it exits early if it's the wrong file format)
113-
// Otherwise try the next file
111+
// Return it only if it has items (it exits early if it's the wrong file
112+
// format), otherwise try the next file
114113
if (csvItems.size() > 0) {
115114
return csvItems;
116115
}
117-
} catch (ExtractionException e) {
116+
} catch (final ExtractionException e) {
118117
// Ignore error and go to next file
119-
// (maybe log it?)
120118
}
121119
}
122120
}
123-
} catch (IOException e) {
121+
} catch (final IOException e) {
124122
throw new InvalidSourceException("Error reading contents of zip file", e);
125123
}
126124

@@ -146,7 +144,7 @@ public List<SubscriptionItem> fromCsvInputStream(@Nonnull final InputStream cont
146144
try (BufferedReader br = new BufferedReader(new InputStreamReader(contentInputStream))) {
147145
final List<SubscriptionItem> subscriptionItems = new ArrayList<>();
148146

149-
// Ignore header
147+
// ignore header and skip first line
150148
currentLine = 1;
151149
line = br.readLine();
152150

@@ -160,13 +158,13 @@ public List<SubscriptionItem> fromCsvInputStream(@Nonnull final InputStream cont
160158
}
161159

162160
// First comma
163-
int i1 = line.indexOf(",");
161+
final int i1 = line.indexOf(",");
164162
if (i1 == -1) {
165163
continue;
166164
}
167165

168166
// Second comma
169-
int i2 = line.indexOf(",", i1 + 1);
167+
final int i2 = line.indexOf(",", i1 + 1);
170168
if (i2 == -1) {
171169
continue;
172170
}
@@ -188,18 +186,20 @@ public List<SubscriptionItem> fromCsvInputStream(@Nonnull final InputStream cont
188186
// Channel title from third entry
189187
final String channelTitle = line.substring(i2 + 1, i3);
190188

191-
final SubscriptionItem newItem = new SubscriptionItem(service.getServiceId(), channelUrl, channelTitle);
189+
final SubscriptionItem newItem
190+
= new SubscriptionItem(service.getServiceId(), channelUrl, channelTitle);
192191
subscriptionItems.add(newItem);
193192
}
194193

195194
return subscriptionItems;
196-
} catch (IOException e) {
195+
} catch (final IOException e) {
197196
if (line == null) {
198197
line = "<null>";
199198
} else if (line.length() > 10) {
200199
line = line.substring(0, 10) + "...";
201200
}
202-
throw new InvalidSourceException("Error reading CSV file, line = '" + line + "', line number = " + currentLine);
201+
throw new InvalidSourceException("Error reading CSV file on line = \"" + line
202+
+ "\", line number = " + currentLine, e);
203203
}
204204
}
205205
}

extractor/src/main/java/org/schabi/newpipe/extractor/subscription/SubscriptionExtractor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ public List<ContentSource> getSupportedSources() {
6363
*
6464
* @throws InvalidSourceException when the channelUrl doesn't exist or is invalid
6565
*/
66-
public List<SubscriptionItem> fromChannelUrl(String channelUrl) throws IOException, ExtractionException {
67-
throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName() + " doesn't support extracting from a channel url");
66+
public List<SubscriptionItem> fromChannelUrl(final String channelUrl)
67+
throws IOException, ExtractionException {
68+
throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName()
69+
+ " doesn't support extracting from a channel url");
6870
}
6971

7072
/**
@@ -83,7 +85,8 @@ public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream content
8385
*
8486
* @throws InvalidSourceException when the content read from the InputStream is invalid and can not be parsed
8587
*/
86-
public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream contentInputStream, String contentType)
88+
public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream contentInputStream,
89+
@Nonnull final String contentType)
8790
throws ExtractionException {
8891
throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName()
8992
+ " doesn't support extracting from an InputStream");

0 commit comments

Comments
 (0)