mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-29 00:10:35 +05:30
[YouTube] Refactor the code to get stream items' view count
This refactoring avoids code duplication as much as possible.
This commit is contained in:
parent
51f9b39953
commit
bd79b921e8
@ -13,6 +13,7 @@ import org.schabi.newpipe.extractor.utils.JsonUtils;
|
|||||||
import org.schabi.newpipe.extractor.utils.Parser;
|
import org.schabi.newpipe.extractor.utils.Parser;
|
||||||
import org.schabi.newpipe.extractor.utils.Utils;
|
import org.schabi.newpipe.extractor.utils.Utils;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
@ -290,20 +291,14 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String viewCount = getTextFromObject(videoInfo.getObject("viewCountText"));
|
// Ignore all exceptions, as the view count can be hidden by creators, and so cannot be
|
||||||
|
// found in this case
|
||||||
|
|
||||||
if (!isNullOrEmpty(viewCount)) {
|
final String viewCountText = getTextFromObject(videoInfo.getObject("viewCountText"));
|
||||||
|
if (!isNullOrEmpty(viewCountText)) {
|
||||||
try {
|
try {
|
||||||
// These approaches are language dependent
|
return getViewCountFromViewCountText(viewCountText, false);
|
||||||
if (viewCount.toLowerCase().contains(NO_VIEWS_LOWERCASE)) {
|
|
||||||
return 0;
|
|
||||||
} else if (viewCount.toLowerCase().contains("recommended")) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Long.parseLong(Utils.removeNonDigitCharacters(viewCount));
|
|
||||||
} catch (final Exception ignored) {
|
} catch (final Exception ignored) {
|
||||||
// Ignore all exceptions, as we can fall back to accessibility data
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,64 +306,71 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
|
|||||||
// livestream (the view count is returned and not the count of people watching currently
|
// livestream (the view count is returned and not the count of people watching currently
|
||||||
// the livestream)
|
// the livestream)
|
||||||
if (getStreamType() != StreamType.LIVE_STREAM) {
|
if (getStreamType() != StreamType.LIVE_STREAM) {
|
||||||
final String videoInfoTitleAccessibilityData = videoInfo.getObject("title")
|
|
||||||
.getObject("accessibility")
|
|
||||||
.getObject("accessibilityData")
|
|
||||||
.getString("label", "");
|
|
||||||
|
|
||||||
if (videoInfoTitleAccessibilityData.toLowerCase().endsWith(NO_VIEWS_LOWERCASE)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Long.parseLong(Utils.removeNonDigitCharacters(
|
return getViewCountFromAccessibilityData();
|
||||||
// This approach is language dependent
|
|
||||||
Parser.matchGroup1(ACCESSIBILITY_DATA_VIEW_COUNT_REGEX,
|
|
||||||
videoInfoTitleAccessibilityData)));
|
|
||||||
} catch (final Exception ignored) {
|
} catch (final Exception ignored) {
|
||||||
// Ignore all exceptions, as the view count can be hidden by creators, and so
|
|
||||||
// cannot be found in this case
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to a short view count, always used for livestreams (see why above)
|
// Fallback to a short view count, always used for livestreams (see why above)
|
||||||
try {
|
if (videoInfo.has("videoInfo")) {
|
||||||
// Returned in playlists, in the form: view count separator upload date
|
// Returned in playlists, in the form: view count separator upload date
|
||||||
if (videoInfo.has("videoInfo")) {
|
try {
|
||||||
final String videoInfoViewCountText = videoInfo.getObject("videoInfo")
|
return getViewCountFromViewCountText(videoInfo.getObject("videoInfo")
|
||||||
.getArray("runs")
|
.getArray("runs")
|
||||||
.getObject(0)
|
.getObject(0)
|
||||||
.getString("text", "");
|
.getString("text", ""), true);
|
||||||
if (videoInfoViewCountText.toLowerCase().contains(NO_VIEWS_LOWERCASE)) {
|
} catch (final Exception ignored) {
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Utils.mixedNumberWordToLong(videoInfoViewCountText);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (videoInfo.has("shortViewCountText")) {
|
||||||
// Returned everywhere but in playlists, used by the website to show view counts
|
// Returned everywhere but in playlists, used by the website to show view counts
|
||||||
if (videoInfo.has("shortViewCountText")) {
|
try {
|
||||||
final String shortVideoViewCountText =
|
final String shortViewCountText =
|
||||||
getTextFromObject(videoInfo.getObject("shortViewCountText"));
|
getTextFromObject(videoInfo.getObject("shortViewCountText"));
|
||||||
if (isNullOrEmpty(shortVideoViewCountText)) {
|
if (!isNullOrEmpty(shortViewCountText)) {
|
||||||
return -1;
|
return getViewCountFromViewCountText(shortViewCountText, true);
|
||||||
}
|
}
|
||||||
|
} catch (final Exception ignored) {
|
||||||
if (shortVideoViewCountText.toLowerCase().contains(NO_VIEWS_LOWERCASE)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Utils.mixedNumberWordToLong(shortVideoViewCountText);
|
|
||||||
}
|
}
|
||||||
} catch (final Exception ignored) {
|
|
||||||
// Ignore all exceptions, as the view count can be hidden by creators, and so cannot be
|
|
||||||
// found in this case
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// No view count extracted: return -1, as the view count can be hidden by creators on videos
|
// No view count extracted: return -1, as the view count can be hidden by creators on videos
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long getViewCountFromViewCountText(@Nonnull final String viewCountText,
|
||||||
|
final boolean isMixedNumber)
|
||||||
|
throws NumberFormatException, ParsingException {
|
||||||
|
// These approaches are language dependent
|
||||||
|
if (viewCountText.toLowerCase().contains(NO_VIEWS_LOWERCASE)) {
|
||||||
|
return 0;
|
||||||
|
} else if (viewCountText.toLowerCase().contains("recommended")) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isMixedNumber ? Utils.mixedNumberWordToLong(viewCountText)
|
||||||
|
: Long.parseLong(Utils.removeNonDigitCharacters(viewCountText));
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getViewCountFromAccessibilityData()
|
||||||
|
throws NumberFormatException, Parser.RegexException {
|
||||||
|
// These approaches are language dependent
|
||||||
|
final String videoInfoTitleAccessibilityData = videoInfo.getObject("title")
|
||||||
|
.getObject("accessibility")
|
||||||
|
.getObject("accessibilityData")
|
||||||
|
.getString("label", "");
|
||||||
|
|
||||||
|
if (videoInfoTitleAccessibilityData.toLowerCase().endsWith(NO_VIEWS_LOWERCASE)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Long.parseLong(Utils.removeNonDigitCharacters(
|
||||||
|
Parser.matchGroup1(ACCESSIBILITY_DATA_VIEW_COUNT_REGEX,
|
||||||
|
videoInfoTitleAccessibilityData)));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getThumbnailUrl() throws ParsingException {
|
public String getThumbnailUrl() throws ParsingException {
|
||||||
return getThumbnailUrlFromInfoItem(videoInfo);
|
return getThumbnailUrlFromInfoItem(videoInfo);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user