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/>.
|
|
|
|
*/
|
|
|
|
|
2020-02-17 20:24:48 +01:00
|
|
|
import com.grack.nanojson.JsonArray;
|
2020-02-17 17:30:22 +01:00
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2020-02-22 20:33:48 +01:00
|
|
|
|
2017-08-13 00:58:29 +02:00
|
|
|
import org.jsoup.nodes.Document;
|
2018-03-01 01:02:43 +01:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
|
|
|
import org.schabi.newpipe.extractor.downloader.Response;
|
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-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
|
|
|
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
|
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;
|
2020-02-17 17:30:22 +01:00
|
|
|
import org.schabi.newpipe.extractor.utils.Parser;
|
2017-08-13 00:58:29 +02:00
|
|
|
|
2017-08-12 21:10:21 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2020-02-22 20:33:48 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2019-01-28 12:18:16 +01:00
|
|
|
public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
|
2017-08-12 21:10:21 +02:00
|
|
|
|
2017-08-13 00:58:29 +02:00
|
|
|
private Document doc;
|
2020-02-17 17:30:22 +01:00
|
|
|
private JsonObject initialData;
|
2017-08-13 00:58:29 +02:00
|
|
|
|
2018-07-01 16:21:40 +02:00
|
|
|
public YoutubeTrendingExtractor(StreamingService service,
|
2018-09-15 21:47:53 +02:00
|
|
|
ListLinkHandler linkHandler,
|
2019-04-28 22:03:16 +02:00
|
|
|
String kioskId) {
|
|
|
|
super(service, linkHandler, kioskId);
|
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 {
|
2019-04-28 22:03:16 +02:00
|
|
|
final String url = getUrl() +
|
|
|
|
"?gl=" + getExtractorContentCountry().getCountryCode();
|
2017-08-14 12:48:51 +02:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
final Response response = downloader.get(url, getExtractorLocalization());
|
2019-10-29 06:00:29 +01:00
|
|
|
doc = YoutubeParsingHelper.parseAndCheckPage(url, response);
|
2020-02-22 23:51:02 +01:00
|
|
|
initialData = YoutubeParsingHelper.getInitialData(response.responseBody());
|
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 {
|
2020-02-17 17:30:22 +01:00
|
|
|
String name;
|
2017-09-25 12:43:40 +02:00
|
|
|
try {
|
2020-02-17 17:30:22 +01:00
|
|
|
name = initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title")
|
|
|
|
.getArray("runs").getObject(0).getString("text");
|
2017-09-25 12:43:40 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ParsingException("Could not get Trending name", e);
|
|
|
|
}
|
2020-02-17 17:30:22 +01:00
|
|
|
if (name != null && !name.isEmpty()) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
throw new ParsingException("Could not get Trending name");
|
2017-09-25 12:43:40 +02:00
|
|
|
}
|
|
|
|
|
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());
|
2020-02-17 20:24:48 +01:00
|
|
|
JsonArray firstPageElements = initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
|
|
|
|
.getArray("tabs").getObject(0).getObject("tabRenderer").getObject("content")
|
|
|
|
.getObject("sectionListRenderer").getArray("contents").getObject(0).getObject("itemSectionRenderer")
|
|
|
|
.getArray("contents").getObject(0).getObject("shelfRenderer").getObject("content")
|
|
|
|
.getObject("expandedShelfContentsRenderer").getArray("items");
|
2019-04-28 22:03:16 +02:00
|
|
|
|
|
|
|
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
|
|
|
|
2020-02-17 20:24:48 +01:00
|
|
|
for (Object ul : firstPageElements) {
|
|
|
|
final JsonObject videoInfo = ((JsonObject) ul).getObject("videoRenderer");
|
2020-02-22 20:33:48 +01:00
|
|
|
collector.commit(new YoutubeStreamInfoItemExtractor(videoInfo, timeAgoParser));
|
2017-08-13 00:58:29 +02:00
|
|
|
}
|
2018-03-11 21:54:41 +01:00
|
|
|
return new InfoItemsPage<>(collector, getNextPageUrl());
|
2020-02-17 20:24:48 +01:00
|
|
|
|
2017-08-12 21:10:21 +02:00
|
|
|
}
|
|
|
|
}
|