|
1 | 1 | package org.schabi.newpipe.extractor.services.youtube; |
2 | 2 |
|
3 | 3 | 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; |
4 | 6 | import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; |
5 | 7 |
|
6 | 8 | import com.grack.nanojson.JsonObject; |
7 | 9 |
|
8 | 10 | import org.jsoup.nodes.Entities; |
9 | 11 |
|
| 12 | +import java.net.MalformedURLException; |
| 13 | +import java.net.URL; |
10 | 14 | import java.util.ArrayList; |
11 | 15 | import java.util.Collections; |
12 | 16 | import java.util.Comparator; |
@@ -242,23 +246,35 @@ private static void addAllCommandRuns( |
242 | 246 | return; |
243 | 247 | } |
244 | 248 |
|
| 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 | + |
245 | 258 | 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); |
247 | 261 |
|
248 | 262 | openers.add(new Run(open, LINK_CLOSE, startIndex, transformContent)); |
249 | 263 | closers.add(new Run(open, LINK_CLOSE, startIndex + length, transformContent)); |
250 | 264 | }); |
251 | 265 | } |
252 | 266 |
|
253 | | - private static Function<String, String> getTransformContentFun(final JsonObject run) { |
| 267 | + private static Function<String, String> getTransformContentFun(final JsonObject run, |
| 268 | + final boolean isYoutube) { |
254 | 269 | final String accessibilityLabel = run.getObject("onTapOptions") |
255 | 270 | .getObject("accessibilityInfo") |
256 | 271 | .getString("accessibilityLabel", "") |
257 | 272 | // accessibility labels are e.g. "Instagram Channel Link: instagram_profile_name" |
258 | 273 | .replaceFirst(" Channel Link", ""); |
259 | 274 |
|
260 | 275 | final Function<String, String> transformContent; |
261 | | - if (accessibilityLabel.isEmpty() || accessibilityLabel.startsWith("YouTube: ")) { |
| 276 | + if (isYoutube |
| 277 | + || accessibilityLabel.isEmpty() || accessibilityLabel.startsWith("YouTube: ")) { |
262 | 278 | // if there is no accessibility label, or the link points to YouTube, cleanup the link |
263 | 279 | // text, see LINK_CONTENT_CLEANER_REGEX's documentation for more details |
264 | 280 | transformContent = (content) -> { |
|
0 commit comments