Skip to content

Commit 822cf30

Browse files
committed
[Youtube] Add _ITEMS constants and improve code style
Move thumbnail id exctraction code to getThumbnailUrlFromId Add test for "My mix" detection to service tests Use ITEM_COUNT_UNKNOWN everywhere instead of -1 and add some tests
1 parent df38b19 commit 822cf30

5 files changed

Lines changed: 55 additions & 38 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/infoItems/MediaCCCConferenceInfoItemExtractor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.schabi.newpipe.extractor.services.media_ccc.extractors.infoItems;
22

33
import com.grack.nanojson.JsonObject;
4+
5+
import org.schabi.newpipe.extractor.ListExtractor;
46
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
57
import org.schabi.newpipe.extractor.exceptions.ParsingException;
68

@@ -23,7 +25,7 @@ public long getSubscriberCount() {
2325

2426
@Override
2527
public long getStreamCount() {
26-
return -1;
28+
return ListExtractor.ITEM_COUNT_UNKNOWN;
2729
}
2830

2931
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.grack.nanojson.JsonObject;
44

5+
import org.schabi.newpipe.extractor.ListExtractor;
56
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
67
import org.schabi.newpipe.extractor.exceptions.ParsingException;
78
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
@@ -86,7 +87,7 @@ public long getStreamCount() throws ParsingException {
8687
try {
8788
if (!channelInfoItem.has("videoCountText")) {
8889
// Video count is not available, channel probably has no public uploads.
89-
return -1;
90+
return ListExtractor.ITEM_COUNT_UNKNOWN;
9091
}
9192

9293
return Long.parseLong(Utils.removeNonDigitCharacters(getTextFromObject(

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

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.IOException;
88
import javax.annotation.Nonnull;
99
import javax.annotation.Nullable;
10+
11+
import org.schabi.newpipe.extractor.ListExtractor;
1012
import org.schabi.newpipe.extractor.StreamingService;
1113
import org.schabi.newpipe.extractor.downloader.Downloader;
1214
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
@@ -23,6 +25,7 @@
2325
*/
2426
public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
2527

28+
2629
private final static String CONTENTS = "contents";
2730
private final static String RESPONSE = "response";
2831
private final static String PLAYLIST = "playlist";
@@ -41,44 +44,25 @@ public void onFetchPage(@Nonnull Downloader downloader)
4144
final String url = getUrl() + "&pbj=1";
4245
final JsonArray ajaxJson = getJsonResponse(url, getExtractorLocalization());
4346
JsonObject initialData = ajaxJson.getObject(3).getObject(RESPONSE);
44-
try {
45-
playlistData = initialData.getObject(CONTENTS).getObject(TWO_COLUMNS_WATCH_NEXT_RESULTS)
46-
.getObject(PLAYLIST).getObject(PLAYLIST);
47-
} catch (NullPointerException e) {
48-
throw new ExtractionException(e);
49-
}
50-
47+
playlistData = initialData.getObject(CONTENTS).getObject(TWO_COLUMN_WATCH_NEXT_RESULTS)
48+
.getObject(PLAYLIST).getObject(PLAYLIST);
5149
}
5250

5351
@Nonnull
5452
@Override
5553
public String getName() throws ParsingException {
56-
try {
57-
final String name = playlistData.getString("title");
58-
if (name != null) {
59-
return name;
60-
} else {
61-
return "";
62-
}
63-
} catch (Exception e) {
64-
throw new ParsingException("Could not get playlist name", e);
54+
final String name = playlistData.getString("title");
55+
if (name == null) {
56+
throw new ParsingException("Could not get playlist name");
6557
}
58+
return name;
6659
}
6760

6861
@Override
6962
public String getThumbnailUrl() throws ParsingException {
7063
try {
7164
final String playlistId = playlistData.getString("playlistId");
72-
final String videoId;
73-
if (playlistId.startsWith("RDMM")) {
74-
videoId = playlistId.substring(4);
75-
} else {
76-
videoId = playlistId.substring(2);
77-
}
78-
if (videoId.isEmpty()) {
79-
throw new ParsingException("");
80-
}
81-
return getThumbnailUrlFromId(videoId);
65+
return getThumbnailUrlFromId(playlistId);
8266
} catch (Exception e) {
8367
throw new ParsingException("Could not get playlist thumbnail", e);
8468
}
@@ -97,20 +81,20 @@ public String getUploaderUrl() {
9781

9882
@Override
9983
public String getUploaderName() {
100-
//Youtube mix are auto-generated
101-
return "";
84+
//Youtube mix are auto-generated by YouTube
85+
return "YouTube";
10286
}
10387

10488
@Override
10589
public String getUploaderAvatarUrl() {
106-
//Youtube mix are auto-generated
90+
//Youtube mix are auto-generated by YouTube
10791
return "";
10892
}
10993

11094
@Override
11195
public long getStreamCount() {
11296
// Auto-generated playlist always start with 25 videos and are endless
113-
return 25;
97+
return ListExtractor.ITEM_COUNT_INFINITE;
11498
}
11599

116100
@Nonnull
@@ -135,7 +119,7 @@ public String getNextPageUrl() throws ExtractionException {
135119

136120
@Override
137121
public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl)
138-
throws ExtractionException, IOException {
122+
throws ExtractionException, IOException {
139123
if (pageUrl == null || pageUrl.isEmpty()) {
140124
throw new ExtractionException(
141125
new IllegalArgumentException("Page url is empty or null"));
@@ -145,7 +129,7 @@ public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl)
145129
final JsonArray ajaxJson = getJsonResponse(pageUrl, getExtractorLocalization());
146130
playlistData =
147131
ajaxJson.getObject(3).getObject(RESPONSE).getObject(CONTENTS)
148-
.getObject(TWO_COLUMNS_WATCH_NEXT_RESULTS).getObject(PLAYLIST)
132+
.getObject(TWO_COLUMN_WATCH_NEXT_RESULTS).getObject(PLAYLIST)
149133
.getObject(PLAYLIST);
150134
final JsonArray streams = playlistData.getArray(CONTENTS);
151135
//Because continuation requests are created with the last video of previous request as start
@@ -155,8 +139,8 @@ public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl)
155139
}
156140

157141
private void collectStreamsFrom(
158-
@Nonnull StreamInfoItemsCollector collector,
159-
@Nullable JsonArray streams) {
142+
@Nonnull StreamInfoItemsCollector collector,
143+
@Nullable JsonArray streams) {
160144
collector.reset();
161145

162146
if (streams == null) {
@@ -176,7 +160,16 @@ private void collectStreamsFrom(
176160
}
177161
}
178162

179-
private String getThumbnailUrlFromId(String videoId) {
163+
private String getThumbnailUrlFromId(String playlistId) throws ParsingException {
164+
final String videoId;
165+
if (playlistId.startsWith("RDMM")) {
166+
videoId = playlistId.substring(4);
167+
} else {
168+
videoId = playlistId.substring(2);
169+
}
170+
if (videoId.isEmpty()) {
171+
throw new ParsingException("videoId is empty");
172+
}
180173
return "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg";
181174
}
182175
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMixPlaylistExtractorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public void getPageMultipleTimes() throws Exception {
9393
public void getStreamCount() throws Exception {
9494
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
9595
}
96+
97+
@Test
98+
public void getStreamCount() throws Exception {
99+
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
100+
}
96101
}
97102

98103
public static class MixWithIndex {
@@ -165,6 +170,11 @@ public void getPageMultipleTimes() throws Exception {
165170
public void getStreamCount() {
166171
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
167172
}
173+
174+
@Test
175+
public void getStreamCount() throws Exception {
176+
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
177+
}
168178
}
169179

170180
public static class MyMix {
@@ -319,5 +329,10 @@ public void getPage() throws Exception {
319329
public void getStreamCount() throws Exception {
320330
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
321331
}
332+
333+
@Test
334+
public void getStreamCount() throws Exception {
335+
assertEquals(ListExtractor.ITEM_COUNT_INFINITE, extractor.getStreamCount());
336+
}
322337
}
323338
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeServiceTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ public void getPlaylistExtractorIsMix() throws Exception {
7575
assertTrue(extractor instanceof YoutubeMixPlaylistExtractor);
7676

7777
extractor = YouTube.getPlaylistExtractor(
78-
"https://www.youtube.com/watch?v=" + videoId + "&list=RD" + videoId);
78+
"https://www.youtube.com/watch?v=" + videoId + "&list=RDMM" + videoId);
79+
assertTrue(extractor instanceof YoutubeMixPlaylistExtractor);
80+
81+
final String mixVideoId = "qHtzO49SDmk";
82+
83+
extractor = YouTube.getPlaylistExtractor(
84+
"https://www.youtube.com/watch?v=" + mixVideoId + "&list=RD" + videoId);
7985
assertTrue(extractor instanceof YoutubeMixPlaylistExtractor);
8086
}
8187
}

0 commit comments

Comments
 (0)