public final class YoutubeParsingHelper extends Object
Modifier and Type | Field and Description |
---|---|
static String |
CONTENT_CHECK_OK
A parameter sent by official clients named
contentCheckOk . |
static String |
CPN
A parameter sent by official clients named
contentPlaybackNonce . |
static String |
DISABLE_PRETTY_PRINT_PARAMETER
A parameter to disable pretty-printed response of InnerTube requests, to reduce response
sizes.
|
static String |
RACY_CHECK_OK
A parameter which may be send by official clients named
racyCheckOk . |
static String |
VIDEO_ID |
static String |
YOUTUBEI_V1_GAPIS_URL
The base URL of requests of non-web clients to the InnerTube internal API.
|
static String |
YOUTUBEI_V1_URL
The base URL of requests of the
WEB clients to the InnerTube internal API. |
Modifier and Type | Method and Description |
---|---|
static void |
addClientInfoHeaders(Map<String,List<String>> headers)
Add the
X-YouTube-Client-Name , X-YouTube-Client-Version ,
Origin , and Referer headers. |
static void |
addCookieHeader(Map<String,List<String>> headers)
Add the
CONSENT cookie to prevent redirect to consent.youtube.com |
static void |
addYouTubeHeaders(Map<String,List<String>> headers)
Add required headers and cookies to an existing headers Map.
|
static boolean |
areHardcodedClientVersionAndKeyValid() |
static byte[] |
createDesktopPlayerBody(Localization localization,
ContentCountry contentCountry,
String videoId,
String sts,
boolean isTvHtml5DesktopJsonBuilder,
String contentPlaybackNonce) |
static void |
defaultAlertsCheck(com.grack.nanojson.JsonObject initialData)
Shared alert detection function, multiple endpoints return the error similarly structured.
|
static String |
extractCachedUrlIfNeeded(String url)
Sometimes, YouTube provides URLs which use Google's cache.
|
static String |
extractCookieValue(String cookieName,
Response response) |
static PlaylistInfo.PlaylistType |
extractPlaylistTypeFromPlaylistId(String playlistId) |
static PlaylistInfo.PlaylistType |
extractPlaylistTypeFromPlaylistUrl(String playlistUrl) |
static String |
extractVideoIdFromMixId(String playlistId) |
static String |
fixThumbnailUrl(String thumbnailUrl) |
static String |
generateConsentCookie() |
static String |
generateContentPlaybackNonce()
Generate a content playback nonce (also called
cpn ), sent by YouTube clients in
playback requests (and also for some clients, in the player request body). |
static String |
generateTParameter()
Try to generate a
t parameter, sent by mobile clients as a query of the player
request. |
static String |
getAndroidUserAgent(Localization localization)
Get the user-agent string used as the user-agent for InnerTube requests with the Android
client.
|
static String |
getClientVersion()
Get the client version used by YouTube website on InnerTube requests.
|
static String |
getFeedUrlFrom(String channelIdOrUser) |
static String |
getIosUserAgent(Localization localization)
Get the user-agent string used as the user-agent for InnerTube requests with the iOS
client.
|
static com.grack.nanojson.JsonObject |
getJsonAndroidPostResponse(String endpoint,
byte[] body,
Localization localization,
String endPartOfUrlRequest) |
static com.grack.nanojson.JsonObject |
getJsonIosPostResponse(String endpoint,
byte[] body,
Localization localization,
String endPartOfUrlRequest) |
static com.grack.nanojson.JsonObject |
getJsonPostResponse(String endpoint,
byte[] body,
Localization localization) |
static String |
getKey()
Get the internal API key used by YouTube website on InnerTube requests.
|
static List<MetaInfo> |
getMetaInfo(com.grack.nanojson.JsonArray contents) |
static String |
getTextAtKey(com.grack.nanojson.JsonObject jsonObject,
String theKey) |
static String |
getTextFromObject(com.grack.nanojson.JsonObject textObject) |
static String |
getTextFromObject(com.grack.nanojson.JsonObject textObject,
boolean html)
Get the text from a JSON object that has either a
simpleText or a runs
array. |
static String |
getThumbnailUrlFromInfoItem(com.grack.nanojson.JsonObject infoItem) |
static String |
getUrlFromNavigationEndpoint(com.grack.nanojson.JsonObject navigationEndpoint) |
static String |
getValidJsonResponseBody(Response response) |
static String[] |
getYoutubeMusicKey() |
static boolean |
isHardcodedYoutubeMusicKeyValid() |
static boolean |
isHooktubeURL(URL url) |
static boolean |
isInvidioURL(URL url) |
static boolean |
isVerified(com.grack.nanojson.JsonArray badges) |
static boolean |
isY2ubeURL(URL url) |
static boolean |
isYoutubeChannelMixId(String playlistId)
Checks if the given playlist id is a YouTube Channel Mix (auto-generated playlist)
Ids from a YouTube channel Mix start with "RDCM"
|
static boolean |
isYoutubeGenreMixId(String playlistId)
Checks if the given playlist id is a YouTube Genre Mix (auto-generated playlist)
Ids from a YouTube Genre Mix start with "RDGMEM"
|
static boolean |
isYoutubeMixId(String playlistId)
Checks if the given playlist id is a YouTube Mix (auto-generated playlist)
Ids from a YouTube Mix start with "RD"
|
static boolean |
isYoutubeMusicMixId(String playlistId)
Checks if the given playlist id is a YouTube Music Mix (auto-generated playlist)
Ids from a YouTube Music Mix start with "RDAMVM" or "RDCLAK"
|
static boolean |
isYoutubeMyMixId(String playlistId)
Checks if the given playlist id is a YouTube My Mix (auto-generated playlist)
Ids from a YouTube My Mix start with "RDMM"
|
static boolean |
isYoutubeServiceURL(URL url) |
static boolean |
isYoutubeURL(URL url) |
static OffsetDateTime |
parseDateFrom(String textualUploadDate) |
static int |
parseDurationString(String input)
Parses the duration string of the video expecting ":" or "." as separators
|
static com.grack.nanojson.JsonBuilder<com.grack.nanojson.JsonObject> |
prepareAndroidMobileJsonBuilder(Localization localization,
ContentCountry contentCountry) |
static com.grack.nanojson.JsonBuilder<com.grack.nanojson.JsonObject> |
prepareDesktopJsonBuilder(Localization localization,
ContentCountry contentCountry) |
static com.grack.nanojson.JsonBuilder<com.grack.nanojson.JsonObject> |
prepareIosMobileJsonBuilder(Localization localization,
ContentCountry contentCountry) |
static com.grack.nanojson.JsonBuilder<com.grack.nanojson.JsonObject> |
prepareTvHtml5EmbedJsonBuilder(Localization localization,
ContentCountry contentCountry,
String videoId) |
static void |
resetClientVersionAndKey()
Only used in tests.
|
static void |
setNumberGenerator(Random random)
Only used in tests.
|
static String |
unescapeDocument(String doc) |
public static final String YOUTUBEI_V1_URL
WEB
clients to the InnerTube internal API.public static final String YOUTUBEI_V1_GAPIS_URL
public static final String DISABLE_PRETTY_PRINT_PARAMETER
Sent in query parameters of the requests, after the API key.
public static final String CPN
contentPlaybackNonce
.
It is sent by official clients on videoplayback requests, and by all clients (except the
WEB
one to the player requests.
It is composed of 16 characters which are generated from
this alphabet
, with the use of strong random
values.
public static final String VIDEO_ID
public static final String CONTENT_CHECK_OK
contentCheckOk
.
Setting it to true
allows us to get streaming data on videos with a warning about
what the sensible content they contain.
public static final String RACY_CHECK_OK
racyCheckOk
.
What this parameter does is not really known, but it seems to be linked to sensitive contents such as age-restricted content.
public static int parseDurationString(@Nonnull String input) throws ParsingException, NumberFormatException
ParsingException
- when more than 3 separators are foundNumberFormatException
public static OffsetDateTime parseDateFrom(String textualUploadDate) throws ParsingException
ParsingException
public static boolean isYoutubeMixId(@Nonnull String playlistId)
playlistId
- the playlist idpublic static boolean isYoutubeMyMixId(@Nonnull String playlistId)
playlistId
- the playlist idpublic static boolean isYoutubeMusicMixId(@Nonnull String playlistId)
playlistId
- the playlist idpublic static boolean isYoutubeChannelMixId(@Nonnull String playlistId)
public static boolean isYoutubeGenreMixId(@Nonnull String playlistId)
@Nonnull public static String extractVideoIdFromMixId(String playlistId) throws ParsingException
playlistId
- the playlist id to parsePlaylistInfo.PlaylistType
extracted from the playlistId (mix playlist
types included)ParsingException
- if the playlistId is null or empty, if the playlistId is not a mix,
if it is a mix but it's not based on a specific stream (this is the
case for channel or genre mixes)@Nonnull public static PlaylistInfo.PlaylistType extractPlaylistTypeFromPlaylistId(String playlistId) throws ParsingException
playlistId
- the playlist id to parsePlaylistInfo.PlaylistType
extracted from the playlistId (mix playlist
types included)ParsingException
- if the playlistId is null or emptypublic static PlaylistInfo.PlaylistType extractPlaylistTypeFromPlaylistUrl(String playlistUrl) throws ParsingException
playlistUrl
- the playlist url to parsePlaylistInfo.PlaylistType
extracted from the playlistUrl's list param
(mix playlist types included)ParsingException
- if the playlistUrl is malformed, if has no list param or if the list
param is emptypublic static boolean areHardcodedClientVersionAndKeyValid() throws IOException, ExtractionException
IOException
ExtractionException
public static String getClientVersion() throws IOException, ExtractionException
IOException
ExtractionException
public static String getKey() throws IOException, ExtractionException
IOException
ExtractionException
public static void resetClientVersionAndKey()
Only used in tests.
Quick-and-dirty solution to reset global state in between test classes.
This is needed for the mocks because in order to reach that state a network request has to be made. If the global state is not reset and the RecordingDownloader is used, then only the first test class has that request recorded. Meaning running the other tests with mocks will fail, because the mock is missing.
public static void setNumberGenerator(Random random)
Only used in tests.
public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException, ReCaptchaException
IOException
ReCaptchaException
public static String[] getYoutubeMusicKey() throws IOException, ReCaptchaException, Parser.RegexException
@Nullable public static String getUrlFromNavigationEndpoint(@Nonnull com.grack.nanojson.JsonObject navigationEndpoint) throws ParsingException
ParsingException
@Nullable public static String getTextFromObject(com.grack.nanojson.JsonObject textObject, boolean html) throws ParsingException
simpleText
or a runs
array.textObject
- JSON object to get the text fromhtml
- whether to return HTML, by parsing the navigationEndpoint
null
ParsingException
@Nullable public static String getTextFromObject(com.grack.nanojson.JsonObject textObject) throws ParsingException
ParsingException
@Nullable public static String getTextAtKey(@Nonnull com.grack.nanojson.JsonObject jsonObject, String theKey) throws ParsingException
ParsingException
public static String getThumbnailUrlFromInfoItem(com.grack.nanojson.JsonObject infoItem) throws ParsingException
ParsingException
@Nonnull public static String getValidJsonResponseBody(@Nonnull Response response) throws ParsingException, MalformedURLException
public static com.grack.nanojson.JsonObject getJsonPostResponse(String endpoint, byte[] body, Localization localization) throws IOException, ExtractionException
IOException
ExtractionException
public static com.grack.nanojson.JsonObject getJsonAndroidPostResponse(String endpoint, byte[] body, @Nonnull Localization localization, @Nullable String endPartOfUrlRequest) throws IOException, ExtractionException
IOException
ExtractionException
public static com.grack.nanojson.JsonObject getJsonIosPostResponse(String endpoint, byte[] body, @Nonnull Localization localization, @Nullable String endPartOfUrlRequest) throws IOException, ExtractionException
IOException
ExtractionException
@Nonnull public static com.grack.nanojson.JsonBuilder<com.grack.nanojson.JsonObject> prepareDesktopJsonBuilder(@Nonnull Localization localization, @Nonnull ContentCountry contentCountry) throws IOException, ExtractionException
IOException
ExtractionException
@Nonnull public static com.grack.nanojson.JsonBuilder<com.grack.nanojson.JsonObject> prepareAndroidMobileJsonBuilder(@Nonnull Localization localization, @Nonnull ContentCountry contentCountry)
@Nonnull public static com.grack.nanojson.JsonBuilder<com.grack.nanojson.JsonObject> prepareIosMobileJsonBuilder(@Nonnull Localization localization, @Nonnull ContentCountry contentCountry)
@Nonnull public static com.grack.nanojson.JsonBuilder<com.grack.nanojson.JsonObject> prepareTvHtml5EmbedJsonBuilder(@Nonnull Localization localization, @Nonnull ContentCountry contentCountry, @Nonnull String videoId)
@Nonnull public static byte[] createDesktopPlayerBody(@Nonnull Localization localization, @Nonnull ContentCountry contentCountry, @Nonnull String videoId, @Nonnull String sts, boolean isTvHtml5DesktopJsonBuilder, @Nonnull String contentPlaybackNonce) throws IOException, ExtractionException
IOException
ExtractionException
@Nonnull public static String getAndroidUserAgent(@Nullable Localization localization)
Localization
provided is null
, fallbacks to
the default one
.localization
- the Localization
to set in the user-agentLocalization
provided@Nonnull public static String getIosUserAgent(@Nullable Localization localization)
Localization
provided is null
, fallbacks to
the default one
.localization
- the Localization
to set in the user-agentLocalization
providedpublic static void addYouTubeHeaders(Map<String,List<String>> headers) throws IOException, ExtractionException
IOException
ExtractionException
addClientInfoHeaders(Map)
,
addCookieHeader(Map)
public static void addClientInfoHeaders(@Nonnull Map<String,List<String>> headers) throws IOException, ExtractionException
X-YouTube-Client-Name
, X-YouTube-Client-Version
,
Origin
, and Referer
headers.headers
- The headers which should be completedIOException
ExtractionException
public static void addCookieHeader(@Nonnull Map<String,List<String>> headers)
CONSENT
cookie to prevent redirect to consent.youtube.com
headers
- the headers which should be completedCONSENT_COOKIE
public static String extractCookieValue(String cookieName, @Nonnull Response response)
public static void defaultAlertsCheck(@Nonnull com.grack.nanojson.JsonObject initialData) throws ParsingException
Will check if the object has an alert of the type "ERROR".
initialData
- the object which will be checked if an alert is presentContentNotAvailableException
- if an alert is detectedParsingException
@Nonnull public static List<MetaInfo> getMetaInfo(@Nonnull com.grack.nanojson.JsonArray contents) throws ParsingException
ParsingException
public static String extractCachedUrlIfNeeded(String url)
https://webcache.googleusercontent.com/search?q=cache:CACHED_URL
url
- the URL which might refer to the Google's webcachepublic static boolean isVerified(com.grack.nanojson.JsonArray badges)
@Nonnull public static String generateContentPlaybackNonce()
cpn
), sent by YouTube clients in
playback requests (and also for some clients, in the player request body).@Nonnull public static String generateTParameter()
t
parameter, sent by mobile clients as a query of the player
request.
Some researches needs to be done to know how this parameter, unique at each request, is generated.