[YouTube] Small improvements to subscription import

This commit is contained in:
Stypox 2021-08-30 15:45:45 +02:00
parent 9570882c73
commit a5e9eeb790
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 21 additions and 18 deletions

View File

@ -46,7 +46,8 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
}
@Override
public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream contentInputStream, String contentType)
public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream contentInputStream,
@Nonnull final String contentType)
throws ExtractionException {
switch (contentType) {
case "json":
@ -100,27 +101,24 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
public List<SubscriptionItem> fromZipInputStream(@Nonnull final InputStream contentInputStream)
throws ExtractionException {
final ZipInputStream zipInputStream = new ZipInputStream(contentInputStream);
try {
try (final ZipInputStream zipInputStream = new ZipInputStream(contentInputStream)) {
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (zipEntry.getName().toLowerCase().endsWith(".csv")) {
try {
final List<SubscriptionItem> csvItems = fromCsvInputStream(zipInputStream);
// Return it only if it has items (it exits early if it's the wrong file format)
// Otherwise try the next file
// Return it only if it has items (it exits early if it's the wrong file
// format), otherwise try the next file
if (csvItems.size() > 0) {
return csvItems;
}
} catch (ExtractionException e) {
} catch (final ExtractionException e) {
// Ignore error and go to next file
// (maybe log it?)
}
}
}
} catch (IOException e) {
} catch (final IOException e) {
throw new InvalidSourceException("Error reading contents of zip file", e);
}
@ -146,7 +144,7 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
try (BufferedReader br = new BufferedReader(new InputStreamReader(contentInputStream))) {
final List<SubscriptionItem> subscriptionItems = new ArrayList<>();
// Ignore header
// ignore header and skip first line
currentLine = 1;
line = br.readLine();
@ -160,13 +158,13 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
}
// First comma
int i1 = line.indexOf(",");
final int i1 = line.indexOf(",");
if (i1 == -1) {
continue;
}
// Second comma
int i2 = line.indexOf(",", i1 + 1);
final int i2 = line.indexOf(",", i1 + 1);
if (i2 == -1) {
continue;
}
@ -188,18 +186,20 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
// Channel title from third entry
final String channelTitle = line.substring(i2 + 1, i3);
final SubscriptionItem newItem = new SubscriptionItem(service.getServiceId(), channelUrl, channelTitle);
final SubscriptionItem newItem
= new SubscriptionItem(service.getServiceId(), channelUrl, channelTitle);
subscriptionItems.add(newItem);
}
return subscriptionItems;
} catch (IOException e) {
} catch (final IOException e) {
if (line == null) {
line = "<null>";
} else if (line.length() > 10) {
line = line.substring(0, 10) + "...";
}
throw new InvalidSourceException("Error reading CSV file, line = '" + line + "', line number = " + currentLine);
throw new InvalidSourceException("Error reading CSV file on line = \"" + line
+ "\", line number = " + currentLine, e);
}
}
}

View File

@ -63,8 +63,10 @@ public abstract class SubscriptionExtractor {
*
* @throws InvalidSourceException when the channelUrl doesn't exist or is invalid
*/
public List<SubscriptionItem> fromChannelUrl(String channelUrl) throws IOException, ExtractionException {
throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName() + " doesn't support extracting from a channel url");
public List<SubscriptionItem> fromChannelUrl(final String channelUrl)
throws IOException, ExtractionException {
throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName()
+ " doesn't support extracting from a channel url");
}
/**
@ -83,7 +85,8 @@ public abstract class SubscriptionExtractor {
*
* @throws InvalidSourceException when the content read from the InputStream is invalid and can not be parsed
*/
public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream contentInputStream, String contentType)
public List<SubscriptionItem> fromInputStream(@Nonnull final InputStream contentInputStream,
@Nonnull final String contentType)
throws ExtractionException {
throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName()
+ " doesn't support extracting from an InputStream");