[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 @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 { throws ExtractionException {
switch (contentType) { switch (contentType) {
case "json": case "json":
@ -100,27 +101,24 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
public List<SubscriptionItem> fromZipInputStream(@Nonnull final InputStream contentInputStream) public List<SubscriptionItem> fromZipInputStream(@Nonnull final InputStream contentInputStream)
throws ExtractionException { throws ExtractionException {
final ZipInputStream zipInputStream = new ZipInputStream(contentInputStream); try (final ZipInputStream zipInputStream = new ZipInputStream(contentInputStream)) {
try {
ZipEntry zipEntry; ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) { while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (zipEntry.getName().toLowerCase().endsWith(".csv")) { if (zipEntry.getName().toLowerCase().endsWith(".csv")) {
try { try {
final List<SubscriptionItem> csvItems = fromCsvInputStream(zipInputStream); final List<SubscriptionItem> csvItems = fromCsvInputStream(zipInputStream);
// Return it only if it has items (it exits early if it's the wrong file format) // Return it only if it has items (it exits early if it's the wrong file
// Otherwise try the next file // format), otherwise try the next file
if (csvItems.size() > 0) { if (csvItems.size() > 0) {
return csvItems; return csvItems;
} }
} catch (ExtractionException e) { } catch (final ExtractionException e) {
// Ignore error and go to next file // 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); 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))) { try (BufferedReader br = new BufferedReader(new InputStreamReader(contentInputStream))) {
final List<SubscriptionItem> subscriptionItems = new ArrayList<>(); final List<SubscriptionItem> subscriptionItems = new ArrayList<>();
// Ignore header // ignore header and skip first line
currentLine = 1; currentLine = 1;
line = br.readLine(); line = br.readLine();
@ -160,13 +158,13 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
} }
// First comma // First comma
int i1 = line.indexOf(","); final int i1 = line.indexOf(",");
if (i1 == -1) { if (i1 == -1) {
continue; continue;
} }
// Second comma // Second comma
int i2 = line.indexOf(",", i1 + 1); final int i2 = line.indexOf(",", i1 + 1);
if (i2 == -1) { if (i2 == -1) {
continue; continue;
} }
@ -188,18 +186,20 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
// Channel title from third entry // Channel title from third entry
final String channelTitle = line.substring(i2 + 1, i3); 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); subscriptionItems.add(newItem);
} }
return subscriptionItems; return subscriptionItems;
} catch (IOException e) { } catch (final IOException e) {
if (line == null) { if (line == null) {
line = "<null>"; line = "<null>";
} else if (line.length() > 10) { } else if (line.length() > 10) {
line = line.substring(0, 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 * @throws InvalidSourceException when the channelUrl doesn't exist or is invalid
*/ */
public List<SubscriptionItem> fromChannelUrl(String channelUrl) throws IOException, ExtractionException { public List<SubscriptionItem> fromChannelUrl(final String channelUrl)
throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName() + " doesn't support extracting from a channel url"); 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 * @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 { throws ExtractionException {
throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName() throw new UnsupportedOperationException("Service " + service.getServiceInfo().getName()
+ " doesn't support extracting from an InputStream"); + " doesn't support extracting from an InputStream");