Skip to content

Commit 64f4878

Browse files
committed
[YouTube]
1 parent f551e6b commit 64f4878

4 files changed

Lines changed: 49 additions & 59 deletions

File tree

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.schabi.newpipe.extractor.exceptions.ParsingException;
3838
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
3939
import org.schabi.newpipe.extractor.linkhandler.ReadyChannelTabListLinkHandler;
40+
import org.schabi.newpipe.extractor.search.filter.FilterItem;
4041
import org.schabi.newpipe.extractor.services.youtube.YoutubeChannelHelper;
4142
import org.schabi.newpipe.extractor.services.youtube.YoutubeChannelHelper.ChannelHeader;
4243
import org.schabi.newpipe.extractor.services.youtube.YoutubeChannelHelper.ChannelHeader.HeaderType;
@@ -395,10 +396,10 @@ private List<ListLinkHandler> getTabsForNonAgeRestrictedChannels() throws Parsin
395396
.getArray("tabs");
396397

397398
final List<ListLinkHandler> tabs = new ArrayList<>();
398-
final Consumer<String> addNonVideosTab = tabName -> {
399+
final Consumer<FilterItem> addNonVideosTab = tabName -> {
399400
try {
400401
tabs.add(YoutubeChannelTabLinkHandlerFactory.getInstance().fromQuery(
401-
channelId, List.of(tabName), ""));
402+
channelId, List.of(tabName), List.of()));
402403
} catch (final ParsingException ignored) {
403404
// Do not add the tab if we couldn't create the LinkHandler
404405
}
@@ -466,9 +467,10 @@ private List<ListLinkHandler> getTabsForAgeRestrictedChannels() throws ParsingEx
466467
final List<ListLinkHandler> tabs = new ArrayList<>();
467468
final String channelUrl = getUrl();
468469

469-
final Consumer<String> addTab = tabName ->
470-
tabs.add(new ReadyChannelTabListLinkHandler(channelUrl + "/" + tabName,
471-
channelId, tabName, YoutubeChannelTabPlaylistExtractor::new));
470+
final Consumer<FilterItem> addTab = tab ->
471+
tabs.add(new ReadyChannelTabListLinkHandler(
472+
channelUrl + YoutubeChannelTabLinkHandlerFactory.getUrlSuffix(tab),
473+
channelId, tab, YoutubeChannelTabPlaylistExtractor::new));
472474

473475
addTab.accept(ChannelTabs.VIDEOS);
474476
addTab.accept(ChannelTabs.SHORTS);

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1515
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
1616
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
17+
import org.schabi.newpipe.extractor.search.filter.FilterItem;
1718
import org.schabi.newpipe.extractor.services.youtube.YoutubeChannelHelper;
1819
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelTabLinkHandlerFactory;
1920

