Skip to content

Commit

Permalink
Use VideoLinkFetcher to load the video link in posts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Docile-Alligator committed Sep 17, 2024
1 parent 04c8a6d commit 341f1b3
Show file tree
Hide file tree
Showing 16 changed files with 421 additions and 276 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ml.docilealligator.infinityforreddit;

import androidx.annotation.Nullable;

import ml.docilealligator.infinityforreddit.post.Post;
import ml.docilealligator.infinityforreddit.thing.StreamableVideo;

public interface FetchVideoLinkListener {
default void onFetchRedditVideoLinkSuccess(Post post, String fileName) {}
default void onFetchImgurVideoLinkSuccess(String videoUrl, String videoDownloadUrl, String fileName) {}
default void onFetchRedgifsVideoLinkSuccess(String webm, String mp4) {}
default void onFetchStreamableVideoLinkSuccess(StreamableVideo streamableVideo) {}
default void onChangeFileName(String fileName) {}
default void onFetchVideoFallbackDirectUrlSuccess(String videoFallbackDirectUrl) {}
default void failed(@Nullable Integer messageRes) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package ml.docilealligator.infinityforreddit;

import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.OptIn;
import androidx.media3.common.util.UnstableApi;

import org.apache.commons.io.FilenameUtils;

import java.util.List;
import java.util.concurrent.Executor;

import javax.inject.Provider;

import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.activities.ViewVideoActivity;
import ml.docilealligator.infinityforreddit.apis.StreamableAPI;
import ml.docilealligator.infinityforreddit.apis.VReddIt;
import ml.docilealligator.infinityforreddit.post.FetchPost;
import ml.docilealligator.infinityforreddit.post.FetchStreamableVideo;
import ml.docilealligator.infinityforreddit.post.Post;
import ml.docilealligator.infinityforreddit.thing.FetchRedgifsVideoLinks;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class VideoLinkFetcher {
public static void fetchVideoLink(Executor executor, Handler handler, Retrofit retrofit, Retrofit vReddItRetrofit,
Retrofit redgifsRetrofit, Provider<StreamableAPI> streamableApiProvider,
SharedPreferences currentAccountSharedPreferences, int videoType,
@Nullable String redgifsId, @Nullable String vRedditItUrl,
@Nullable String shortCode,
FetchVideoLinkListener fetchVideoLinkListener) {
switch (videoType) {
case ViewVideoActivity.VIDEO_TYPE_STREAMABLE:
FetchStreamableVideo.fetchStreamableVideo(executor, handler, streamableApiProvider, shortCode, fetchVideoLinkListener);
break;
case ViewVideoActivity.VIDEO_TYPE_REDGIFS:
FetchRedgifsVideoLinks.fetchRedgifsVideoLinks(executor, handler, redgifsRetrofit,
currentAccountSharedPreferences, redgifsId, fetchVideoLinkListener);
break;
case ViewVideoActivity.VIDEO_TYPE_V_REDD_IT:
loadVReddItVideo(executor, handler, retrofit, vReddItRetrofit, redgifsRetrofit, streamableApiProvider,
currentAccountSharedPreferences, vRedditItUrl, fetchVideoLinkListener);
break;
}
}

public static void loadVReddItVideo(Executor executor, Handler handler, Retrofit retrofit, Retrofit mVReddItRetrofit,
Retrofit redgifsRetrofit, Provider<StreamableAPI> streamableApiProvider,
SharedPreferences currentAccountSharedPreferences,
String vRedditItUrl, FetchVideoLinkListener fetchVideoLinkListener) {
mVReddItRetrofit.create(VReddIt.class).getRedirectUrl(vRedditItUrl).enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
Uri redirectUri = Uri.parse(response.raw().request().url().toString());
String redirectPath = redirectUri.getPath();
if (redirectPath != null && (redirectPath.matches("/r/\\w+/comments/\\w+/?\\w+/?") || redirectPath.matches("/user/\\w+/comments/\\w+/?\\w+/?"))) {
List<String> segments = redirectUri.getPathSegments();
int commentsIndex = segments.lastIndexOf("comments");
String postId = segments.get(commentsIndex + 1);
FetchPost.fetchPost(executor, handler, retrofit, postId, null, Account.ANONYMOUS_ACCOUNT,
new FetchPost.FetchPostListener() {
@OptIn(markerClass = UnstableApi.class)
@Override
public void fetchPostSuccess(Post post) {
fetchVideoLinkListener.onFetchVideoFallbackDirectUrlSuccess(post.getVideoFallBackDirectUrl());
if (post.isRedgifs()) {
String redgifsId = post.getRedgifsId();
if (redgifsId != null && redgifsId.contains("-")) {
redgifsId = redgifsId.substring(0, redgifsId.indexOf('-'));
}
fetchVideoLinkListener.onChangeFileName("Redgifs-" + redgifsId + ".mp4");

FetchRedgifsVideoLinks.fetchRedgifsVideoLinks(executor, handler, redgifsRetrofit,
currentAccountSharedPreferences, redgifsId, fetchVideoLinkListener);
} else if (post.isStreamable()) {
String shortCode = post.getStreamableShortCode();
fetchVideoLinkListener.onChangeFileName("Streamable-" + shortCode + ".mp4");

FetchStreamableVideo.fetchStreamableVideo(executor, handler, streamableApiProvider, shortCode, fetchVideoLinkListener);
} else if (post.isImgur()) {
String videoDownloadUrl = post.getVideoDownloadUrl();
String videoFileName = "imgur-" + FilenameUtils.getName(videoDownloadUrl);
fetchVideoLinkListener.onFetchImgurVideoLinkSuccess(post.getVideoUrl(), post.getVideoDownloadUrl(), videoFileName);
} else {
if (post.getVideoUrl() != null) {
String videoFileName = post.getSubredditName() + "-" + post.getId() + ".mp4";
fetchVideoLinkListener.onFetchRedditVideoLinkSuccess(post, videoFileName);
} else {
fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_video_url);
}
}
}

@Override
public void fetchPostFailed() {
fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_post);
}
});
} else {
fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_post_id);
}
} else {
fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_redirect_url);
}
}

@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_redirect_url);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ protected void onCreate(Bundle savedInstanceState) {
postFilter.containGalleryType = false;
break;
case Post.LINK_TYPE:
case Post.NO_PREVIEW_LINK_TYPE:
postFilter.containTextType = false;
postFilter.containLinkType = true;
postFilter.containImageType = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,6 @@ private void requestPermissionAndDownload() {
private void download() {
isDownloading = false;

/*Intent intent = new Intent(this, DownloadMediaService.class);
intent.putExtra(DownloadMediaService.EXTRA_URL, mImageUrl);
intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, isGif ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF : DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, mImageFileName);
intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, mSubredditName);
intent.putExtra(DownloadMediaService.EXTRA_IS_NSFW, isNsfw);
ContextCompat.startForegroundService(this, intent);*/

PersistableBundle extras = new PersistableBundle();
extras.putString(DownloadMediaService.EXTRA_URL, mImageUrl);
extras.putInt(DownloadMediaService.EXTRA_MEDIA_TYPE, isGif ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF : DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private void fetchImgurMedia(String imgurId) {
switch (getIntent().getIntExtra(EXTRA_IMGUR_TYPE, IMGUR_TYPE_IMAGE)) {
case IMGUR_TYPE_GALLERY:
imgurRetrofit.create(ImgurAPI.class).getGalleryImages(APIUtils.IMGUR_CLIENT_ID, imgurId)
.enqueue(new Callback<String>() {
.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
Expand Down
Loading

0 comments on commit 341f1b3

Please sign in to comment.