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

113 lines
4.5 KiB
Java
Raw Normal View History

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;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
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;
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;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
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;
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;
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;
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,
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 {
final String url = getUrl() +
"?gl=" + getExtractorContentCountry().getCountryCode();
2017-08-14 12:48:51 +02:00
final Response response = downloader.get(url, getExtractorLocalization());
doc = YoutubeParsingHelper.parseAndCheckPage(url, response);
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
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) {
2017-08-12 21:10:21 +02:00
return null;
}
@Nonnull
@Override
public String getName() throws ParsingException {
String name;
try {
name = initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title")
.getArray("runs").getObject(0).getString("text");
} catch (Exception e) {
throw new ParsingException("Could not get Trending name", e);
}
if (name != null && !name.isEmpty()) {
return name;
}
throw new ParsingException("Could not get Trending name");
}
2017-11-25 02:03:30 +01:00
@Nonnull
2017-08-12 21:10:21 +02:00
@Override
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");
final TimeAgoParser timeAgoParser = getTimeAgoParser();
2020-02-17 20:24:48 +01:00
for (Object ul : firstPageElements) {
final JsonObject videoInfo = ((JsonObject) ul).getObject("videoRenderer");
collector.commit(new YoutubeStreamInfoItemExtractor(videoInfo, timeAgoParser));
2017-08-13 00:58:29 +02:00
}
return new InfoItemsPage<>(collector, getNextPageUrl());
2020-02-17 20:24:48 +01:00
2017-08-12 21:10:21 +02:00
}
}