From 3db95163f88d9d9b130dfd3beeb0c8d3e8db15db Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Sun, 20 Jun 2021 15:07:15 -0400 Subject: [PATCH] fix not detecting downloaded video --- .../viewholder/SliderVideoViewHolder.java | 6 +- .../viewholder/feed/FeedVideoViewHolder.java | 6 +- .../fragments/PostViewV2Fragment.java | 6 +- .../repositories/responses/Media.kt | 2 +- .../repositories/responses/VideoVersion.java | 57 ------------------- .../responses/stories/StoryMedia.kt | 4 +- .../instagrabber/utils/DirectItemFactory.kt | 6 +- .../instagrabber/utils/DownloadUtils.java | 12 ++-- .../instagrabber/utils/ResponseBodyUtils.java | 40 +++++++++++-- 9 files changed, 56 insertions(+), 83 deletions(-) delete mode 100644 app/src/main/java/awais/instagrabber/repositories/responses/VideoVersion.java diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/SliderVideoViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/SliderVideoViewHolder.java index 8f6226ef..832554b7 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/SliderVideoViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/SliderVideoViewHolder.java @@ -17,7 +17,7 @@ import awais.instagrabber.customviews.VideoPlayerViewHelper; import awais.instagrabber.databinding.LayoutVideoPlayerWithThumbnailBinding; import awais.instagrabber.fragments.settings.PreferenceKeys; import awais.instagrabber.repositories.responses.Media; -import awais.instagrabber.repositories.responses.VideoVersion; +import awais.instagrabber.repositories.responses.MediaCandidate; import awais.instagrabber.utils.NumberUtils; import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.Utils; @@ -122,9 +122,9 @@ public class SliderVideoViewHolder extends SliderItemViewHolder { }; final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); String videoUrl = null; - final List videoVersions = media.getVideoVersions(); + final List videoVersions = media.getVideoVersions(); if (videoVersions != null && !videoVersions.isEmpty()) { - final VideoVersion videoVersion = videoVersions.get(0); + final MediaCandidate videoVersion = videoVersions.get(0); if (videoVersion != null) { videoUrl = videoVersion.getUrl(); } diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/feed/FeedVideoViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/feed/FeedVideoViewHolder.java index e94af79b..885632c1 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/feed/FeedVideoViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/feed/FeedVideoViewHolder.java @@ -20,7 +20,7 @@ import awais.instagrabber.customviews.VideoPlayerViewHelper; import awais.instagrabber.databinding.ItemFeedVideoBinding; import awais.instagrabber.fragments.settings.PreferenceKeys; import awais.instagrabber.repositories.responses.Media; -import awais.instagrabber.repositories.responses.VideoVersion; +import awais.instagrabber.repositories.responses.MediaCandidate; import awais.instagrabber.utils.NumberUtils; import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.Utils; @@ -85,9 +85,9 @@ public class FeedVideoViewHolder extends FeedItemViewHolder { }; final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); String videoUrl = null; - final List videoVersions = media.getVideoVersions(); + final List videoVersions = media.getVideoVersions(); if (videoVersions != null && !videoVersions.isEmpty()) { - final VideoVersion videoVersion = videoVersions.get(0); + final MediaCandidate videoVersion = videoVersions.get(0); videoUrl = videoVersion.getUrl(); } final VideoPlayerViewHelper videoPlayerViewHelper = new VideoPlayerViewHelper(binding.getRoot().getContext(), diff --git a/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java b/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java index 69c12419..a4f100fd 100644 --- a/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/PostViewV2Fragment.java @@ -94,7 +94,7 @@ import awais.instagrabber.repositories.responses.Caption; import awais.instagrabber.repositories.responses.Location; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.User; -import awais.instagrabber.repositories.responses.VideoVersion; +import awais.instagrabber.repositories.responses.MediaCandidate; import awais.instagrabber.repositories.responses.directmessages.RankedRecipient; import awais.instagrabber.utils.DownloadUtils; import awais.instagrabber.utils.NullSafePair; @@ -1148,9 +1148,9 @@ public class PostViewV2Fragment extends Fragment implements EditTextDialogFragme }; final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); String videoUrl = null; - final List videoVersions = media.getVideoVersions(); + final List videoVersions = media.getVideoVersions(); if (videoVersions != null && !videoVersions.isEmpty()) { - final VideoVersion videoVersion = videoVersions.get(0); + final MediaCandidate videoVersion = videoVersions.get(0); if (videoVersion != null) { videoUrl = videoVersion.getUrl(); } diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/Media.kt b/app/src/main/java/awais/instagrabber/repositories/responses/Media.kt index 481781a1..03d8aba2 100644 --- a/app/src/main/java/awais/instagrabber/repositories/responses/Media.kt +++ b/app/src/main/java/awais/instagrabber/repositories/responses/Media.kt @@ -24,7 +24,7 @@ data class Media( var likeCount: Long = 0, var hasLiked: Boolean = false, val isReelMedia: Boolean = false, - val videoVersions: List? = null, + val videoVersions: List? = null, val hasAudio: Boolean = false, val videoDuration: Double = 0.0, val viewCount: Long = 0, diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/VideoVersion.java b/app/src/main/java/awais/instagrabber/repositories/responses/VideoVersion.java deleted file mode 100644 index b635cd93..00000000 --- a/app/src/main/java/awais/instagrabber/repositories/responses/VideoVersion.java +++ /dev/null @@ -1,57 +0,0 @@ -package awais.instagrabber.repositories.responses; - -import java.io.Serializable; -import java.util.Objects; - -public class VideoVersion implements Serializable { - private final String id; - private final String type; - private final int width; - private final int height; - private final String url; - - public VideoVersion(final String id, final String type, final int width, final int height, final String url) { - this.id = id; - this.type = type; - this.width = width; - this.height = height; - this.url = url; - } - - public String getId() { - return id; - } - - public String getType() { - return type; - } - - public int getWidth() { - return width; - } - - public int getHeight() { - return height; - } - - public String getUrl() { - return url; - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - final VideoVersion that = (VideoVersion) o; - return width == that.width && - height == that.height && - Objects.equals(id, that.id) && - Objects.equals(type, that.type) && - Objects.equals(url, that.url); - } - - @Override - public int hashCode() { - return Objects.hash(id, type, width, height, url); - } -} diff --git a/app/src/main/java/awais/instagrabber/repositories/responses/stories/StoryMedia.kt b/app/src/main/java/awais/instagrabber/repositories/responses/stories/StoryMedia.kt index 68f525bf..4fa49d3d 100644 --- a/app/src/main/java/awais/instagrabber/repositories/responses/stories/StoryMedia.kt +++ b/app/src/main/java/awais/instagrabber/repositories/responses/stories/StoryMedia.kt @@ -4,7 +4,7 @@ import awais.instagrabber.models.enums.MediaItemType import awais.instagrabber.utils.TextUtils import awais.instagrabber.repositories.responses.ImageVersions2 import awais.instagrabber.repositories.responses.User -import awais.instagrabber.repositories.responses.VideoVersion +import awais.instagrabber.repositories.responses.MediaCandidate import java.io.Serializable import java.util.* @@ -20,7 +20,7 @@ data class StoryMedia( val originalHeight: Int = 0, val mediaType: MediaItemType? = null, val isReelMedia: Boolean = false, - val videoVersions: List? = null, + val videoVersions: List? = null, val hasAudio: Boolean = false, val videoDuration: Double = 0.0, val viewCount: Long = 0, diff --git a/app/src/main/java/awais/instagrabber/utils/DirectItemFactory.kt b/app/src/main/java/awais/instagrabber/utils/DirectItemFactory.kt index 7c72583a..a254a0cb 100644 --- a/app/src/main/java/awais/instagrabber/utils/DirectItemFactory.kt +++ b/app/src/main/java/awais/instagrabber/utils/DirectItemFactory.kt @@ -38,11 +38,9 @@ fun createImageOrVideo( isVideo: Boolean ): DirectItem { val imageVersions2 = ImageVersions2(listOf(MediaCandidate(width, height, uri.toString()))) - var videoVersions: List? = null + var videoVersions: List? = null if (isVideo) { - val videoVersion = VideoVersion( - null, - null, + val videoVersion = MediaCandidate( width, height, uri.toString() diff --git a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java index 755b6198..ad1382f8 100644 --- a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java @@ -43,7 +43,7 @@ import awais.instagrabber.models.enums.MediaItemType; import awais.instagrabber.repositories.responses.Audio; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.User; -import awais.instagrabber.repositories.responses.VideoVersion; +import awais.instagrabber.repositories.responses.MediaCandidate; import awais.instagrabber.workers.DownloadWorker; import static awais.instagrabber.fragments.settings.PreferenceKeys.PREF_BARINSTA_DIR_URI; @@ -340,7 +340,8 @@ public final class DownloadUtils { switch (media.getMediaType()) { case MEDIA_TYPE_IMAGE: case MEDIA_TYPE_VIDEO: { - final String url = ResponseBodyUtils.getImageUrl(media); + final String url = media.getMediaType() == MediaItemType.MEDIA_TYPE_VIDEO + ? ResponseBodyUtils.getVideoUrl(media) : ResponseBodyUtils.getImageUrl(media); final Pair, String> file = getDownloadSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), url, ""); final boolean fileExists = file.first != null && checkPathExists(file.first); boolean usernameFileExists = false; @@ -357,7 +358,8 @@ public final class DownloadUtils { for (int i = 0; i < sliderItems.size(); i++) { final Media child = sliderItems.get(i); if (child == null) continue; - final String url = ResponseBodyUtils.getImageUrl(child); + final String url = child.getMediaType() == MediaItemType.MEDIA_TYPE_VIDEO + ? ResponseBodyUtils.getVideoUrl(child) : ResponseBodyUtils.getImageUrl(child); final Pair, String> file = getDownloadChildSavePaths( new ArrayList<>(userFolderPaths), media.getCode(), i + 1, url, ""); final boolean fileExists = file.first != null && checkPathExists(file.first); @@ -548,10 +550,10 @@ public final class DownloadUtils { return ResponseBodyUtils.getImageUrl(media); } case MEDIA_TYPE_VIDEO: { - final List videoVersions = media.getVideoVersions(); + final List videoVersions = media.getVideoVersions(); String url = null; if (videoVersions != null && !videoVersions.isEmpty()) { - final VideoVersion videoVersion = videoVersions.get(0); + final MediaCandidate videoVersion = videoVersions.get(0); if (videoVersion != null) { url = videoVersion.getUrl(); } diff --git a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java index 333defa8..0330d6e2 100644 --- a/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/ResponseBodyUtils.java @@ -28,7 +28,7 @@ import awais.instagrabber.repositories.responses.Location; import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.MediaCandidate; import awais.instagrabber.repositories.responses.User; -import awais.instagrabber.repositories.responses.VideoVersion; +import awais.instagrabber.repositories.responses.MediaCandidate; public final class ResponseBodyUtils { private static final String TAG = "ResponseBodyUtils"; @@ -188,11 +188,9 @@ public final class ResponseBodyUtils { owner.optBoolean("is_verified")); } final String id = feedItem.getString(Constants.EXTRAS_ID); - VideoVersion videoVersion = null; + MediaCandidate videoVersion = null; if (isVideo) { - videoVersion = new VideoVersion( - null, - null, + videoVersion = new MediaCandidate( width, height, resourceUrl @@ -439,6 +437,37 @@ public final class ResponseBodyUtils { return candidate.getUrl(); } + public static String getThumbVideoUrl(final Media media) { + return getVideoCandidate(media, CandidateType.VIDEO_THUMBNAIL); + } + + public static String getVideoUrl(final Media media) { + return getVideoCandidate(media, CandidateType.DOWNLOAD); + } + + // TODO: merge with getImageCandidate when Kotlin + private static String getVideoCandidate(final Media media, final CandidateType type) { + if (media == null) return null; + final List candidates = media.getVideoVersions(); + if (candidates == null || candidates.isEmpty()) return null; + final boolean isSquare = Integer.compare(media.getOriginalWidth(), media.getOriginalHeight()) == 0; + final List sortedCandidates = candidates.stream() + .sorted((c1, c2) -> Integer.compare(c2.getWidth(), c1.getWidth())) + .collect(Collectors.toList()); + final List filteredCandidates = sortedCandidates.stream() + .filter(c -> + c.getWidth() <= media.getOriginalWidth() + && c.getWidth() <= type.getValue() + && (isSquare || Integer + .compare(c.getWidth(), c.getHeight()) != 0) + ) + .collect(Collectors.toList()); + if (filteredCandidates.size() == 0) return sortedCandidates.get(0).getUrl(); + final MediaCandidate candidate = filteredCandidates.get(0); + if (candidate == null) return null; + return candidate.getUrl(); + } + public static StoryModel parseBroadcastItem(final JSONObject data) throws JSONException { final StoryModel model = new StoryModel(data.getString("id"), data.getString("cover_frame_url"), @@ -453,6 +482,7 @@ public final class ResponseBodyUtils { } private enum CandidateType { + VIDEO_THUMBNAIL(700), THUMBNAIL(1000), DOWNLOAD(10000);