-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Youtube kiosk (Trending Music) addition Issue #8801 NewPipe (Partial Solution) #955
Open
CaptSzat
wants to merge
3
commits into
TeamNewPipe:dev
Choose a base branch
from
CaptSzat:kiosk-YT-Music-addition
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...g/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicTrendingExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Created by Christian Schabesberger on 12.08.17. | ||
* | ||
* Copyright (C) Christian Schabesberger 2018 <[email protected]> | ||
* YoutubeTrendingExtractor.java is part of NewPipe Extractor. | ||
* | ||
* NewPipe Extractor 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 Extractor 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 Extractor. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package org.schabi.newpipe.extractor.services.youtube.extractors; | ||
|
||
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse; | ||
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextAtKey; | ||
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder; | ||
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8; | ||
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; | ||
|
||
import com.grack.nanojson.JsonObject; | ||
import com.grack.nanojson.JsonWriter; | ||
|
||
import org.schabi.newpipe.extractor.Page; | ||
import org.schabi.newpipe.extractor.StreamingService; | ||
import org.schabi.newpipe.extractor.downloader.Downloader; | ||
import org.schabi.newpipe.extractor.exceptions.ExtractionException; | ||
import org.schabi.newpipe.extractor.exceptions.ParsingException; | ||
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.stream.StreamInfoItem; | ||
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
public class YoutubeMusicTrendingExtractor extends KioskExtractor<StreamInfoItem> { | ||
private JsonObject initialData; | ||
|
||
public YoutubeMusicTrendingExtractor(final StreamingService service, | ||
final ListLinkHandler linkHandler, | ||
final String kioskId) { | ||
super(service, linkHandler, kioskId); | ||
} | ||
|
||
@Override | ||
public void onFetchPage(@Nonnull final Downloader downloader) | ||
throws IOException, ExtractionException { | ||
// @formatter:off | ||
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(getExtractorLocalization(), | ||
getExtractorContentCountry()) | ||
.value("browseId", "MUtrending") | ||
.done()) | ||
.getBytes(UTF_8); | ||
// @formatter:on | ||
|
||
initialData = getJsonPostResponse("browse", body, getExtractorLocalization()); | ||
} | ||
|
||
@Override | ||
public InfoItemsPage<StreamInfoItem> getPage(final Page page) { | ||
return InfoItemsPage.emptyPage(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getName() throws ParsingException { | ||
final JsonObject header = initialData.getObject("header"); | ||
String name = null; | ||
if (header.has("feedTabbedHeaderRenderer")) { | ||
name = getTextAtKey(header.getObject("feedTabbedHeaderRenderer"), "title"); | ||
} else if (header.has("c4TabbedHeaderRenderer")) { | ||
name = getTextAtKey(header.getObject("c4TabbedHeaderRenderer"), "title"); | ||
} | ||
|
||
if (isNullOrEmpty(name)) { | ||
throw new ParsingException("Could not get Trending name"); | ||
} | ||
return name; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ParsingException { | ||
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); | ||
final TimeAgoParser timeAgoParser = getTimeAgoParser(); | ||
final JsonObject tabContent = getTrendingTabContent(); | ||
|
||
if (tabContent.has("richGridRenderer")) { | ||
tabContent.getObject("richGridRenderer") | ||
.getArray("contents") | ||
.stream() | ||
.filter(JsonObject.class::isInstance) | ||
.map(JsonObject.class::cast) | ||
// Filter Trending shorts and Recently trending sections | ||
.filter(content -> content.has("richItemRenderer")) | ||
// .filter(shelfRenderer -> !shelfRenderer.has("music")) | ||
.map(content -> content.getObject("richItemRenderer") | ||
.getObject("content") | ||
.getObject("videoRenderer")) | ||
.forEachOrdered(videoRenderer -> collector.commit( | ||
new YoutubeStreamInfoItemExtractor(videoRenderer, timeAgoParser))); | ||
} else if (tabContent.has("sectionListRenderer")) { | ||
tabContent.getObject("sectionListRenderer") | ||
.getArray("contents") | ||
.stream() | ||
.filter(JsonObject.class::isInstance) | ||
.map(JsonObject.class::cast) | ||
.flatMap(content -> content.getObject("itemSectionRenderer") | ||
.getArray("contents") | ||
.stream()) | ||
.filter(JsonObject.class::isInstance) | ||
.map(JsonObject.class::cast) | ||
.map(content -> content.getObject("shelfRenderer")) | ||
// Filter Trending shorts and Recently trending sections which have a title, | ||
// contrary to normal trends | ||
.filter(shelfRenderer -> !shelfRenderer.has("title")) | ||
// .filter(shelfRenderer -> !shelfRenderer.has("music")) | ||
.flatMap(shelfRenderer -> shelfRenderer.getObject("content") | ||
.getObject("expandedShelfContentsRenderer") | ||
.getArray("items") | ||
.stream()) | ||
.filter(JsonObject.class::isInstance) | ||
.map(JsonObject.class::cast) | ||
.map(item -> item.getObject("videoRenderer")) | ||
.forEachOrdered(videoRenderer -> collector.commit( | ||
new YoutubeStreamInfoItemExtractor(videoRenderer, timeAgoParser))); | ||
} | ||
|
||
return new InfoItemsPage<>(collector, null); | ||
} | ||
|
||
private JsonObject getTrendingTabContent() throws ParsingException { | ||
return initialData.getObject("contents") | ||
.getObject("twoColumnBrowseResultsRenderer") | ||
.getArray("tabs") | ||
.stream() | ||
.filter(JsonObject.class::isInstance) | ||
.map(JsonObject.class::cast) | ||
.map(tab -> tab.getObject("tabRenderer")) | ||
.filter(tabRenderer -> tabRenderer.getBoolean("selected")) | ||
.filter(tabRenderer -> tabRenderer.has("content")) | ||
// There should be at most one tab selected | ||
.findFirst() | ||
.orElseThrow(() -> new ParsingException("Could not get \"Now\" trending tab")) | ||
.getObject("content"); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ewpipe/extractor/services/youtube/linkHandler/YoutubeMusicTrendingLinkHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.schabi.newpipe.extractor.services.youtube.linkHandler; | ||
|
||
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isInvidioURL; | ||
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeURL; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.List; | ||
import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException; | ||
import org.schabi.newpipe.extractor.exceptions.ParsingException; | ||
import org.schabi.newpipe.extractor.linkhandler.LinkHandler; | ||
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; | ||
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; | ||
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; | ||
import org.schabi.newpipe.extractor.utils.Utils; | ||
|
||
public final class YoutubeMusicTrendingLinkHandlerFactory extends ListLinkHandlerFactory { | ||
|
||
public String getUrl(final String id, | ||
final List<String> contentFilters, | ||
final String sortFilter) { | ||
return "https://www.youtube.com/feed/trending/music"; | ||
} | ||
|
||
@Override | ||
public String getId(final String url) { | ||
return "Trending"; | ||
} | ||
|
||
@Override | ||
public boolean onAcceptUrl(final String url) { | ||
final URL urlObj; | ||
try { | ||
urlObj = Utils.stringToURL(url); | ||
} catch (final MalformedURLException e) { | ||
return false; | ||
} | ||
|
||
final String urlPath = urlObj.getPath(); | ||
return Utils.isHTTP(urlObj) && (isYoutubeURL(urlObj) || isInvidioURL(urlObj)) | ||
&& urlPath.equals("/feed/trending/music"); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...java/org/schabi/newpipe/extractor/services/youtube/YoutubeMusicTrendingKioskInfoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.schabi.newpipe.extractor.services.youtube; | ||
|
||
/* | ||
* Created by Christian Schabesberger on 12.08.17. | ||
* | ||
* Copyright (C) Christian Schabesberger 2017 <[email protected]> | ||
* YoutubeTrendingKioskInfoTest.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/>. | ||
*/ | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.schabi.newpipe.extractor.ServiceList.YouTube; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.schabi.newpipe.downloader.DownloaderFactory; | ||
import org.schabi.newpipe.extractor.NewPipe; | ||
import org.schabi.newpipe.extractor.StreamingService; | ||
import org.schabi.newpipe.extractor.kiosk.KioskInfo; | ||
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory; | ||
|
||
/** | ||
* Test for {@link KioskInfo} | ||
*/ | ||
public class YoutubeMusicTrendingKioskInfoTest { | ||
|
||
private static final String RESOURCE_PATH = DownloaderFactory.RESOURCE_PATH + "kiosk"; | ||
|
||
static KioskInfo kioskInfo; | ||
|
||
@BeforeAll | ||
public static void setUp() | ||
throws Exception { | ||
YoutubeTestsUtils.ensureStateless(); | ||
NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH)); | ||
LinkHandlerFactory LinkHandlerFactory = ((StreamingService) YouTube).getKioskList().getListLinkHandlerFactoryByType("Trending"); | ||
|
||
kioskInfo = KioskInfo.getInfo(YouTube, LinkHandlerFactory.fromId("Trending").getUrl()); | ||
} | ||
|
||
@Test | ||
public void getStreams() { | ||
assertFalse(kioskInfo.getRelatedItems().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void getId() { | ||
assertTrue(kioskInfo.getId().equals("Trending Music") | ||
|| kioskInfo.getId().equals("Trends")); | ||
} | ||
|
||
@Test | ||
public void getName() { | ||
assertFalse(kioskInfo.getName().isEmpty()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried looking for this
browseId
everywhere, onyoutube.com
andmusic.youtube.com
(There's no trending page at all).What I found out is that the official client uses a
browseId
ofFEtrending
with theparams
as4gINGgt5dG1hX2NoYXJ0cw%3D%3D
, which is URL and base64 encoded protobuf.The protobuf decoded looks like this:
I think we should change this to follow the official client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
music: '4gINGgt5dG1hX2NoYXJ0cw%3D%3D',
gaming: '4gIcGhpnYW1pbmdfY29ycHVzX21vc3RfcG9wdWxhcg%3D%3D',
movies: '4gIKGgh0cmFpbGVycw%3D%3D'