NewPipeExtractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractor.java

285 lines
11 KiB
Java
Raw Normal View History

2017-03-01 18:47:52 +01:00
package org.schabi.newpipe.extractor.services.youtube;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
2017-03-01 18:47:52 +01:00
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
2017-08-06 22:20:15 +02:00
import org.schabi.newpipe.extractor.StreamingService;
2017-08-11 03:23:09 +02:00
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
2017-03-01 18:47:52 +01:00
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
2018-02-24 22:20:50 +01:00
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.Utils;
2017-04-21 23:31:40 +02:00
import javax.annotation.Nonnull;
2017-03-01 18:47:52 +01:00
import java.io.IOException;
/*
2017-03-01 18:47:52 +01:00
* Created by Christian Schabesberger on 25.07.16.
*
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
2017-08-11 03:23:09 +02:00
* YoutubeChannelExtractor.java is part of NewPipe.
2017-03-01 18:47:52 +01:00
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
@SuppressWarnings("WeakerAccess")
2017-08-11 03:23:09 +02:00
public class YoutubeChannelExtractor extends ChannelExtractor {
private static final String CHANNEL_FEED_BASE = "https://www.youtube.com/feeds/videos.xml?channel_id=";
2017-08-06 22:20:15 +02:00
private static final String CHANNEL_URL_PARAMETERS = "/videos?view=0&flow=list&sort=dd&live_view=10000";
private Document doc;
/**
2018-02-24 22:20:50 +01:00
* It's lazily initialized (when getInfoItemPage is called)
*/
private Document nextStreamsAjax;
/**
* Unfortunately, we have to fetch the page even if we are only getting next streams,
* as they don't deliver enough information on their own (the channel name, for example).
* <br/>
* This help us to keep track on what are we fetching.
*/
2017-09-26 22:46:21 +02:00
private boolean fetchingNextStreams;
2018-02-24 22:20:50 +01:00
public YoutubeChannelExtractor(StreamingService service, String url, String nextPageUrl) throws IOException, ExtractionException {
super(service, url, nextPageUrl);
2018-02-24 22:20:50 +01:00
fetchingNextStreams = nextPageUrl != null && !nextPageUrl.isEmpty();
2017-08-06 22:20:15 +02:00
}
@Override
2017-11-28 13:37:01 +01:00
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
String channelUrl = super.getCleanUrl() + CHANNEL_URL_PARAMETERS;
2017-08-11 03:23:09 +02:00
String pageContent = downloader.download(channelUrl);
doc = Jsoup.parse(pageContent, channelUrl);
2017-08-06 22:20:15 +02:00
2017-09-26 22:46:21 +02:00
if (!fetchingNextStreams) {
2018-02-24 22:20:50 +01:00
nextPageUrl = getNextPageUrlFrom(doc);
2017-09-26 22:46:21 +02:00
}
2017-08-06 22:20:15 +02:00
nextStreamsAjax = null;
}
2017-03-01 18:47:52 +01:00
@Nonnull
@Override
public String getCleanUrl() {
try {
return "https://www.youtube.com/channel/" + getId();
} catch (ParsingException e) {
return super.getCleanUrl();
}
}
@Nonnull
@Override
2017-08-11 03:23:09 +02:00
public String getId() throws ParsingException {
try {
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", e);
2017-03-01 18:47:52 +01:00
}
}
@Nonnull
2017-03-01 18:47:52 +01:00
@Override
2017-08-11 03:23:09 +02:00
public String getName() throws ParsingException {
2017-03-01 18:47:52 +01:00
try {
return doc.select("meta[property=\"og:title\"]").first().attr("content");
} catch (Exception e) {
throw new ParsingException("Could not get channel name", e);
2017-03-01 18:47:52 +01:00
}
}
@Override
public String getAvatarUrl() throws ParsingException {
try {
return doc.select("img[class=\"channel-header-profile-image\"]").first().attr("abs:src");
} catch (Exception e) {
2017-03-01 18:47:52 +01:00
throw new ParsingException("Could not get avatar", e);
}
}
@Override
public String getBannerUrl() throws ParsingException {
try {
2017-08-06 22:20:15 +02:00
Element el = doc.select("div[id=\"gh-banner\"]").first().select("style").first();
String cssContent = el.html();
String url = "https:" + Parser.matchGroup1("url\\(([^)]+)\\)", cssContent);
2017-08-06 22:20:15 +02:00
return url.contains("s.ytimg.com") || url.contains("default_banner") ? null : url;
} catch (Exception e) {
2017-03-01 18:47:52 +01:00
throw new ParsingException("Could not get Banner", e);
}
}
2017-08-11 03:23:09 +02:00
@Override
public String getFeedUrl() throws ParsingException {
try {
return CHANNEL_FEED_BASE + getId();
2017-08-11 03:23:09 +02:00
} catch (Exception e) {
throw new ParsingException("Could not get feed url", e);
}
}
@Override
public long getSubscriberCount() throws ParsingException {
2017-08-06 22:20:15 +02:00
Element el = doc.select("span[class*=\"yt-subscription-button-subscriber-count\"]").first();
if (el != null) {
return Long.parseLong(Utils.removeNonDigitCharacters(el.text()));
} else {
throw new ParsingException("Could not get subscriber count");
2017-03-01 18:47:52 +01:00
}
}
@Override
public String getDescription() throws ParsingException {
try {
return doc.select("meta[name=\"description\"]").first().attr("content");
} catch (Exception e) {
2017-08-11 03:23:09 +02:00
throw new ParsingException("Could not get channel description", e);
}
}
2017-11-25 02:03:30 +01:00
@Nonnull
@Override
2018-02-24 22:20:50 +01:00
public StreamInfoItemsCollector getStreams() throws ExtractionException {
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2017-08-06 22:20:15 +02:00
Element ul = doc.select("ul[id=\"browse-items-primary\"]").first();
collectStreamsFrom(collector, ul);
return collector;
}
@Override
2018-02-24 22:20:50 +01:00
public InfoItemPage getInfoItemPage() throws IOException, ExtractionException {
if (!hasNextPage()) {
throw new ExtractionException("Channel doesn't have more streams");
}
2018-01-13 20:26:11 +01:00
fetchPage();
2018-01-06 20:24:55 +01:00
2018-02-24 22:20:50 +01:00
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
2018-01-06 20:24:55 +01:00
2018-02-24 22:20:50 +01:00
setupNextPageAjax(NewPipe.getDownloader());
collectStreamsFrom(collector, nextStreamsAjax.select("body").first());
2018-02-24 22:20:50 +01:00
return new InfoItemPage(collector, nextPageUrl);
}
2018-02-24 22:20:50 +01:00
private void setupNextPageAjax(Downloader downloader) throws IOException, ReCaptchaException, ParsingException {
String ajaxDataRaw = downloader.download(nextPageUrl);
try {
JsonObject ajaxData = JsonParser.object().from(ajaxDataRaw);
String htmlDataRaw = ajaxData.getString("content_html");
2018-02-24 22:20:50 +01:00
nextStreamsAjax = Jsoup.parse(htmlDataRaw, nextPageUrl);
String nextStreamsHtmlDataRaw = ajaxData.getString("load_more_widget_html");
if (!nextStreamsHtmlDataRaw.isEmpty()) {
2018-02-24 22:20:50 +01:00
nextPageUrl = getNextPageUrlFrom(Jsoup.parse(nextStreamsHtmlDataRaw, nextPageUrl));
} else {
2018-02-24 22:20:50 +01:00
nextPageUrl = "";
}
} catch (JsonParserException e) {
throw new ParsingException("Could not parse json data for next streams", e);
}
}
2018-02-24 22:20:50 +01:00
private String getNextPageUrlFrom(Document d) throws ParsingException {
try {
Element button = d.select("button[class*=\"yt-uix-load-more\"]").first();
if (button != null) {
return button.attr("abs:data-uix-load-more-href");
} else {
// Sometimes channels are simply so small, they don't have a more streams/videos
return "";
}
} catch (Exception e) {
throw new ParsingException("could not get next streams' url", e);
}
}
2018-02-24 22:20:50 +01:00
private void collectStreamsFrom(StreamInfoItemsCollector collector, Element element) throws ParsingException {
collector.reset();
final String uploaderName = getName();
2017-11-25 02:38:46 +01:00
final String uploaderUrl = getCleanUrl();
for (final Element li : element.children()) {
2017-03-01 18:47:52 +01:00
if (li.select("div[class=\"feed-item-dismissable\"]").first() != null) {
collector.commit(new YoutubeStreamInfoItemExtractor(li) {
2017-03-01 18:47:52 +01:00
@Override
2017-08-11 20:21:49 +02:00
public String getUrl() throws ParsingException {
2017-03-01 18:47:52 +01:00
try {
Element el = li.select("div[class=\"feed-item-dismissable\"]").first();
Element dl = el.select("h3").first().select("a").first();
return dl.attr("abs:href");
} catch (Exception e) {
throw new ParsingException("Could not get web page url for the video", e);
}
}
@Override
2017-08-11 20:21:49 +02:00
public String getName() throws ParsingException {
2017-03-01 18:47:52 +01:00
try {
Element el = li.select("div[class=\"feed-item-dismissable\"]").first();
Element dl = el.select("h3").first().select("a").first();
return dl.text();
} catch (Exception e) {
throw new ParsingException("Could not get title", e);
}
}
@Override
public String getUploaderName() throws ParsingException {
return uploaderName;
2017-03-01 18:47:52 +01:00
}
2017-11-25 02:38:46 +01:00
@Override
public String getUploaderUrl() throws ParsingException {
return uploaderUrl;
}
2017-03-01 18:47:52 +01:00
@Override
public String getThumbnailUrl() throws ParsingException {
try {
String url;
Element te = li.select("span[class=\"yt-thumb-clip\"]").first()
.select("img").first();
url = te.attr("abs:src");
// Sometimes youtube sends links to gif files which somehow seem to not exist
// anymore. Items with such gif also offer a secondary image source. So we are going
// to use that if we've caught such an item.
if (url.contains(".gif")) {
url = te.attr("abs:data-thumb");
}
return url;
} catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e);
}
}
});
}
}
}
}