|
74 | 74 | import java.util.Optional; |
75 | 75 | import java.util.Random; |
76 | 76 | import java.util.Set; |
| 77 | +import java.util.function.Predicate; |
77 | 78 | import java.util.regex.Pattern; |
78 | 79 | import java.util.stream.Collectors; |
79 | 80 | import java.util.stream.Stream; |
@@ -197,6 +198,8 @@ private YoutubeParsingHelper() { |
197 | 198 |
|
198 | 199 | private static boolean consentAccepted = false; |
199 | 200 |
|
| 201 | + private static final Predicate<String> STRING_PREDICATE = text -> !text.isEmpty(); |
| 202 | + |
200 | 203 | public static boolean isGoogleURL(final String url) { |
201 | 204 | final String cachedUrl = extractCachedUrlIfNeeded(url); |
202 | 205 | try { |
@@ -553,8 +556,8 @@ private static String getClientVersionFromServiceTrackingParam( |
553 | 556 | .map(JsonObject.class::cast) |
554 | 557 | .filter(param -> param.getString("key", "") |
555 | 558 | .equals(clientVersionKey)) |
556 | | - .map(param -> param.getString("value")) |
557 | | - .filter(paramValue -> !isNullOrEmpty(paramValue)) |
| 559 | + .map(param -> param.getString("value", "")) |
| 560 | + .filter(STRING_PREDICATE) |
558 | 561 | .findFirst() |
559 | 562 | .orElse(null); |
560 | 563 | } |
@@ -765,7 +768,7 @@ public static Optional<String> getUrlFromNavigationEndpoint( |
765 | 768 | return Optional.ofNullable(metadata.getString("url")) |
766 | 769 | .map(url -> "https://www.youtube.com" + url); |
767 | 770 | }) |
768 | | - .filter(url -> !url.isEmpty()); |
| 771 | + .filter(STRING_PREDICATE); |
769 | 772 | } |
770 | 773 |
|
771 | 774 | @Nonnull |
@@ -836,7 +839,7 @@ public static Optional<String> getTextFromObject(@Nonnull final JsonObject textO |
836 | 839 | } |
837 | 840 | return Optional.of(string); |
838 | 841 | }) |
839 | | - .filter(text -> !text.isEmpty()); |
| 842 | + .filter(STRING_PREDICATE); |
840 | 843 | } |
841 | 844 |
|
842 | 845 | @Nonnull |
@@ -864,7 +867,7 @@ public static Optional<String> getUrlFromObject(@Nonnull final JsonObject textOb |
864 | 867 | public static Optional<String> getTextAtKey(@Nonnull final JsonObject jsonObject, |
865 | 868 | final String theKey) { |
866 | 869 | return Optional.ofNullable(jsonObject.getString(theKey)) |
867 | | - .filter(text -> !text.isEmpty()) |
| 870 | + .filter(STRING_PREDICATE) |
868 | 871 | .or(() -> getTextFromObject(jsonObject.getObject(theKey))); |
869 | 872 | } |
870 | 873 |
|
|
0 commit comments