Skip to content

Commit c8c9af1

Browse files
committed
[YouTube] Fix mm:ss timestamps being replaced with text in descriptions
1 parent 8a415ca commit c8c9af1

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

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

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

33
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
4+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeServiceURL;
5+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isYoutubeURL;
46
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
57

68
import com.grack.nanojson.JsonObject;
79

810
import org.jsoup.nodes.Entities;
911

12+
import java.net.MalformedURLException;
13+
import java.net.URL;
1014
import java.util.ArrayList;
1115
import java.util.Collections;
1216
import java.util.Comparator;
@@ -242,23 +246,35 @@ private static void addAllCommandRuns(
242246
return;
243247
}
244248

249+
boolean isYoutubeUrl;
250+
try {
251+
final URL parsedUrl = new URL(url);
252+
isYoutubeUrl = isYoutubeURL(parsedUrl) || isYoutubeServiceURL(parsedUrl);
253+
} catch (final MalformedURLException ignored) {
254+
// this should never happen, but just in case, assume this is a generic URL
255+
isYoutubeUrl = false;
256+
}
257+
245258
final String open = "<a href=\"" + Entities.escape(url) + "\">";
246-
final Function<String, String> transformContent = getTransformContentFun(run);
259+
final Function<String, String> transformContent = getTransformContentFun(
260+
run, isYoutubeUrl);
247261

248262
openers.add(new Run(open, LINK_CLOSE, startIndex, transformContent));
249263
closers.add(new Run(open, LINK_CLOSE, startIndex + length, transformContent));
250264
});
251265
}
252266

253-
private static Function<String, String> getTransformContentFun(final JsonObject run) {
267+
private static Function<String, String> getTransformContentFun(final JsonObject run,
268+
final boolean isYoutube) {
254269
final String accessibilityLabel = run.getObject("onTapOptions")
255270
.getObject("accessibilityInfo")
256271
.getString("accessibilityLabel", "")
257272
// accessibility labels are e.g. "Instagram Channel Link: instagram_profile_name"
258273
.replaceFirst(" Channel Link", "");
259274

260275
final Function<String, String> transformContent;
261-
if (accessibilityLabel.isEmpty() || accessibilityLabel.startsWith("YouTube: ")) {
276+
if (isYoutube
277+
|| accessibilityLabel.isEmpty() || accessibilityLabel.startsWith("YouTube: ")) {
262278
// if there is no accessibility label, or the link points to YouTube, cleanup the link
263279
// text, see LINK_CONTENT_CLEANER_REGEX's documentation for more details
264280
transformContent = (content) -> {

0 commit comments

Comments
 (0)