2018-05-08 21:19:03 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
2017-08-12 21:10:21 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Created by Christian Schabesberger on 12.08.17.
|
|
|
|
*
|
2018-07-01 16:21:40 +02:00
|
|
|
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
|
2017-08-12 21:10:21 +02:00
|
|
|
* YoutubeTrendingExtractor.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2017-08-13 00:58:29 +02:00
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
import org.jsoup.nodes.Element;
|
2018-01-04 04:46:27 +01:00
|
|
|
import org.jsoup.select.Elements;
|
2019-10-29 06:00:29 +01:00
|
|
|
import org.schabi.newpipe.extractor.DownloadResponse;
|
2018-03-01 01:02:43 +01:00
|
|
|
import org.schabi.newpipe.extractor.Downloader;
|
2018-07-13 18:02:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
2018-03-01 01:02:43 +01:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2017-08-12 21:10:21 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
2017-08-13 00:58:29 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2017-08-12 21:10:21 +02:00
|
|
|
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
|
2019-10-29 06:00:29 +01:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
|
2018-03-01 01:02:43 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
2018-02-24 22:20:50 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
|
2018-09-15 21:47:53 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Localization;
|
2019-10-02 07:02:01 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.TimeAgoParser;
|
2017-08-13 00:58:29 +02:00
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2017-08-12 21:10:21 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2019-01-28 12:18:16 +01:00
|
|
|
public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
|
2017-08-12 21:10:21 +02:00
|
|
|
|
2019-10-02 07:02:01 +02:00
|
|
|
private final TimeAgoParser timeAgoParser = getService().getTimeAgoParser();
|
|
|
|
|
2017-08-13 00:58:29 +02:00
|
|
|
private Document doc;
|
|
|
|
|
2018-07-01 16:21:40 +02:00
|
|
|
public YoutubeTrendingExtractor(StreamingService service,
|
2018-09-15 21:47:53 +02:00
|
|
|
ListLinkHandler linkHandler,
|
|
|
|
String kioskId,
|
|
|
|
Localization localization) {
|
|
|
|
super(service, linkHandler, kioskId, localization);
|
2017-08-12 21:10:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-11-28 13:37:01 +01:00
|
|
|
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
|
2018-10-06 12:22:37 +02:00
|
|
|
final String contentCountry = getLocalization().getCountry();
|
2018-05-06 14:08:50 +02:00
|
|
|
String url = getUrl();
|
2017-08-14 12:48:51 +02:00
|
|
|
if(contentCountry != null && !contentCountry.isEmpty()) {
|
|
|
|
url += "?gl=" + contentCountry;
|
|
|
|
}
|
|
|
|
|
2019-10-29 06:00:29 +01:00
|
|
|
final DownloadResponse response = downloader.get(url);
|
|
|
|
doc = YoutubeParsingHelper.parseAndCheckPage(url, response);
|
2017-08-12 21:10:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-02-26 15:55:27 +01:00
|
|
|
public String getNextPageUrl() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) {
|
2017-08-12 21:10:21 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-25 01:10:04 +01:00
|
|
|
@Nonnull
|
2017-09-25 12:43:40 +02:00
|
|
|
@Override
|
|
|
|
public String getName() throws ParsingException {
|
|
|
|
try {
|
|
|
|
Element a = doc.select("a[href*=\"/feed/trending\"]").first();
|
|
|
|
Element span = a.select("span[class*=\"display-name\"]").first();
|
|
|
|
Element nameSpan = span.select("span").first();
|
|
|
|
return nameSpan.text();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get Trending name", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-25 02:03:30 +01:00
|
|
|
@Nonnull
|
2017-08-12 21:10:21 +02:00
|
|
|
@Override
|
2018-03-11 21:54:41 +01:00
|
|
|
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ParsingException {
|
2018-02-24 22:20:50 +01:00
|
|
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
2018-01-04 04:46:27 +01:00
|
|
|
Elements uls = doc.select("ul[class*=\"expanded-shelf-content-list\"]");
|
|
|
|
for(Element ul : uls) {
|
|
|
|
for(final Element li : ul.children()) {
|
|
|
|
final Element el = li.select("div[class*=\"yt-lockup-dismissable\"]").first();
|
2019-10-02 07:02:01 +02:00
|
|
|
collector.commit(new YoutubeStreamInfoItemExtractor(li, timeAgoParser) {
|
2018-01-04 04:46:27 +01:00
|
|
|
@Override
|
|
|
|
public String getUrl() throws ParsingException {
|
|
|
|
try {
|
|
|
|
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);
|
|
|
|
}
|
2017-08-13 00:58:29 +02:00
|
|
|
}
|
2018-01-04 04:46:27 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() throws ParsingException {
|
|
|
|
try {
|
|
|
|
Element dl = el.select("h3").first().select("a").first();
|
|
|
|
return dl.text();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get web page url for the video", e);
|
|
|
|
}
|
2017-08-13 00:58:29 +02:00
|
|
|
}
|
2018-01-04 04:46:27 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() throws ParsingException {
|
|
|
|
try {
|
2018-01-15 12:33:09 +01:00
|
|
|
String link = getUploaderLink().attr("abs:href");
|
2018-01-04 04:46:27 +01:00
|
|
|
if (link.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("is empty");
|
|
|
|
}
|
|
|
|
return link;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get Uploader name");
|
2017-11-25 04:00:19 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-04 04:46:27 +01:00
|
|
|
|
|
|
|
private Element getUploaderLink() {
|
2019-08-16 21:17:03 +02:00
|
|
|
// this url is not always in the form "/channel/..."
|
|
|
|
// sometimes Youtube provides urls in the from "/user/..."
|
2018-01-04 04:46:27 +01:00
|
|
|
Element uploaderEl = el.select("div[class*=\"yt-lockup-byline \"]").first();
|
|
|
|
return uploaderEl.select("a").first();
|
2017-08-13 00:58:29 +02:00
|
|
|
}
|
2018-01-04 04:46:27 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getUploaderName() throws ParsingException {
|
|
|
|
try {
|
|
|
|
return getUploaderLink().text();
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get Uploader name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
|
|
|
try {
|
|
|
|
String url;
|
|
|
|
Element te = li.select("span[class=\"yt-thumb-simple\"]").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);
|
2017-08-13 00:58:29 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-04 04:46:27 +01:00
|
|
|
});
|
|
|
|
}
|
2017-08-13 00:58:29 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrl());
|
2017-08-12 21:10:21 +02:00
|
|
|
}
|
|
|
|
}
|