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

101 lines
4.3 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;
2020-04-15 14:09:46 +02:00
import org.schabi.newpipe.extractor.Page;
2018-03-01 01:02:43 +01:00
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
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;
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;
2017-08-13 00:58:29 +02:00
2017-08-12 21:10:21 +02:00
import java.io.IOException;
import javax.annotation.Nonnull;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
2020-04-15 18:49:58 +02:00
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2020-02-27 17:39:23 +01:00
2019-01-28 12:18:16 +01:00
public class YoutubeTrendingExtractor extends KioskExtractor<StreamInfoItem> {
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 {
2020-02-26 16:09:32 +01:00
final String url = getUrl() + "?pbj=1&gl="
+ getExtractorContentCountry().getCountryCode();
2017-08-14 12:48:51 +02:00
2020-02-29 16:55:07 +01:00
final JsonArray ajaxJson = getJsonResponse(url, getExtractorLocalization());
2020-02-26 16:09:32 +01:00
initialData = ajaxJson.getObject(1).getObject("response");
2017-08-12 21:10:21 +02:00
}
2018-02-26 15:55:27 +01:00
@Override
public InfoItemsPage<StreamInfoItem> getPage(final Page page) {
return InfoItemsPage.emptyPage();
2017-08-12 21:10:21 +02:00
}
@Nonnull
@Override
public String getName() throws ParsingException {
2020-04-20 14:27:33 +02:00
String name = getTextFromObject(initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title"));
2020-04-15 18:49:58 +02:00
if (!isNullOrEmpty(name)) {
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
2020-02-25 09:07:22 +01:00
public InfoItemsPage<StreamInfoItem> getInitialPage() {
2018-02-24 22:20:50 +01:00
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
final TimeAgoParser timeAgoParser = getTimeAgoParser();
JsonArray itemSectionRenderers = initialData.getObject("contents").getObject("twoColumnBrowseResultsRenderer")
.getArray("tabs").getObject(0).getObject("tabRenderer").getObject("content")
.getObject("sectionListRenderer").getArray("contents");
for (Object itemSectionRenderer : itemSectionRenderers) {
JsonObject expandedShelfContentsRenderer = ((JsonObject) itemSectionRenderer).getObject("itemSectionRenderer")
.getArray("contents").getObject(0).getObject("shelfRenderer").getObject("content")
.getObject("expandedShelfContentsRenderer");
2020-04-16 16:08:14 +02:00
for (Object ul : expandedShelfContentsRenderer.getArray("items")) {
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, null);
2017-08-12 21:10:21 +02:00
}
}