1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +00:00

fix not detecting downloaded video

This commit is contained in:
Austin Huang 2021-06-20 15:07:15 -04:00
parent ad69f7d382
commit 3db95163f8
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
9 changed files with 56 additions and 83 deletions

View File

@ -17,7 +17,7 @@ import awais.instagrabber.customviews.VideoPlayerViewHelper;
import awais.instagrabber.databinding.LayoutVideoPlayerWithThumbnailBinding; import awais.instagrabber.databinding.LayoutVideoPlayerWithThumbnailBinding;
import awais.instagrabber.fragments.settings.PreferenceKeys; import awais.instagrabber.fragments.settings.PreferenceKeys;
import awais.instagrabber.repositories.responses.Media; 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.NumberUtils;
import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.ResponseBodyUtils;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
@ -122,9 +122,9 @@ public class SliderVideoViewHolder extends SliderItemViewHolder {
}; };
final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight();
String videoUrl = null; String videoUrl = null;
final List<VideoVersion> videoVersions = media.getVideoVersions(); final List<MediaCandidate> videoVersions = media.getVideoVersions();
if (videoVersions != null && !videoVersions.isEmpty()) { if (videoVersions != null && !videoVersions.isEmpty()) {
final VideoVersion videoVersion = videoVersions.get(0); final MediaCandidate videoVersion = videoVersions.get(0);
if (videoVersion != null) { if (videoVersion != null) {
videoUrl = videoVersion.getUrl(); videoUrl = videoVersion.getUrl();
} }

View File

@ -20,7 +20,7 @@ import awais.instagrabber.customviews.VideoPlayerViewHelper;
import awais.instagrabber.databinding.ItemFeedVideoBinding; import awais.instagrabber.databinding.ItemFeedVideoBinding;
import awais.instagrabber.fragments.settings.PreferenceKeys; import awais.instagrabber.fragments.settings.PreferenceKeys;
import awais.instagrabber.repositories.responses.Media; 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.NumberUtils;
import awais.instagrabber.utils.ResponseBodyUtils; import awais.instagrabber.utils.ResponseBodyUtils;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
@ -85,9 +85,9 @@ public class FeedVideoViewHolder extends FeedItemViewHolder {
}; };
final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight();
String videoUrl = null; String videoUrl = null;
final List<VideoVersion> videoVersions = media.getVideoVersions(); final List<MediaCandidate> videoVersions = media.getVideoVersions();
if (videoVersions != null && !videoVersions.isEmpty()) { if (videoVersions != null && !videoVersions.isEmpty()) {
final VideoVersion videoVersion = videoVersions.get(0); final MediaCandidate videoVersion = videoVersions.get(0);
videoUrl = videoVersion.getUrl(); videoUrl = videoVersion.getUrl();
} }
final VideoPlayerViewHelper videoPlayerViewHelper = new VideoPlayerViewHelper(binding.getRoot().getContext(), final VideoPlayerViewHelper videoPlayerViewHelper = new VideoPlayerViewHelper(binding.getRoot().getContext(),

View File

@ -94,7 +94,7 @@ import awais.instagrabber.repositories.responses.Caption;
import awais.instagrabber.repositories.responses.Location; import awais.instagrabber.repositories.responses.Location;
import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.Media;
import awais.instagrabber.repositories.responses.User; 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.repositories.responses.directmessages.RankedRecipient;
import awais.instagrabber.utils.DownloadUtils; import awais.instagrabber.utils.DownloadUtils;
import awais.instagrabber.utils.NullSafePair; import awais.instagrabber.utils.NullSafePair;
@ -1148,9 +1148,9 @@ public class PostViewV2Fragment extends Fragment implements EditTextDialogFragme
}; };
final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight();
String videoUrl = null; String videoUrl = null;
final List<VideoVersion> videoVersions = media.getVideoVersions(); final List<MediaCandidate> videoVersions = media.getVideoVersions();
if (videoVersions != null && !videoVersions.isEmpty()) { if (videoVersions != null && !videoVersions.isEmpty()) {
final VideoVersion videoVersion = videoVersions.get(0); final MediaCandidate videoVersion = videoVersions.get(0);
if (videoVersion != null) { if (videoVersion != null) {
videoUrl = videoVersion.getUrl(); videoUrl = videoVersion.getUrl();
} }

View File

@ -24,7 +24,7 @@ data class Media(
var likeCount: Long = 0, var likeCount: Long = 0,
var hasLiked: Boolean = false, var hasLiked: Boolean = false,
val isReelMedia: Boolean = false, val isReelMedia: Boolean = false,
val videoVersions: List<VideoVersion>? = null, val videoVersions: List<MediaCandidate>? = null,
val hasAudio: Boolean = false, val hasAudio: Boolean = false,
val videoDuration: Double = 0.0, val videoDuration: Double = 0.0,
val viewCount: Long = 0, val viewCount: Long = 0,

View File

@ -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);
}
}

View File

@ -4,7 +4,7 @@ import awais.instagrabber.models.enums.MediaItemType
import awais.instagrabber.utils.TextUtils import awais.instagrabber.utils.TextUtils
import awais.instagrabber.repositories.responses.ImageVersions2 import awais.instagrabber.repositories.responses.ImageVersions2
import awais.instagrabber.repositories.responses.User import awais.instagrabber.repositories.responses.User
import awais.instagrabber.repositories.responses.VideoVersion import awais.instagrabber.repositories.responses.MediaCandidate
import java.io.Serializable import java.io.Serializable
import java.util.* import java.util.*
@ -20,7 +20,7 @@ data class StoryMedia(
val originalHeight: Int = 0, val originalHeight: Int = 0,
val mediaType: MediaItemType? = null, val mediaType: MediaItemType? = null,
val isReelMedia: Boolean = false, val isReelMedia: Boolean = false,
val videoVersions: List<VideoVersion>? = null, val videoVersions: List<MediaCandidate>? = null,
val hasAudio: Boolean = false, val hasAudio: Boolean = false,
val videoDuration: Double = 0.0, val videoDuration: Double = 0.0,
val viewCount: Long = 0, val viewCount: Long = 0,

View File

@ -38,11 +38,9 @@ fun createImageOrVideo(
isVideo: Boolean isVideo: Boolean
): DirectItem { ): DirectItem {
val imageVersions2 = ImageVersions2(listOf(MediaCandidate(width, height, uri.toString()))) val imageVersions2 = ImageVersions2(listOf(MediaCandidate(width, height, uri.toString())))
var videoVersions: List<VideoVersion>? = null var videoVersions: List<MediaCandidate>? = null
if (isVideo) { if (isVideo) {
val videoVersion = VideoVersion( val videoVersion = MediaCandidate(
null,
null,
width, width,
height, height,
uri.toString() uri.toString()

View File

@ -43,7 +43,7 @@ import awais.instagrabber.models.enums.MediaItemType;
import awais.instagrabber.repositories.responses.Audio; import awais.instagrabber.repositories.responses.Audio;
import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.Media;
import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.repositories.responses.VideoVersion; import awais.instagrabber.repositories.responses.MediaCandidate;
import awais.instagrabber.workers.DownloadWorker; import awais.instagrabber.workers.DownloadWorker;
import static awais.instagrabber.fragments.settings.PreferenceKeys.PREF_BARINSTA_DIR_URI; import static awais.instagrabber.fragments.settings.PreferenceKeys.PREF_BARINSTA_DIR_URI;
@ -340,7 +340,8 @@ public final class DownloadUtils {
switch (media.getMediaType()) { switch (media.getMediaType()) {
case MEDIA_TYPE_IMAGE: case MEDIA_TYPE_IMAGE:
case MEDIA_TYPE_VIDEO: { 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<List<String>, String> file = getDownloadSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), url, ""); final Pair<List<String>, String> file = getDownloadSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), url, "");
final boolean fileExists = file.first != null && checkPathExists(file.first); final boolean fileExists = file.first != null && checkPathExists(file.first);
boolean usernameFileExists = false; boolean usernameFileExists = false;
@ -357,7 +358,8 @@ public final class DownloadUtils {
for (int i = 0; i < sliderItems.size(); i++) { for (int i = 0; i < sliderItems.size(); i++) {
final Media child = sliderItems.get(i); final Media child = sliderItems.get(i);
if (child == null) continue; 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<List<String>, String> file = getDownloadChildSavePaths( final Pair<List<String>, String> file = getDownloadChildSavePaths(
new ArrayList<>(userFolderPaths), media.getCode(), i + 1, url, ""); new ArrayList<>(userFolderPaths), media.getCode(), i + 1, url, "");
final boolean fileExists = file.first != null && checkPathExists(file.first); final boolean fileExists = file.first != null && checkPathExists(file.first);
@ -548,10 +550,10 @@ public final class DownloadUtils {
return ResponseBodyUtils.getImageUrl(media); return ResponseBodyUtils.getImageUrl(media);
} }
case MEDIA_TYPE_VIDEO: { case MEDIA_TYPE_VIDEO: {
final List<VideoVersion> videoVersions = media.getVideoVersions(); final List<MediaCandidate> videoVersions = media.getVideoVersions();
String url = null; String url = null;
if (videoVersions != null && !videoVersions.isEmpty()) { if (videoVersions != null && !videoVersions.isEmpty()) {
final VideoVersion videoVersion = videoVersions.get(0); final MediaCandidate videoVersion = videoVersions.get(0);
if (videoVersion != null) { if (videoVersion != null) {
url = videoVersion.getUrl(); url = videoVersion.getUrl();
} }

View File

@ -28,7 +28,7 @@ import awais.instagrabber.repositories.responses.Location;
import awais.instagrabber.repositories.responses.Media; import awais.instagrabber.repositories.responses.Media;
import awais.instagrabber.repositories.responses.MediaCandidate; import awais.instagrabber.repositories.responses.MediaCandidate;
import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.repositories.responses.VideoVersion; import awais.instagrabber.repositories.responses.MediaCandidate;
public final class ResponseBodyUtils { public final class ResponseBodyUtils {
private static final String TAG = "ResponseBodyUtils"; private static final String TAG = "ResponseBodyUtils";
@ -188,11 +188,9 @@ public final class ResponseBodyUtils {
owner.optBoolean("is_verified")); owner.optBoolean("is_verified"));
} }
final String id = feedItem.getString(Constants.EXTRAS_ID); final String id = feedItem.getString(Constants.EXTRAS_ID);
VideoVersion videoVersion = null; MediaCandidate videoVersion = null;
if (isVideo) { if (isVideo) {
videoVersion = new VideoVersion( videoVersion = new MediaCandidate(
null,
null,
width, width,
height, height,
resourceUrl resourceUrl
@ -439,6 +437,37 @@ public final class ResponseBodyUtils {
return candidate.getUrl(); 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<MediaCandidate> candidates = media.getVideoVersions();
if (candidates == null || candidates.isEmpty()) return null;
final boolean isSquare = Integer.compare(media.getOriginalWidth(), media.getOriginalHeight()) == 0;
final List<MediaCandidate> sortedCandidates = candidates.stream()
.sorted((c1, c2) -> Integer.compare(c2.getWidth(), c1.getWidth()))
.collect(Collectors.toList());
final List<MediaCandidate> 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 { public static StoryModel parseBroadcastItem(final JSONObject data) throws JSONException {
final StoryModel model = new StoryModel(data.getString("id"), final StoryModel model = new StoryModel(data.getString("id"),
data.getString("cover_frame_url"), data.getString("cover_frame_url"),
@ -453,6 +482,7 @@ public final class ResponseBodyUtils {
} }
private enum CandidateType { private enum CandidateType {
VIDEO_THUMBNAIL(700),
THUMBNAIL(1000), THUMBNAIL(1000),
DOWNLOAD(10000); DOWNLOAD(10000);