Skip to content

Commit 8163518

Browse files
Fix compile errors
1 parent adffae9 commit 8163518

3 files changed

Lines changed: 60 additions & 106 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMetaInfoHelper.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package org.schabi.newpipe.extractor.services.youtube;
22

3-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractCachedUrlIfNeeded;
4-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
5-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObjectOrThrow;
6-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
7-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isGoogleURL;
8-
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
9-
103
import com.grack.nanojson.JsonArray;
114
import com.grack.nanojson.JsonObject;
12-
135
import org.schabi.newpipe.extractor.MetaInfo;
146
import org.schabi.newpipe.extractor.exceptions.ParsingException;
157
import org.schabi.newpipe.extractor.stream.Description;
168

9+
import javax.annotation.Nonnull;
1710
import java.net.MalformedURLException;
1811
import java.net.URL;
1912
import java.util.ArrayList;
@@ -22,14 +15,19 @@
2215
import java.util.function.Consumer;
2316
import java.util.stream.Collectors;
2417

25-
import javax.annotation.Nonnull;
18+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractCachedUrlIfNeeded;
19+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
20+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObjectOrThrow;
21+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
22+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isGoogleURL;
23+
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
2624

2725
public final class YoutubeMetaInfoHelper {
26+
private static final String ACTION_TEXT = "actionText";
2827

2928
private YoutubeMetaInfoHelper() {
3029
}
3130

32-
3331
@Nonnull
3432
public static List<MetaInfo> getMetaInfo(@Nonnull final JsonArray contents)
3533
throws ParsingException {
@@ -82,17 +80,10 @@ private static MetaInfo getInfoPanelContent(@Nonnull final JsonObject infoPanelC
8280
throw new ParsingException("Could not get metadata info URL", e);
8381
}
8482

85-
final String metaInfoLinkText;
86-
if (infoPanelContentRenderer.has("inlineSource")) {
87-
metaInfoLinkText = getTextFromObject(
88-
infoPanelContentRenderer.getObject("inlineSource"));
89-
} else {
90-
metaInfoLinkText = getTextFromObject(
91-
infoPanelContentRenderer.getObject("disclaimer"));
92-
}
93-
if (isNullOrEmpty(metaInfoLinkText)) {
94-
throw new ParsingException("Could not get metadata info link text.");
95-
}
83+
final var source = infoPanelContentRenderer.getObject("inlineSource",
84+
infoPanelContentRenderer.getObject("disclaimer"));
85+
final String metaInfoLinkText = getTextFromObjectOrThrow(source,
86+
"metadata info link");
9687
metaInfo.addUrlText(metaInfoLinkText);
9788
}
9889

@@ -170,16 +161,16 @@ private static void getEmergencyOneboxRenderer(
170161

171162
// usually a phone number
172163
final String action; // this variable is expected to start with "\n"
173-
if (r.has("actionText")) {
174-
action = "\n" + getTextFromObjectOrThrow(r.getObject("actionText"), "action");
164+
if (r.has(ACTION_TEXT)) {
165+
action = "\n" + getTextFromObjectOrThrow(r.getObject(ACTION_TEXT), "action");
175166
} else if (r.has("contacts")) {
176167
final JsonArray contacts = r.getArray("contacts");
177168
final StringBuilder stringBuilder = new StringBuilder();
178169
// Loop over contacts item from the first contact to the last one
179170
for (int i = 0; i < contacts.size(); i++) {
180171
stringBuilder.append("\n");
181172
stringBuilder.append(getTextFromObjectOrThrow(contacts.getObject(i)
182-
.getObject("actionText"), "contacts.actionText"));
173+
.getObject(ACTION_TEXT), "contacts.actionText"));
183174
}
184175
action = stringBuilder.toString();
185176
} else {

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

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -258,32 +258,21 @@ public List<Image> getThumbnails() throws ParsingException {
258258
public Description getDescription() {
259259
assertPageFetched();
260260
// Description with more info on links
261-
final String videoSecondaryInfoRendererDescription = getTextFromObject(
262-
getVideoSecondaryInfoRenderer().getObject("description"),
263-
true);
264-
if (!isNullOrEmpty(videoSecondaryInfoRendererDescription)) {
265-
return new Description(videoSecondaryInfoRendererDescription, Description.HTML);
266-
}
267-
268-
final String attributedDescription = attributedDescriptionToHtml(
269-
getVideoSecondaryInfoRenderer().getObject("attributedDescription"));
270-
if (!isNullOrEmpty(attributedDescription)) {
271-
return new Description(attributedDescription, Description.HTML);
272-
}
273-
274-
String description = playerResponse.getObject(VIDEO_DETAILS)
275-
.getString("shortDescription");
276-
if (description == null) {
277-
final JsonObject descriptionObject = playerMicroFormatRenderer.getObject("description");
278-
description = getTextFromObject(descriptionObject);
279-
}
280-
281-
// Raw non-html description
282-
return new Description(description, Description.PLAIN_TEXT);
261+
return getTextFromObject(getVideoSecondaryInfoRenderer().getObject("description"), true)
262+
.or(() -> Optional.ofNullable(attributedDescriptionToHtml(
263+
getVideoSecondaryInfoRenderer().getObject("attributedDescription"))))
264+
.map(description -> new Description(description, Description.HTML))
265+
.or(() -> Optional.ofNullable(playerResponse.getObject(VIDEO_DETAILS)
266+
.getString("shortDescription"))
267+
.or(() -> getTextFromObject(playerMicroFormatRenderer
268+
.getObject("description")))
269+
// Raw non-html description
270+
.map(description -> new Description(description, Description.PLAIN_TEXT)))
271+
.orElse(Description.EMPTY_DESCRIPTION);
283272
}
284273

285274
@Override
286-
public int getAgeLimit() throws ParsingException {
275+
public int getAgeLimit() {
287276
if (ageLimit != -1) {
288277
return ageLimit;
289278
}
@@ -574,7 +563,7 @@ public long getUploaderSubscriberCount() throws ParsingException {
574563

575564
@Nonnull
576565
@Override
577-
public String getDashMpdUrl() throws ParsingException {
566+
public String getDashMpdUrl() {
578567
assertPageFetched();
579568

580569
// There is no DASH manifest available with the iOS client
@@ -588,7 +577,7 @@ public String getDashMpdUrl() throws ParsingException {
588577

589578
@Nonnull
590579
@Override
591-
public String getHlsUrl() throws ParsingException {
580+
public String getHlsUrl() {
592581
assertPageFetched();
593582

594583
// Return HLS manifest of the iOS client first because on livestreams, the HLS manifest
@@ -1445,7 +1434,7 @@ public String getCategory() {
14451434

14461435
@Nonnull
14471436
@Override
1448-
public String getLicence() throws ParsingException {
1437+
public String getLicence() {
14491438
final var metadataRowRenderer = getVideoSecondaryInfoRenderer()
14501439
.getObject("metadataRowContainer")
14511440
.getObject("metadataRowContainerRenderer")

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

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,35 @@
1818

1919
package org.schabi.newpipe.extractor.services.youtube.extractors;
2020

21-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
22-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getThumbnailsFromInfoItem;
23-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getImagesFromThumbnailsArray;
24-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
25-
2621
import com.grack.nanojson.JsonArray;
2722
import com.grack.nanojson.JsonObject;
28-
2923
import org.schabi.newpipe.extractor.Image;
3024
import org.schabi.newpipe.extractor.exceptions.ParsingException;
3125
import org.schabi.newpipe.extractor.localization.DateWrapper;
3226
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
3327
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
3428
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory;
29+
import org.schabi.newpipe.extractor.stream.ContentAvailability;
3530
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
3631
import org.schabi.newpipe.extractor.stream.StreamType;
37-
import org.schabi.newpipe.extractor.stream.ContentAvailability;
3832
import org.schabi.newpipe.extractor.utils.JsonUtils;
3933
import org.schabi.newpipe.extractor.utils.Parser;
4034
import org.schabi.newpipe.extractor.utils.Utils;
4135

4236
import javax.annotation.Nonnull;
4337
import javax.annotation.Nullable;
44-
4538
import java.time.Instant;
4639
import java.time.LocalDateTime;
4740
import java.time.ZoneId;
4841
import java.time.format.DateTimeFormatter;
4942
import java.util.List;
5043
import java.util.Optional;
5144
import java.util.regex.Pattern;
52-
import java.util.stream.Collectors;
45+
46+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getImagesFromThumbnailsArray;
47+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
48+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getThumbnailsFromInfoItem;
49+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
5350

5451
public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
5552

@@ -132,15 +129,9 @@ public String getUrl() throws ParsingException {
132129
@Override
133130
public String getName() throws ParsingException {
134131
final JsonObject title = videoInfo.getObject("title");
135-
final String name = getTextFromObject(title);
136-
if (!isNullOrEmpty(name)) {
137-
return name;
138-
}
139-
// Videos can have no title, e.g. https://www.youtube.com/watch?v=nc1kN8ZSfGQ
140-
if (!isNullOrEmpty(title) && !title.has("runs")) {
141-
return "";
142-
}
143-
throw new ParsingException("Could not get name");
132+
return getTextFromObject(title)
133+
.or(() -> Optional.ofNullable(!title.has("runs") ? "" : null))
134+
.orElseThrow(() -> new ParsingException("Could not get name"));
144135
}
145136

146137
@Override
@@ -149,45 +140,28 @@ public long getDuration() throws ParsingException {
149140
return -1;
150141
}
151142

152-
String duration = getTextFromObject(videoInfo.getObject("lengthText"));
153-
154-
if (isNullOrEmpty(duration)) {
155-
// Available in playlists for videos
156-
duration = videoInfo.getString("lengthSeconds");
157-
158-
if (isNullOrEmpty(duration)) {
159-
final List<String> timeOverlays = videoInfo.getArray("thumbnailOverlays")
160-
.stream()
161-
.filter(JsonObject.class::isInstance)
162-
.map(JsonObject.class::cast)
163-
.filter(thumbnailOverlay ->
164-
thumbnailOverlay.has("thumbnailOverlayTimeStatusRenderer"))
165-
.map(thumbnailOverlay -> getTextFromObject(
166-
thumbnailOverlay.getObject("thumbnailOverlayTimeStatusRenderer")
167-
.getObject("text")))
143+
return getTextFromObject(videoInfo.getObject("lengthText"))
144+
.or(() -> Optional.ofNullable(videoInfo.getString("lengthSeconds")))
145+
.or(() -> videoInfo.getArray("thumbnailOverlays")
146+
.streamAsJsonObjects()
147+
.map(thumbnailOverlay -> {
148+
final var thumbnailRendererText = thumbnailOverlay
149+
.getObject("thumbnailOverlayTimeStatusRenderer")
150+
.getObject("text");
151+
return getTextFromObject(thumbnailRendererText).orElse(null);
152+
})
168153
.filter(text -> !isNullOrEmpty(text))
169-
.collect(Collectors.toList());
170-
171-
for (final String timeOverlayText : timeOverlays) {
154+
.findFirst())
155+
.map(duration -> {
172156
try {
173-
return YoutubeParsingHelper.parseDurationString(timeOverlayText);
174-
} catch (final ParsingException ex) {
175-
// try next
157+
return YoutubeParsingHelper.parseDurationString(duration);
158+
} catch (final ParsingException e) {
159+
return null;
176160
}
177-
}
178-
}
179-
180-
if (isNullOrEmpty(duration)) {
181-
if (isPremiere()) {
182-
// Premieres can be livestreams, so the duration is not available in this
183-
// case
184-
return -1;
185-
}
186-
187-
throw new ParsingException("Could not get duration");
188-
}
189-
190-
return YoutubeParsingHelper.parseDurationString(duration);
161+
})
162+
// Premieres can be livestreams, so the duration is not available in this case
163+
.or(() -> Optional.ofNullable(isPremiere() ? -1 : null))
164+
.orElseThrow(() -> new ParsingException("Could not get duration"));
191165
}
192166

193167
@Override
@@ -455,7 +429,7 @@ public boolean isShortFormContent() throws ParsingException {
455429
}
456430
}
457431

458-
private boolean isMembersOnly() throws ParsingException {
432+
private boolean isMembersOnly() {
459433
return videoInfo.getArray("badges")
460434
.stream()
461435
.filter(JsonObject.class::isInstance)
@@ -467,7 +441,7 @@ private boolean isMembersOnly() throws ParsingException {
467441

468442
@Nonnull
469443
@Override
470-
public ContentAvailability getContentAvailability() throws ParsingException {
444+
public ContentAvailability getContentAvailability() {
471445
if (isPremiere()) {
472446
return ContentAvailability.UPCOMING;
473447
}

0 commit comments

Comments
 (0)