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);
String path = uri.getPath();
if (!data.has(JSONUtils.PREVIEW_KEY) && previews.isEmpty()) {
if (!data.getJSONObject("post").isNull("body")) {
if (!data.getJSONObject("post").has("thumbnail_url") && previews.isEmpty()) {
if (!data.getJSONObject("post").isNull("body") && url.equals("")) {
//Text post
int postType = Post.TEXT_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
@ -234,7 +234,7 @@ public class ParsePost {
post.setVideoUrl(videoUrl);
post.setVideoDownloadUrl(videoDownloadUrl);
} else {
} else if (!url.equals("")) {
//No preview link post
int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, authorFull,
@ -269,12 +269,22 @@ public class ParsePost {
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 {
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);
String previewUrl = images.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
int previewWidth = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY);
@ -306,37 +316,7 @@ public class ParsePost {
post.setPreviews(previews);
post.setVideoUrl(videoUrl);
post.setVideoDownloadUrl(videoDownloadUrl);
} else if (data.has(JSONUtils.PREVIEW_KEY)) {
if (data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) {
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 {
} else if (data.getJSONObject("post").has("thumbnail_url")) {
if (path.endsWith(".jpg") || path.endsWith(".png") || path.endsWith(".jpeg") || path.endsWith(".webp")) {
//Image post
int postType = Post.IMAGE_TYPE;
@ -389,18 +369,6 @@ public class ParsePost {
post.setPreviews(previews);
post.setVideoUrl(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 {
//Link post
int postType = Post.LINK_TYPE;
@ -441,8 +409,6 @@ public class ParsePost {
}
}
}
}
}
} else {
if (path.endsWith(".jpg") || path.endsWith(".png") || path.endsWith(".jpeg")) {
//Image post