2018-05-26 19:15:45 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
|
|
|
|
2020-02-22 20:19:41 +01:00
|
|
|
import com.grack.nanojson.JsonArray;
|
|
|
|
import com.grack.nanojson.JsonObject;
|
2020-07-26 10:01:03 +02:00
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
|
|
|
import com.grack.nanojson.JsonWriter;
|
|
|
|
|
2018-05-26 19:15:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.InfoItem;
|
2020-04-15 14:09:46 +02:00
|
|
|
import org.schabi.newpipe.extractor.Page;
|
2018-05-26 19:15:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
2018-05-26 19:15:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2019-10-02 07:02:01 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
|
2018-05-26 19:15:45 +02:00
|
|
|
import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector;
|
|
|
|
import org.schabi.newpipe.extractor.search.SearchExtractor;
|
2020-04-11 19:18:08 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.JsonUtils;
|
2018-05-26 19:15:45 +02:00
|
|
|
|
2020-04-11 19:18:08 +02:00
|
|
|
import java.io.IOException;
|
2020-07-26 10:01:03 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2020-02-22 20:19:41 +01:00
|
|
|
|
2020-07-26 10:01:03 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getClientVersion;
|
2020-04-10 10:51:05 +02:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse;
|
2020-07-26 12:00:56 +02:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey;
|
2020-04-10 10:51:05 +02:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
|
2020-07-26 10:01:03 +02:00
|
|
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody;
|
2020-05-11 11:40:24 +02:00
|
|
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
2020-02-27 17:39:23 +01:00
|
|
|
|
2018-07-01 16:21:40 +02:00
|
|
|
/*
|
|
|
|
* Created by Christian Schabesberger on 22.07.2018
|
|
|
|
*
|
|
|
|
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
|
|
|
|
* YoutubeSearchExtractor.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/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-26 19:15:45 +02:00
|
|
|
public class YoutubeSearchExtractor extends SearchExtractor {
|
2020-02-22 23:51:02 +01:00
|
|
|
private JsonObject initialData;
|
2018-05-26 19:15:45 +02:00
|
|
|
|
2020-04-01 10:03:54 +02:00
|
|
|
public YoutubeSearchExtractor(final StreamingService service, final SearchQueryHandler linkHandler) {
|
2019-04-28 22:03:16 +02:00
|
|
|
super(service, linkHandler);
|
2018-05-26 19:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-01 10:03:54 +02:00
|
|
|
public void onFetchPage(@Nonnull final Downloader downloader) throws IOException, ExtractionException {
|
|
|
|
final String url = getUrl() + "&pbj=1";
|
2020-02-26 09:01:57 +01:00
|
|
|
|
2020-04-01 10:03:54 +02:00
|
|
|
final JsonArray ajaxJson = getJsonResponse(url, getExtractorLocalization());
|
2020-02-26 09:01:57 +01:00
|
|
|
|
2020-04-01 10:03:54 +02:00
|
|
|
initialData = ajaxJson.getObject(1).getObject("response");
|
2018-05-26 19:15:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
@Nonnull
|
2018-10-25 15:46:47 +02:00
|
|
|
@Override
|
|
|
|
public String getUrl() throws ParsingException {
|
2019-04-28 22:03:16 +02:00
|
|
|
return super.getUrl() + "&gl=" + getExtractorContentCountry().getCountryCode();
|
2018-10-25 15:46:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 16:06:52 +02:00
|
|
|
@Nonnull
|
2018-05-26 19:15:45 +02:00
|
|
|
@Override
|
2020-02-29 22:57:25 +01:00
|
|
|
public String getSearchSuggestion() throws ParsingException {
|
2020-04-12 16:06:52 +02:00
|
|
|
final JsonObject itemSectionRenderer = initialData.getObject("contents")
|
2020-04-01 10:03:54 +02:00
|
|
|
.getObject("twoColumnSearchResultsRenderer").getObject("primaryContents")
|
|
|
|
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
2020-04-12 16:06:52 +02:00
|
|
|
.getObject("itemSectionRenderer");
|
|
|
|
final JsonObject didYouMeanRenderer = itemSectionRenderer.getArray("contents").getObject(0)
|
2020-04-11 19:18:08 +02:00
|
|
|
.getObject("didYouMeanRenderer");
|
2020-04-12 16:06:52 +02:00
|
|
|
final JsonObject showingResultsForRenderer = itemSectionRenderer.getArray("contents").getObject(0)
|
2020-04-01 10:03:54 +02:00
|
|
|
.getObject("showingResultsForRenderer");
|
2020-04-12 16:06:52 +02:00
|
|
|
|
2020-05-07 15:32:25 +02:00
|
|
|
if (!didYouMeanRenderer.isEmpty()) {
|
2020-04-12 16:06:52 +02:00
|
|
|
return JsonUtils.getString(didYouMeanRenderer, "correctedQueryEndpoint.searchEndpoint.query");
|
|
|
|
} else if (showingResultsForRenderer != null) {
|
|
|
|
return getTextFromObject(showingResultsForRenderer.getObject("correctedQuery"));
|
|
|
|
} else {
|
2020-04-01 10:03:54 +02:00
|
|
|
return "";
|
2018-05-26 19:15:45 +02:00
|
|
|
}
|
2020-04-11 19:18:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isCorrectedSearch() {
|
|
|
|
final JsonObject showingResultsForRenderer = initialData.getObject("contents")
|
|
|
|
.getObject("twoColumnSearchResultsRenderer").getObject("primaryContents")
|
|
|
|
.getObject("sectionListRenderer").getArray("contents").getObject(0)
|
|
|
|
.getObject("itemSectionRenderer").getArray("contents").getObject(0)
|
|
|
|
.getObject("showingResultsForRenderer");
|
2020-05-07 15:32:25 +02:00
|
|
|
return !showingResultsForRenderer.isEmpty();
|
2018-05-26 19:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2020-07-26 12:00:56 +02:00
|
|
|
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException {
|
2020-03-23 22:05:02 +01:00
|
|
|
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
|
2020-02-28 08:57:44 +01:00
|
|
|
|
2020-04-01 10:03:54 +02:00
|
|
|
final JsonArray sections = initialData.getObject("contents").getObject("twoColumnSearchResultsRenderer")
|
|
|
|
.getObject("primaryContents").getObject("sectionListRenderer").getArray("contents");
|
2020-03-17 11:33:39 +01:00
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
Page nextPage = null;
|
2020-04-10 10:25:53 +02:00
|
|
|
|
2020-07-26 10:01:03 +02:00
|
|
|
for (final Object section : sections) {
|
|
|
|
if (((JsonObject) section).has("itemSectionRenderer")) {
|
|
|
|
final JsonObject itemSectionRenderer = ((JsonObject) section).getObject("itemSectionRenderer");
|
2020-02-24 18:24:36 +01:00
|
|
|
|
2020-07-26 10:01:03 +02:00
|
|
|
collectStreamsFrom(collector, itemSectionRenderer.getArray("contents"));
|
2018-05-26 19:15:45 +02:00
|
|
|
|
2020-07-26 10:01:03 +02:00
|
|
|
nextPage = getNextPageFrom(itemSectionRenderer.getArray("continuations"));
|
|
|
|
} else if (((JsonObject) section).has("continuationItemRenderer")) {
|
|
|
|
nextPage = getNewNextPageFrom(((JsonObject) section).getObject("continuationItemRenderer"));
|
|
|
|
}
|
2020-04-10 10:25:53 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
return new InfoItemsPage<>(collector, nextPage);
|
2018-05-26 19:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-15 14:09:46 +02:00
|
|
|
public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, ExtractionException {
|
2020-05-11 15:25:18 +02:00
|
|
|
if (page == null || isNullOrEmpty(page.getUrl())) {
|
|
|
|
throw new IllegalArgumentException("Page doesn't contain an URL");
|
|
|
|
}
|
|
|
|
|
2020-03-23 22:05:02 +01:00
|
|
|
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
|
2020-02-26 09:01:57 +01:00
|
|
|
|
2020-07-26 10:01:03 +02:00
|
|
|
if (page.getId() == null) {
|
|
|
|
final JsonArray ajaxJson = getJsonResponse(page.getUrl(), getExtractorLocalization());
|
|
|
|
|
|
|
|
final JsonObject itemSectionContinuation = ajaxJson.getObject(1).getObject("response")
|
|
|
|
.getObject("continuationContents").getObject("itemSectionContinuation");
|
|
|
|
|
|
|
|
collectStreamsFrom(collector, itemSectionContinuation.getArray("contents"));
|
|
|
|
final JsonArray continuations = itemSectionContinuation.getArray("continuations");
|
|
|
|
|
|
|
|
return new InfoItemsPage<>(collector, getNextPageFrom(continuations));
|
|
|
|
} else {
|
|
|
|
// @formatter:off
|
|
|
|
final byte[] json = JsonWriter.string()
|
|
|
|
.object()
|
|
|
|
.object("context")
|
|
|
|
.object("client")
|
|
|
|
.value("hl", "en")
|
|
|
|
.value("gl", getExtractorContentCountry().getCountryCode())
|
|
|
|
.value("clientName", "WEB")
|
|
|
|
.value("clientVersion", getClientVersion())
|
|
|
|
.value("utcOffsetMinutes", 0)
|
|
|
|
.end()
|
|
|
|
.object("request").end()
|
|
|
|
.object("user").end()
|
|
|
|
.end()
|
|
|
|
.value("continuation", page.getId())
|
|
|
|
.end().done().getBytes("UTF-8");
|
|
|
|
// @formatter:on
|
|
|
|
|
|
|
|
final Map<String, List<String>> headers = new HashMap<>();
|
|
|
|
headers.put("Origin", Collections.singletonList("https://www.youtube.com"));
|
|
|
|
headers.put("Referer", Collections.singletonList(this.getUrl()));
|
|
|
|
headers.put("Content-Type", Collections.singletonList("application/json"));
|
|
|
|
|
|
|
|
final String responseBody = getValidJsonResponseBody(getDownloader().post(page.getUrl(), headers, json));
|
|
|
|
|
|
|
|
final JsonObject ajaxJson;
|
|
|
|
try {
|
|
|
|
ajaxJson = JsonParser.object().from(responseBody);
|
|
|
|
} catch (JsonParserException e) {
|
|
|
|
throw new ParsingException("Could not parse JSON", e);
|
|
|
|
}
|
2020-03-17 11:33:39 +01:00
|
|
|
|
2020-07-26 10:01:03 +02:00
|
|
|
final JsonArray continuationItems = ajaxJson.getArray("onResponseReceivedCommands")
|
|
|
|
.getObject(0).getObject("appendContinuationItemsAction").getArray("continuationItems");
|
2020-03-17 11:33:39 +01:00
|
|
|
|
2020-07-26 10:01:03 +02:00
|
|
|
final JsonArray contents = continuationItems.getObject(0).getObject("itemSectionRenderer").getArray("contents");
|
|
|
|
collectStreamsFrom(collector, contents);
|
|
|
|
|
|
|
|
return new InfoItemsPage<>(collector, getNewNextPageFrom(continuationItems.getObject(1).getObject("continuationItemRenderer")));
|
|
|
|
}
|
2018-05-26 19:15:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-01 10:03:54 +02:00
|
|
|
private void collectStreamsFrom(final InfoItemsSearchCollector collector, final JsonArray videos) throws NothingFoundException, ParsingException {
|
2019-04-28 22:03:16 +02:00
|
|
|
final TimeAgoParser timeAgoParser = getTimeAgoParser();
|
2018-05-26 19:15:45 +02:00
|
|
|
|
2020-02-24 18:24:36 +01:00
|
|
|
for (Object item : videos) {
|
2020-04-16 16:08:14 +02:00
|
|
|
if (((JsonObject) item).has("backgroundPromoRenderer")) {
|
2020-02-27 17:39:23 +01:00
|
|
|
throw new NothingFoundException(getTextFromObject(((JsonObject) item)
|
|
|
|
.getObject("backgroundPromoRenderer").getObject("bodyText")));
|
2020-04-16 16:08:14 +02:00
|
|
|
} else if (((JsonObject) item).has("videoRenderer")) {
|
2020-02-22 20:19:41 +01:00
|
|
|
collector.commit(new YoutubeStreamInfoItemExtractor(((JsonObject) item).getObject("videoRenderer"), timeAgoParser));
|
2020-04-16 16:08:14 +02:00
|
|
|
} else if (((JsonObject) item).has("channelRenderer")) {
|
2020-02-23 18:27:28 +01:00
|
|
|
collector.commit(new YoutubeChannelInfoItemExtractor(((JsonObject) item).getObject("channelRenderer")));
|
2020-04-16 16:08:14 +02:00
|
|
|
} else if (((JsonObject) item).has("playlistRenderer")) {
|
2020-02-23 19:45:45 +01:00
|
|
|
collector.commit(new YoutubePlaylistInfoItemExtractor(((JsonObject) item).getObject("playlistRenderer")));
|
2018-05-26 19:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 20:19:41 +01:00
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
private Page getNextPageFrom(final JsonArray continuations) throws ParsingException {
|
2020-05-11 11:40:24 +02:00
|
|
|
if (isNullOrEmpty(continuations)) {
|
2020-04-15 14:09:46 +02:00
|
|
|
return null;
|
2020-03-17 11:33:39 +01:00
|
|
|
}
|
|
|
|
|
2020-04-01 10:03:54 +02:00
|
|
|
final JsonObject nextContinuationData = continuations.getObject(0).getObject("nextContinuationData");
|
|
|
|
final String continuation = nextContinuationData.getString("continuation");
|
|
|
|
final String clickTrackingParams = nextContinuationData.getString("clickTrackingParams");
|
2020-02-24 18:24:36 +01:00
|
|
|
|
2020-04-15 14:09:46 +02:00
|
|
|
return new Page(getUrl() + "&pbj=1&ctoken=" + continuation + "&continuation=" + continuation
|
|
|
|
+ "&itct=" + clickTrackingParams);
|
2020-02-24 18:24:36 +01:00
|
|
|
}
|
2020-07-26 10:01:03 +02:00
|
|
|
|
2020-07-26 12:00:56 +02:00
|
|
|
private Page getNewNextPageFrom(final JsonObject continuationItemRenderer) throws IOException, ExtractionException {
|
2020-07-26 10:01:03 +02:00
|
|
|
if (isNullOrEmpty(continuationItemRenderer)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String token = continuationItemRenderer.getObject("continuationEndpoint")
|
|
|
|
.getObject("continuationCommand").getString("token");
|
|
|
|
|
2020-07-26 12:00:56 +02:00
|
|
|
final String url = "https://www.youtube.com/youtubei/v1/search?key=" + getKey();
|
2020-07-26 10:01:03 +02:00
|
|
|
|
|
|
|
return new Page(url, token);
|
|
|
|
}
|
2018-05-26 19:15:45 +02:00
|
|
|
}
|