Class Utils

java.lang.Object
org.schabi.newpipe.extractor.utils.Utils

public final class Utils extends Object
  • Field Details

  • Method Details

    • encodeUrlUtf8

      public static String encodeUrlUtf8(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 String decodeUrlUtf8(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 String removeNonDigitCharacters(@Nonnull 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(String numberWord) throws 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:
      NumberFormatException
      ParsingException
    • checkUrl

      public static void checkUrl(String pattern, String url) throws ParsingException
      Check if the url matches the pattern.
      Parameters:
      pattern - the pattern that will be used to check the url
      url - the url to be tested
      Throws:
      ParsingException
    • checkUrl

      public static void checkUrl(Pattern pattern, String url) throws ParsingException
      Check if the url matches the pattern.
      Parameters:
      pattern - the pattern that will be used to check the url
      url - the url to be tested
      Throws:
      ParsingException
    • replaceHttpWithHttps

      public static String replaceHttpWithHttps(String url)
    • getQueryValue

      @Nullable public static String getQueryValue(@Nonnull URL url, 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 used
      parameterName - 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 URL stringToURL(String url) throws MalformedURLException
      Convert a string to a URL 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:
      MalformedURLException
    • isHTTP

      public static boolean isHTTP(@Nonnull URL url)
    • removeMAndWWWFromUrl

      public static String removeMAndWWWFromUrl(String url)
    • removeUTF8BOM

      @Nonnull public static String removeUTF8BOM(@Nonnull String s)
    • getBaseUrl

      @Nonnull public static String getBaseUrl(String url) throws ParsingException
      Throws:
      ParsingException
    • followGoogleRedirectIfNeeded

      public static String followGoogleRedirectIfNeeded(String url)
      If the provided url is a Google search redirect, then the actual url is extracted from the url= 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(String str)
    • isNullOrEmpty

      public static boolean isNullOrEmpty(Collection<?> collection)
      Checks if a collection is null or empty.

      This method can be also used for JsonArrays.

      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(Map<K,V> map)
      Checks if a map is null or empty.

      This method can be also used for JsonObjects.

      Parameters:
      map - the map on which check if it's null or empty
      Returns:
      whether the map is null or empty
    • isBlank

      public static boolean isBlank(String string)
    • join

      @Nonnull public static String join(String delimiter, String mapJoin, @Nonnull Map<? extends CharSequence,? extends CharSequence> elements)
    • nonEmptyAndNullJoin

      @Nonnull public static String nonEmptyAndNullJoin(CharSequence delimiter, String... elements)
      Concatenate all non-null, non-empty and strings which are not equal to "null".
    • getStringResultFromRegexArray

      @Nonnull public static String getStringResultFromRegexArray(@Nonnull String input, @Nonnull 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 expressions
      regexes - the string array of regular expressions
      Returns:
      the result
      Throws:
      Parser.RegexException - if none of the patterns match the input
    • getStringResultFromRegexArray

      @Nonnull public static String getStringResultFromRegexArray(@Nonnull String input, @Nonnull Pattern[] regexes) throws Parser.RegexException
      Find the result of an array of Patterns inside an input on the first group (0).
      Parameters:
      input - the input on which using the regular expressions
      regexes - the Pattern array
      Returns:
      the result
      Throws:
      Parser.RegexException - if none of the patterns match the input
    • getStringResultFromRegexArray

      @Nonnull public static String getStringResultFromRegexArray(@Nonnull String input, @Nonnull 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 expressions
      regexes - the string array of regular expressions
      group - 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 String getStringResultFromRegexArray(@Nonnull String input, @Nonnull Pattern[] regexes, int group) throws Parser.RegexException
      Find the result of an array of Patterns inside an input on a specific group.
      Parameters:
      input - the input on which using the regular expressions
      regexes - the Pattern array
      group - 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