Better post type detection

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-07-21 16:29:50 +02:00
parent a784ca86f9
commit f6800b818d
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58

View File

@ -196,8 +196,8 @@ public class ParsePost {
Uri uri = Uri.parse(url); Uri uri = Uri.parse(url);
String path = uri.getPath(); String path = uri.getPath();
if (!data.has(JSONUtils.PREVIEW_KEY) && previews.isEmpty()) { if (!data.getJSONObject("post").has("thumbnail_url") && previews.isEmpty()) {
if (!data.getJSONObject("post").isNull("body")) { if (!data.getJSONObject("post").isNull("body") && url.equals("")) {
//Text post //Text post
int postType = Post.TEXT_TYPE; int postType = Post.TEXT_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
@ -229,15 +229,15 @@ public class ParsePost {
String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString();
String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY);
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, postTimeMillis, title, permalink, score, postType, voteType, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, postTimeMillis, title, permalink, score, postType, voteType,
nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort);
post.setVideoUrl(videoUrl); post.setVideoUrl(videoUrl);
post.setVideoDownloadUrl(videoDownloadUrl); post.setVideoDownloadUrl(videoDownloadUrl);
} else { } else if (!url.equals("")) {
//No preview link post //No preview link post
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
postTimeMillis, title, url, permalink, score, postTimeMillis, title, url, permalink, score,
postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort);
if (data.isNull(JSONUtils.SELFTEXT_KEY)) { if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
@ -269,12 +269,22 @@ public class ParsePost {
post.setStreamableShortCode(shortCode); post.setStreamableShortCode(shortCode);
} }
} }
} else {
int postType = Post.TEXT_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
postTimeMillis, title, permalink, score,
postType, voteType, nComments, upvoteRatio, nsfw,
locked, saved, distinguished, suggestedSort);
String body = "";
post.setSelfText(body);
post.setSelfTextPlain(body);
post.setSelfTextPlainTrimmed(body.trim());
} }
} }
} }
} else { } else {
if (previews.isEmpty()) { if (previews.isEmpty()) {
if (data.has(JSONUtils.PREVIEW_KEY)) { if (data.getJSONObject("post").has("thumbnail_url")) {
JSONObject images = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0); JSONObject images = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0);
String previewUrl = images.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); String previewUrl = images.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
int previewWidth = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY); int previewWidth = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY);
@ -300,49 +310,19 @@ public class ParsePost {
String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString();
String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY); String videoDownloadUrl = redditVideoObject.getString(JSONUtils.FALLBACK_URL_KEY);
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, postTimeMillis, title, permalink, score, postType, voteType, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull, postTimeMillis, title, permalink, score, postType, voteType,
nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort); nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort);
post.setPreviews(previews); post.setPreviews(previews);
post.setVideoUrl(videoUrl); post.setVideoUrl(videoUrl);
post.setVideoDownloadUrl(videoDownloadUrl); post.setVideoDownloadUrl(videoDownloadUrl);
} else if (data.has(JSONUtils.PREVIEW_KEY)) { } else if (data.getJSONObject("post").has("thumbnail_url")) {
if (data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) { if (path.endsWith(".jpg") || path.endsWith(".png") || path.endsWith(".jpeg") || path.endsWith(".webp")) {
int postType = Post.VIDEO_TYPE;
String authority = uri.getAuthority();
// The hls stream inside REDDIT_VIDEO_PREVIEW_KEY can sometimes lack an audio track
if (authority.contains("imgur.com") && (path.endsWith(".gifv") || path.endsWith(".mp4"))) {
if (path.endsWith(".gifv")) {
url = url.substring(0, url.length() - 5) + ".mp4";
}
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,postTimeMillis, title, permalink, score, postType, voteType,
nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort);
post.setPreviews(previews);
post.setVideoUrl(url);
post.setVideoDownloadUrl(url);
post.setIsImgur(true);
} else {
//Gif video post (HLS)
String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY)
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.HLS_URL_KEY)).toString();
String videoDownloadUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY)
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.FALLBACK_URL_KEY);
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, postTimeMillis, title, permalink, score, postType, voteType,
nComments, upvoteRatio, nsfw, locked, saved, distinguished, suggestedSort);
post.setPreviews(previews);
post.setVideoUrl(videoUrl);
post.setVideoDownloadUrl(videoDownloadUrl);
}
} else {
if (path.endsWith(".jpg") || path.endsWith(".png") || path.endsWith(".jpeg")|| path.endsWith(".webp")) {
//Image post //Image post
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
authorFull,postTimeMillis, title, url, permalink, score, authorFull, postTimeMillis, title, url, permalink, score,
postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved,
distinguished, suggestedSort); distinguished, suggestedSort);
@ -353,7 +333,7 @@ public class ParsePost {
} else if (path.endsWith(".gif")) { } else if (path.endsWith(".gif")) {
//Gif post //Gif post
int postType = Post.GIF_TYPE; int postType = Post.GIF_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
postTimeMillis, title, url, permalink, score, postTimeMillis, title, url, permalink, score,
postType, voteType, nComments, upvoteRatio, postType, voteType, nComments, upvoteRatio,
nsfw, locked, saved, nsfw, locked, saved,
@ -369,7 +349,7 @@ public class ParsePost {
url = url.substring(0, url.length() - 5) + ".mp4"; url = url.substring(0, url.length() - 5) + ".mp4";
} }
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
postTimeMillis, title, url, permalink, score, postTimeMillis, title, url, permalink, score,
postType, voteType, nComments, upvoteRatio, postType, voteType, nComments, upvoteRatio,
nsfw, locked, saved, nsfw, locked, saved,
@ -382,30 +362,18 @@ public class ParsePost {
//Video post //Video post
int postType = Post.VIDEO_TYPE; int postType = Post.VIDEO_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
postTimeMillis, title, url, permalink, score, postTimeMillis, title, url, permalink, score,
postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved,
distinguished, suggestedSort); distinguished, suggestedSort);
post.setPreviews(previews); post.setPreviews(previews);
post.setVideoUrl(url); post.setVideoUrl(url);
post.setVideoDownloadUrl(url); post.setVideoDownloadUrl(url);
} else {
if (url.contains(permalink)) {
//Text post but with a preview
int postType = Post.TEXT_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull,
postTimeMillis, title, permalink, score,
postType, voteType, nComments, upvoteRatio, nsfw, locked, saved,
distinguished, suggestedSort);
//Need attention
post.setPreviews(previews);
} else { } else {
//Link post //Link post
int postType = Post.LINK_TYPE; int postType = Post.LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,authorFull, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
postTimeMillis, title, url, permalink, score, postTimeMillis, title, url, permalink, score,
postType, voteType, nComments, upvoteRatio, nsfw, locked, saved, postType, voteType, nComments, upvoteRatio, nsfw, locked, saved,
distinguished, suggestedSort); distinguished, suggestedSort);
@ -441,8 +409,6 @@ public class ParsePost {
} }
} }
} }
}
}
} else { } else {
if (path.endsWith(".jpg") || path.endsWith(".png") || path.endsWith(".jpeg")) { if (path.endsWith(".jpg") || path.endsWith(".png") || path.endsWith(".jpeg")) {
//Image post //Image post