Class Utils
- java.lang.Object
-
- org.schabi.newpipe.extractor.utils.Utils
-
public final class Utils extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkUrl(java.lang.String pattern, java.lang.String url)
Check if the url matches the pattern.static java.lang.String
decodeUrlUtf8(java.lang.String url)
Decodes a URL using the UTF-8 character set.static java.lang.String
encodeUrlUtf8(java.lang.String string)
Encodes a string to URL format using the UTF-8 character set.static java.lang.String
followGoogleRedirectIfNeeded(java.lang.String url)
If the provided url is a Google search redirect, then the actual url is extracted from theurl=
query value and returned, otherwise the original url is returned.static java.lang.String
getBaseUrl(java.lang.String url)
static java.lang.String
getQueryValue(java.net.URL url, java.lang.String parameterName)
Get the value of a URL-query by name.static java.lang.String
getStringResultFromRegexArray(java.lang.String input, java.lang.String[] regexes)
Find the result of an array of string regular expressions inside an input on the first group (0
).static java.lang.String
getStringResultFromRegexArray(java.lang.String input, java.lang.String[] regexes, int group)
Find the result of an array of string regular expressions inside an input on a specific group.static java.lang.String
getStringResultFromRegexArray(java.lang.String input, java.util.regex.Pattern[] regexes)
Find the result of an array ofPattern
s inside an input on the first group (0
).static java.lang.String
getStringResultFromRegexArray(java.lang.String input, java.util.regex.Pattern[] regexes, int group)
Find the result of an array ofPattern
s inside an input on a specific group.static boolean
isBlank(java.lang.String string)
static boolean
isHTTP(java.net.URL url)
static boolean
isNullOrEmpty(java.lang.String str)
static boolean
isNullOrEmpty(java.util.Collection<?> collection)
Checks if a collection is null or empty.static <K,V>
booleanisNullOrEmpty(java.util.Map<K,V> map)
Checks if amap
is null or empty.static java.lang.String
join(java.lang.String delimiter, java.lang.String mapJoin, java.util.Map<? extends java.lang.CharSequence,? extends java.lang.CharSequence> elements)
static long
mixedNumberWordToLong(java.lang.String numberWord)
Convert a mixed number word to a long.static java.lang.String
nonEmptyAndNullJoin(java.lang.CharSequence delimiter, java.lang.String... elements)
Concatenate all non-null, non-empty and strings which are not equal to"null"
.static java.lang.String
removeMAndWWWFromUrl(java.lang.String url)
static java.lang.String
removeNonDigitCharacters(java.lang.String toRemove)
Remove all non-digit characters from a string.static java.lang.String
removeUTF8BOM(java.lang.String s)
static java.lang.String
replaceHttpWithHttps(java.lang.String url)
static java.net.URL
stringToURL(java.lang.String url)
Convert a string to aURL object
.
-
-
-
Field Detail
-
HTTP
public static final java.lang.String HTTP
- See Also:
- Constant Field Values
-
HTTPS
public static final java.lang.String HTTPS
- See Also:
- Constant Field Values
-
-
Method Detail
-
encodeUrlUtf8
public static java.lang.String encodeUrlUtf8(java.lang.String string)
Encodes a string to URL format using the UTF-8 character set.- Parameters:
string
- The string to be encoded.- Returns:
- The encoded URL.
-
decodeUrlUtf8
public static java.lang.String decodeUrlUtf8(java.lang.String url)
Decodes a URL using the UTF-8 character set.- Parameters:
url
- The URL to be decoded.- Returns:
- The decoded URL.
-
removeNonDigitCharacters
@Nonnull public static java.lang.String removeNonDigitCharacters(@Nonnull java.lang.String toRemove)
Remove all non-digit characters from a string.Examples:
- 1 234 567 views -> 1234567
- $31,133.124 -> 31133124
- Parameters:
toRemove
- string to remove non-digit chars- Returns:
- a string that contains only digits
-
mixedNumberWordToLong
public static long mixedNumberWordToLong(java.lang.String numberWord) throws java.lang.NumberFormatException, ParsingException
Convert a mixed number word to a long.Examples:
- 123 -> 123
- 1.23K -> 1230
- 1.23M -> 1230000
- Parameters:
numberWord
- string to be converted to a long- Returns:
- a long
- Throws:
java.lang.NumberFormatException
ParsingException
-
checkUrl
public static void checkUrl(java.lang.String pattern, java.lang.String url) throws ParsingException
Check if the url matches the pattern.- Parameters:
pattern
- the pattern that will be used to check the urlurl
- the url to be tested- Throws:
ParsingException
-
replaceHttpWithHttps
public static java.lang.String replaceHttpWithHttps(java.lang.String url)
-
getQueryValue
@Nullable public static java.lang.String getQueryValue(@Nonnull java.net.URL url, java.lang.String parameterName)
Get the value of a URL-query by name.If an url-query is give multiple times, only the value of the first query is returned.
- Parameters:
url
- the url to be usedparameterName
- the pattern that will be used to check the url- Returns:
- a string that contains the value of the query parameter or
null
if nothing was found
-
stringToURL
@Nonnull public static java.net.URL stringToURL(java.lang.String url) throws java.net.MalformedURLException
Convert a string to aURL object
.Defaults to HTTP if no protocol is given.
- Parameters:
url
- the string to be converted to a URL-Object- Returns:
- a
URL object
containing the url - Throws:
java.net.MalformedURLException
-
isHTTP
public static boolean isHTTP(@Nonnull java.net.URL url)
-
removeMAndWWWFromUrl
public static java.lang.String removeMAndWWWFromUrl(java.lang.String url)
-
removeUTF8BOM
@Nonnull public static java.lang.String removeUTF8BOM(@Nonnull java.lang.String s)
-
getBaseUrl
@Nonnull public static java.lang.String getBaseUrl(java.lang.String url) throws ParsingException
- Throws:
ParsingException
-
followGoogleRedirectIfNeeded
public static java.lang.String followGoogleRedirectIfNeeded(java.lang.String url)
If the provided url is a Google search redirect, then the actual url is extracted from theurl=
query value and returned, otherwise the original url is returned.- Parameters:
url
- the url which can possibly be a Google search redirect- Returns:
- an url with no Google search redirects
-
isNullOrEmpty
public static boolean isNullOrEmpty(java.lang.String str)
-
isNullOrEmpty
public static boolean isNullOrEmpty(java.util.Collection<?> collection)
Checks if a collection is null or empty.This method can be also used for
JsonArray
s.- Parameters:
collection
- the collection on which check if it's null or empty- Returns:
- whether the collection is null or empty
-
isNullOrEmpty
public static <K,V> boolean isNullOrEmpty(java.util.Map<K,V> map)
Checks if amap
is null or empty.This method can be also used for
JsonObject
s.- Parameters:
map
- themap
on which check if it's null or empty- Returns:
- whether the
map
is null or empty
-
isBlank
public static boolean isBlank(java.lang.String string)
-
join
@Nonnull public static java.lang.String join(java.lang.String delimiter, java.lang.String mapJoin, @Nonnull java.util.Map<? extends java.lang.CharSequence,? extends java.lang.CharSequence> elements)
-
nonEmptyAndNullJoin
@Nonnull public static java.lang.String nonEmptyAndNullJoin(java.lang.CharSequence delimiter, java.lang.String... elements)
Concatenate all non-null, non-empty and strings which are not equal to"null"
.
-
getStringResultFromRegexArray
@Nonnull public static java.lang.String getStringResultFromRegexArray(@Nonnull java.lang.String input, @Nonnull java.lang.String[] regexes) throws Parser.RegexException
Find the result of an array of string regular expressions inside an input on the first group (0
).- Parameters:
input
- the input on which using the regular expressionsregexes
- the string array of regular expressions- Returns:
- the result
- Throws:
Parser.RegexException
- if none of the patterns match the input
-
getStringResultFromRegexArray
@Nonnull public static java.lang.String getStringResultFromRegexArray(@Nonnull java.lang.String input, @Nonnull java.util.regex.Pattern[] regexes) throws Parser.RegexException
Find the result of an array ofPattern
s inside an input on the first group (0
).- Parameters:
input
- the input on which using the regular expressionsregexes
- thePattern
array- Returns:
- the result
- Throws:
Parser.RegexException
- if none of the patterns match the input
-
getStringResultFromRegexArray
@Nonnull public static java.lang.String getStringResultFromRegexArray(@Nonnull java.lang.String input, @Nonnull java.lang.String[] regexes, int group) throws Parser.RegexException
Find the result of an array of string regular expressions inside an input on a specific group.- Parameters:
input
- the input on which using the regular expressionsregexes
- the string array of regular expressionsgroup
- the group to match- Returns:
- the result
- Throws:
Parser.RegexException
- if none of the patterns match the input, or at least in the specified group
-
getStringResultFromRegexArray
@Nonnull public static java.lang.String getStringResultFromRegexArray(@Nonnull java.lang.String input, @Nonnull java.util.regex.Pattern[] regexes, int group) throws Parser.RegexException
Find the result of an array ofPattern
s inside an input on a specific group.- Parameters:
input
- the input on which using the regular expressionsregexes
- thePattern
arraygroup
- the group to match- Returns:
- the result
- Throws:
Parser.RegexException
- if none of the patterns match the input, or at least in the specified group
-
-