Skip to content

Commit 2ac713e

Browse files
authored
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.
2 parents acebbaf + 0eaca52 commit 2ac713e

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,10 @@ private List<SubscriptionItem> getItemsFromOPML(InputStream contentInputStream)
6363
String title = outline.attr("title");
6464
String xmlUrl = outline.attr("abs:xmlUrl");
6565

66-
if (title.isEmpty() || xmlUrl.isEmpty()) {
67-
throw new InvalidSourceException("document has invalid entries");
68-
}
69-
7066
try {
7167
String id = Parser.matchGroup1(ID_PATTERN, xmlUrl);
7268
result.add(new SubscriptionItem(service.getServiceId(), BASE_CHANNEL_URL + id, title));
73-
} catch (Parser.RegexException e) {
74-
throw new InvalidSourceException("document has invalid entries", e);
75-
}
69+
} catch (Parser.RegexException ignored) { /* ignore invalid subscriptions */ }
7670
}
7771

7872
return result;

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,38 @@ public void testEmptySourceException() throws Exception {
5959
assertTrue(items.isEmpty());
6060
}
6161

62+
@Test
63+
public void testSubscriptionWithEmptyTitleInSource() throws Exception {
64+
String channelId = "AA0AaAa0AaaaAAAAAA0aa0AA";
65+
String source = "<opml version=\"1.1\"><body><outline text=\"YouTube Subscriptions\" title=\"YouTube Subscriptions\">" +
66+
"<outline text=\"\" title=\"\" type=\"rss\" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_id=" + channelId + "\" />" +
67+
"</outline></body></opml>";
68+
69+
List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(new ByteArrayInputStream(source.getBytes("UTF-8")));
70+
assertTrue("List doesn't have exactly 1 item (had " + items.size() + ")", items.size() == 1);
71+
assertTrue("Item does not have an empty title (had \"" + items.get(0).getName() + "\")", items.get(0).getName().isEmpty());
72+
assertTrue("Item does not have the right channel id \"" + channelId + "\" (the whole url is \"" + items.get(0).getUrl() + "\")", items.get(0).getUrl().endsWith(channelId));
73+
}
74+
75+
@Test
76+
public void testSubscriptionWithInvalidUrlInSource() throws Exception {
77+
String source = "<opml version=\"1.1\"><body><outline text=\"YouTube Subscriptions\" title=\"YouTube Subscriptions\">" +
78+
"<outline text=\"invalid\" title=\"url\" type=\"rss\" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_not_id=|||||||\"/>" +
79+
"<outline text=\"fail\" title=\"fail\" type=\"rss\" xmlUgrl=\"invalidTag\"/>" +
80+
"<outline text=\"invalid\" title=\"url\" type=\"rss\" xmlUrl=\"\"/>" +
81+
"<outline text=\"\" title=\"\" type=\"rss\" xmlUrl=\"\"/>" +
82+
"</outline></body></opml>";
83+
84+
List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(new ByteArrayInputStream(source.getBytes("UTF-8")));
85+
assertTrue(items.isEmpty());
86+
}
87+
6288
@Test
6389
public void testInvalidSourceException() {
6490
List<String> invalidList = Arrays.asList(
6591
"<xml><notvalid></notvalid></xml>",
6692
"<opml><notvalid></notvalid></opml>",
6793
"<opml><body></body></opml>",
68-
"<opml><body><outline text=\"fail\" title=\"fail\" type=\"rss\" xmlUgrl=\"invalidTag\"/></outline></body></opml>",
69-
"<opml><body><outline><outline text=\"invalid\" title=\"url\" type=\"rss\"" +
70-
" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_not_id=|||||||\"/></outline></body></opml>",
7194
"",
7295
null,
7396
"\uD83D\uDC28\uD83D\uDC28\uD83D\uDC28",
@@ -78,11 +101,11 @@ public void testInvalidSourceException() {
78101
if (invalidContent != null) {
79102
byte[] bytes = invalidContent.getBytes("UTF-8");
80103
subscriptionExtractor.fromInputStream(new ByteArrayInputStream(bytes));
104+
fail("Extracting from \"" + invalidContent + "\" didn't throw an exception");
81105
} else {
82106
subscriptionExtractor.fromInputStream(null);
107+
fail("Extracting from null String didn't throw an exception");
83108
}
84-
85-
fail("didn't throw exception");
86109
} catch (Exception e) {
87110
// System.out.println(" -> " + e);
88111
boolean isExpectedException = e instanceof SubscriptionExtractor.InvalidSourceException;

0 commit comments

Comments
 (0)