Merge pull request #160 from Stypox/invalid-youtube-subscription-fix
Fixed youtube subscription import: ignore ones with invalid url and keep ones with empty title.
This commit is contained in:
commit
2ac713e70f
|
@ -63,16 +63,10 @@ public class YoutubeSubscriptionExtractor extends SubscriptionExtractor {
|
||||||
String title = outline.attr("title");
|
String title = outline.attr("title");
|
||||||
String xmlUrl = outline.attr("abs:xmlUrl");
|
String xmlUrl = outline.attr("abs:xmlUrl");
|
||||||
|
|
||||||
if (title.isEmpty() || xmlUrl.isEmpty()) {
|
|
||||||
throw new InvalidSourceException("document has invalid entries");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String id = Parser.matchGroup1(ID_PATTERN, xmlUrl);
|
String id = Parser.matchGroup1(ID_PATTERN, xmlUrl);
|
||||||
result.add(new SubscriptionItem(service.getServiceId(), BASE_CHANNEL_URL + id, title));
|
result.add(new SubscriptionItem(service.getServiceId(), BASE_CHANNEL_URL + id, title));
|
||||||
} catch (Parser.RegexException e) {
|
} catch (Parser.RegexException ignored) { /* ignore invalid subscriptions */ }
|
||||||
throw new InvalidSourceException("document has invalid entries", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -59,15 +59,38 @@ public class YoutubeSubscriptionExtractorTest {
|
||||||
assertTrue(items.isEmpty());
|
assertTrue(items.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSubscriptionWithEmptyTitleInSource() throws Exception {
|
||||||
|
String channelId = "AA0AaAa0AaaaAAAAAA0aa0AA";
|
||||||
|
String source = "<opml version=\"1.1\"><body><outline text=\"YouTube Subscriptions\" title=\"YouTube Subscriptions\">" +
|
||||||
|
"<outline text=\"\" title=\"\" type=\"rss\" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_id=" + channelId + "\" />" +
|
||||||
|
"</outline></body></opml>";
|
||||||
|
|
||||||
|
List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(new ByteArrayInputStream(source.getBytes("UTF-8")));
|
||||||
|
assertTrue("List doesn't have exactly 1 item (had " + items.size() + ")", items.size() == 1);
|
||||||
|
assertTrue("Item does not have an empty title (had \"" + items.get(0).getName() + "\")", items.get(0).getName().isEmpty());
|
||||||
|
assertTrue("Item does not have the right channel id \"" + channelId + "\" (the whole url is \"" + items.get(0).getUrl() + "\")", items.get(0).getUrl().endsWith(channelId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSubscriptionWithInvalidUrlInSource() throws Exception {
|
||||||
|
String source = "<opml version=\"1.1\"><body><outline text=\"YouTube Subscriptions\" title=\"YouTube Subscriptions\">" +
|
||||||
|
"<outline text=\"invalid\" title=\"url\" type=\"rss\" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_not_id=|||||||\"/>" +
|
||||||
|
"<outline text=\"fail\" title=\"fail\" type=\"rss\" xmlUgrl=\"invalidTag\"/>" +
|
||||||
|
"<outline text=\"invalid\" title=\"url\" type=\"rss\" xmlUrl=\"\"/>" +
|
||||||
|
"<outline text=\"\" title=\"\" type=\"rss\" xmlUrl=\"\"/>" +
|
||||||
|
"</outline></body></opml>";
|
||||||
|
|
||||||
|
List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(new ByteArrayInputStream(source.getBytes("UTF-8")));
|
||||||
|
assertTrue(items.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidSourceException() {
|
public void testInvalidSourceException() {
|
||||||
List<String> invalidList = Arrays.asList(
|
List<String> invalidList = Arrays.asList(
|
||||||
"<xml><notvalid></notvalid></xml>",
|
"<xml><notvalid></notvalid></xml>",
|
||||||
"<opml><notvalid></notvalid></opml>",
|
"<opml><notvalid></notvalid></opml>",
|
||||||
"<opml><body></body></opml>",
|
"<opml><body></body></opml>",
|
||||||
"<opml><body><outline text=\"fail\" title=\"fail\" type=\"rss\" xmlUgrl=\"invalidTag\"/></outline></body></opml>",
|
|
||||||
"<opml><body><outline><outline text=\"invalid\" title=\"url\" type=\"rss\"" +
|
|
||||||
" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_not_id=|||||||\"/></outline></body></opml>",
|
|
||||||
"",
|
"",
|
||||||
null,
|
null,
|
||||||
"\uD83D\uDC28\uD83D\uDC28\uD83D\uDC28",
|
"\uD83D\uDC28\uD83D\uDC28\uD83D\uDC28",
|
||||||
|
@ -78,11 +101,11 @@ public class YoutubeSubscriptionExtractorTest {
|
||||||
if (invalidContent != null) {
|
if (invalidContent != null) {
|
||||||
byte[] bytes = invalidContent.getBytes("UTF-8");
|
byte[] bytes = invalidContent.getBytes("UTF-8");
|
||||||
subscriptionExtractor.fromInputStream(new ByteArrayInputStream(bytes));
|
subscriptionExtractor.fromInputStream(new ByteArrayInputStream(bytes));
|
||||||
|
fail("Extracting from \"" + invalidContent + "\" didn't throw an exception");
|
||||||
} else {
|
} else {
|
||||||
subscriptionExtractor.fromInputStream(null);
|
subscriptionExtractor.fromInputStream(null);
|
||||||
|
fail("Extracting from null String didn't throw an exception");
|
||||||
}
|
}
|
||||||
|
|
||||||
fail("didn't throw exception");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// System.out.println(" -> " + e);
|
// System.out.println(" -> " + e);
|
||||||
boolean isExpectedException = e instanceof SubscriptionExtractor.InvalidSourceException;
|
boolean isExpectedException = e instanceof SubscriptionExtractor.InvalidSourceException;
|
||||||
|
|
Loading…
Reference in New Issue