@@ -65,24 +66,22 @@ public class YoutubeChannelTabExtractor extends ChannelTabExtractor {
6566
public YoutubeChannelTabExtractor(final StreamingService service,
6667
final ListLinkHandler linkHandler) {
6768
super(service, linkHandler);
68-
useVisitorData = getName().equals(ChannelTabs.SHORTS);
69+
useVisitorData = getChannelTabType().equals(ChannelTabs.SHORTS);
6970
}
7071

7172
@Nonnull
7273
private String getChannelTabsParameters() throws ParsingException {
73-
final String name = getName();
74-
switch (name) {
75-
case ChannelTabs.VIDEOS:
76-
return "EgZ2aWRlb3PyBgQKAjoA";
77-
case ChannelTabs.SHORTS:
78-
return "EgZzaG9ydHPyBgUKA5oBAA%3D%3D";
79-
case ChannelTabs.LIVESTREAMS:
80-
return "EgdzdHJlYW1z8gYECgJ6AA%3D%3D";
81-
case ChannelTabs.PLAYLISTS:
82-
return "EglwbGF5bGlzdHPyBgQKAkIA";
83-
default:
84-
throw new ParsingException("Unsupported channel tab: " + name);
74+
final FilterItem type = getChannelTabType();
75+
if (type.equals(ChannelTabs.VIDEOS)) {
76+
return "EgZ2aWRlb3PyBgQKAjoA";
77+
} else if (type.equals(ChannelTabs.SHORTS)) {
78+
return "EgZzaG9ydHPyBgUKA5oBAA%3D%3D";
79+
} else if (type.equals(ChannelTabs.LIVESTREAMS)) {
80+
return "EgdzdHJlYW1z8gYECgJ6AA%3D%3D";
81+
} else if (type.equals(ChannelTabs.PLAYLISTS)) {
82+
return "EglwbGF5bGlzdHPyBgQKAkIA";
8583
}
84+
throw new ParsingException("Unsupported channel tab: " + type);
8685
}
8786

8887
@Override
@@ -107,7 +106,7 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
107106
public String getUrl() throws ParsingException {
108107
try {
109108
return YoutubeChannelTabLinkHandlerFactory.getInstance()
110-
.getUrl("channel/" + getId(), List.of(getName()), "");
109+
.getUrl("channel/" + getId(), getLinkHandler().getContentFilters(), List.of());
111110
} catch (final ParsingException e) {
112111
return super.getUrl();
113112
}
@@ -250,7 +249,8 @@ public InfoItemsPage<InfoItem> getPage(final Page page)
250249
}
251250

252251
Optional<JsonObject> getTabData() {
253-
final String urlSuffix = YoutubeChannelTabLinkHandlerFactory.getUrlSuffix(getName());
252+
final String urlSuffix =
253+
YoutubeChannelTabLinkHandlerFactory.getUrlSuffix(getChannelTabType());
254254

255255
return jsonResponse.getObject("contents")
256256
.getObject("twoColumnBrowseResultsRenderer")

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1313
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
1414
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
15+
import org.schabi.newpipe.extractor.search.filter.FilterItem;
1516
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubePlaylistLinkHandlerFactory;
1617

1718
import java.io.IOException;
@@ -118,7 +119,7 @@ public InfoItemsPage getPage(final Page page) throws IOException, ExtractionExce
118119
private ListLinkHandler getPlaylistLinkHandler(
119120
@Nonnull final ListLinkHandler originalLinkHandler)
120121
throws IllegalArgumentException, SystemPlaylistUrlCreationException {
121-
final List<String> contentFilters = originalLinkHandler.getContentFilters();
122+
final List<FilterItem> contentFilters = originalLinkHandler.getContentFilters();
122123
if (contentFilters.isEmpty()) {
123124
throw new IllegalArgumentException("A content filter is required");
124125
}
@@ -131,25 +132,21 @@ private ListLinkHandler getPlaylistLinkHandler(
131132
final String channelIdWithoutUc = channelId.substring(2);
132133

133134
final String playlistId;
134-
switch (contentFilters.get(0)) {
135-
case ChannelTabs.VIDEOS:
136-
playlistId = "UULF" + channelIdWithoutUc;
137-
break;
138-
case ChannelTabs.SHORTS:
139-
playlistId = "UUSH" + channelIdWithoutUc;
140-
break;
141-
case ChannelTabs.LIVESTREAMS:
142-
playlistId = "UULV" + channelIdWithoutUc;
143-
break;
144-
default:
145-
throw new IllegalArgumentException(
146-
"Only Videos, Shorts and Livestreams tabs can extracted as playlists");
135+
if (contentFilters.get(0).equals(ChannelTabs.VIDEOS)) {
136+
playlistId = "UULF" + channelIdWithoutUc;
137+
} else if (contentFilters.get(0).equals(ChannelTabs.SHORTS)) {
138+
playlistId = "UUSH" + channelIdWithoutUc;
139+
} else if (contentFilters.get(0).equals(ChannelTabs.LIVESTREAMS)) {
140+
playlistId = "UULV" + channelIdWithoutUc;
141+
} else {
142+
throw new IllegalArgumentException(
143+
"Only Videos, Shorts and Livestreams tabs can extracted as playlists");
147144
}
148145

149146
try {
150147
final String newUrl = YoutubePlaylistLinkHandlerFactory.getInstance()
151148
.getUrl(playlistId);
152-
return new ListLinkHandler(newUrl, newUrl, playlistId, List.of(), "");
149+
return new ListLinkHandler(newUrl, newUrl, playlistId, List.of(), List.of());
153150
} catch (final ParsingException e) {
154151
// This should be not reachable, as the given playlist ID should be valid and
155152
// YoutubePlaylistLinkHandlerFactory doesn't throw any exception

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/linkHandler/YoutubeChannelTabLinkHandlerFactory.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import org.schabi.newpipe.extractor.exceptions.ParsingException;
55
import org.schabi.newpipe.extractor.exceptions.UnsupportedTabException;
66
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
7+
import org.schabi.newpipe.extractor.search.filter.FilterItem;
78

89
import javax.annotation.Nonnull;
10+
import javax.annotation.Nullable;
11+
912
import java.util.List;
1013

1114
public final class YoutubeChannelTabLinkHandlerFactory extends ListLinkHandlerFactory {
@@ -20,26 +23,24 @@ public static YoutubeChannelTabLinkHandlerFactory getInstance() {
2023
}
2124

2225
@Nonnull
23-
public static String getUrlSuffix(@Nonnull final String tab)
26+
public static String getUrlSuffix(@Nonnull final FilterItem tab)
2427
throws UnsupportedTabException {
25-
switch (tab) {
26-
case ChannelTabs.VIDEOS:
27-
return "/videos";
28-
case ChannelTabs.SHORTS:
29-
return "/shorts";
30-
case ChannelTabs.LIVESTREAMS:
31-
return "/streams";
32-
case ChannelTabs.PLAYLISTS:
33-
return "/playlists";
34-
default:
35-
throw new UnsupportedTabException(tab);
28+
if (tab.equals(ChannelTabs.VIDEOS)) {
29+
return "/videos";
30+
} else if (tab.equals(ChannelTabs.SHORTS)) {
31+
return "/shorts";
32+
} else if (tab.equals(ChannelTabs.LIVESTREAMS)) {
33+
return "/streams";
34+
} else if (tab.equals(ChannelTabs.PLAYLISTS)) {
35+
return "/playlists";
3636
}
37+
throw new UnsupportedTabException(tab);
3738
}
3839

3940
@Override
4041
public String getUrl(final String id,
41-
final List<String> contentFilter,
42-
final String sortFilter)
42+
@Nonnull final List<FilterItem> contentFilter,
43+
@Nullable final List<FilterItem> sortFilter)
4344
throws ParsingException, UnsupportedOperationException {
4445
return "https://www.youtube.com/" + id + getUrlSuffix(contentFilter.get(0));
4546
}
@@ -58,14 +59,4 @@ public boolean onAcceptUrl(final String url) throws ParsingException {
5859
}
5960
return true;
6061
}
61-
62-
@Override
63-
public String[] getAvailableContentFilter() {
64-
return new String[] {
65-
ChannelTabs.VIDEOS,
66-
ChannelTabs.SHORTS,
67-
ChannelTabs.LIVESTREAMS,
68-
ChannelTabs.PLAYLISTS
69-
};
70-
}
7162
}

0 commit comments

Comments
 (0)