Fix YouTubeChannelExtractor

It was given some inconsistent results (/user and /channel), now it only returns /channel urls don't matter what the original is (at least when calling the getCleanUrl() method).
This commit is contained in:
Mauricio Colli 2017-08-15 23:56:10 -03:00
parent a8a4eaf81b
commit 34547a37b6
2 changed files with 28 additions and 7 deletions

View file

@ -59,7 +59,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
public void fetchPage() throws IOException, ExtractionException {
Downloader downloader = NewPipe.getDownloader();
String channelUrl = getCleanUrl() + CHANNEL_URL_PARAMETERS;
String channelUrl = super.getCleanUrl() + CHANNEL_URL_PARAMETERS;
String pageContent = downloader.download(channelUrl);
doc = Jsoup.parse(pageContent, channelUrl);
@ -67,12 +67,24 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
nextStreamsAjax = null;
}
@Override
public String getCleanUrl() {
try {
return "https://www.youtube.com/channel/" + getId();
} catch (ParsingException e) {
return super.getCleanUrl();
}
}
@Override
public String getId() throws ParsingException {
try {
return getUrlIdHandler().getId(getCleanUrl());
Element element = doc.getElementsByClass("yt-uix-subscription-button").first();
if (element == null) element = doc.getElementsByClass("yt-uix-subscription-preferences-button").first();
return element.attr("data-channel-external-id");
} catch (Exception e) {
throw new ParsingException("Could not get channel id");
throw new ParsingException("Could not get channel id", e);
}
}
@ -110,8 +122,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
@Override
public String getFeedUrl() throws ParsingException {
try {
String channelId = doc.getElementsByClass("yt-uix-subscription-button").first().attr("data-channel-external-id");
return channelId == null ? "" : CHANNEL_FEED_BASE + channelId;
return CHANNEL_FEED_BASE + getId();
} catch (Exception e) {
throw new ParsingException("Could not get feed url", e);
}

View file

@ -41,7 +41,7 @@ public class YoutubeChannelExtractorTest {
public void setUp() throws Exception {
NewPipe.init(Downloader.getInstance());
extractor = YouTube.getService()
.getChannelExtractor("https://www.youtube.com/channel/UCYJ61XIK64sp6ZFFS8sctxw");
.getChannelExtractor("https://www.youtube.com/user/Gronkh");
}
@Test
@ -54,6 +54,16 @@ public class YoutubeChannelExtractorTest {
assertEquals(extractor.getName(), "Gronkh");
}
@Test
public void testGetId() throws Exception {
assertEquals(extractor.getId(), "UCYJ61XIK64sp6ZFFS8sctxw");
}
@Test
public void testGetUrl() throws Exception {
assertEquals(extractor.getCleanUrl(), "https://www.youtube.com/channel/UCYJ61XIK64sp6ZFFS8sctxw");
}
@Test
public void testGetDescription() throws Exception {
assertEquals(extractor.getDescription(), "★ ★ ★ KLICK MICH HART, DU SAU! :D ★ ★ ★ Zart im Schmelz und süffig im Abgang. Ungebremster Spieltrieb seit 1896. Tägliche Folgen nonstop seit dem 01.04.2010!...");
@ -71,7 +81,7 @@ public class YoutubeChannelExtractorTest {
@Test
public void testGetFeedUrl() throws Exception {
assertTrue(extractor.getFeedUrl(), extractor.getFeedUrl().contains("feed"));
assertEquals(extractor.getFeedUrl(), "https://www.youtube.com/feeds/videos.xml?channel_id=UCYJ61XIK64sp6ZFFS8sctxw");
}
@Test