|
| 1 | +package ml.docilealligator.infinityforreddit; |
| 2 | + |
| 3 | +import android.content.SharedPreferences; |
| 4 | +import android.net.Uri; |
| 5 | +import android.os.Handler; |
| 6 | + |
| 7 | +import androidx.annotation.NonNull; |
| 8 | +import androidx.annotation.Nullable; |
| 9 | +import androidx.annotation.OptIn; |
| 10 | +import androidx.media3.common.util.UnstableApi; |
| 11 | + |
| 12 | +import org.apache.commons.io.FilenameUtils; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.concurrent.Executor; |
| 16 | + |
| 17 | +import javax.inject.Provider; |
| 18 | + |
| 19 | +import ml.docilealligator.infinityforreddit.account.Account; |
| 20 | +import ml.docilealligator.infinityforreddit.activities.ViewVideoActivity; |
| 21 | +import ml.docilealligator.infinityforreddit.apis.StreamableAPI; |
| 22 | +import ml.docilealligator.infinityforreddit.apis.VReddIt; |
| 23 | +import ml.docilealligator.infinityforreddit.post.FetchPost; |
| 24 | +import ml.docilealligator.infinityforreddit.post.FetchStreamableVideo; |
| 25 | +import ml.docilealligator.infinityforreddit.post.Post; |
| 26 | +import ml.docilealligator.infinityforreddit.thing.FetchRedgifsVideoLinks; |
| 27 | +import retrofit2.Call; |
| 28 | +import retrofit2.Callback; |
| 29 | +import retrofit2.Response; |
| 30 | +import retrofit2.Retrofit; |
| 31 | + |
| 32 | +public class VideoLinkFetcher { |
| 33 | + public static void fetchVideoLink(Executor executor, Handler handler, Retrofit retrofit, Retrofit vReddItRetrofit, |
| 34 | + Retrofit redgifsRetrofit, Provider<StreamableAPI> streamableApiProvider, |
| 35 | + SharedPreferences currentAccountSharedPreferences, int videoType, |
| 36 | + @Nullable String redgifsId, @Nullable String vRedditItUrl, |
| 37 | + @Nullable String shortCode, |
| 38 | + FetchVideoLinkListener fetchVideoLinkListener) { |
| 39 | + switch (videoType) { |
| 40 | + case ViewVideoActivity.VIDEO_TYPE_STREAMABLE: |
| 41 | + FetchStreamableVideo.fetchStreamableVideo(executor, handler, streamableApiProvider, shortCode, fetchVideoLinkListener); |
| 42 | + break; |
| 43 | + case ViewVideoActivity.VIDEO_TYPE_REDGIFS: |
| 44 | + FetchRedgifsVideoLinks.fetchRedgifsVideoLinks(executor, handler, redgifsRetrofit, |
| 45 | + currentAccountSharedPreferences, redgifsId, fetchVideoLinkListener); |
| 46 | + break; |
| 47 | + case ViewVideoActivity.VIDEO_TYPE_V_REDD_IT: |
| 48 | + loadVReddItVideo(executor, handler, retrofit, vReddItRetrofit, redgifsRetrofit, streamableApiProvider, |
| 49 | + currentAccountSharedPreferences, vRedditItUrl, fetchVideoLinkListener); |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void loadVReddItVideo(Executor executor, Handler handler, Retrofit retrofit, Retrofit mVReddItRetrofit, |
| 55 | + Retrofit redgifsRetrofit, Provider<StreamableAPI> streamableApiProvider, |
| 56 | + SharedPreferences currentAccountSharedPreferences, |
| 57 | + String vRedditItUrl, FetchVideoLinkListener fetchVideoLinkListener) { |
| 58 | + mVReddItRetrofit.create(VReddIt.class).getRedirectUrl(vRedditItUrl).enqueue(new Callback<>() { |
| 59 | + @Override |
| 60 | + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { |
| 61 | + if (response.isSuccessful()) { |
| 62 | + Uri redirectUri = Uri.parse(response.raw().request().url().toString()); |
| 63 | + String redirectPath = redirectUri.getPath(); |
| 64 | + if (redirectPath != null && (redirectPath.matches("/r/\\w+/comments/\\w+/?\\w+/?") || redirectPath.matches("/user/\\w+/comments/\\w+/?\\w+/?"))) { |
| 65 | + List<String> segments = redirectUri.getPathSegments(); |
| 66 | + int commentsIndex = segments.lastIndexOf("comments"); |
| 67 | + String postId = segments.get(commentsIndex + 1); |
| 68 | + FetchPost.fetchPost(executor, handler, retrofit, postId, null, Account.ANONYMOUS_ACCOUNT, |
| 69 | + new FetchPost.FetchPostListener() { |
| 70 | + @OptIn(markerClass = UnstableApi.class) |
| 71 | + @Override |
| 72 | + public void fetchPostSuccess(Post post) { |
| 73 | + fetchVideoLinkListener.onFetchVideoFallbackDirectUrlSuccess(post.getVideoFallBackDirectUrl()); |
| 74 | + if (post.isRedgifs()) { |
| 75 | + String redgifsId = post.getRedgifsId(); |
| 76 | + if (redgifsId != null && redgifsId.contains("-")) { |
| 77 | + redgifsId = redgifsId.substring(0, redgifsId.indexOf('-')); |
| 78 | + } |
| 79 | + fetchVideoLinkListener.onChangeFileName("Redgifs-" + redgifsId + ".mp4"); |
| 80 | + |
| 81 | + FetchRedgifsVideoLinks.fetchRedgifsVideoLinks(executor, handler, redgifsRetrofit, |
| 82 | + currentAccountSharedPreferences, redgifsId, fetchVideoLinkListener); |
| 83 | + } else if (post.isStreamable()) { |
| 84 | + String shortCode = post.getStreamableShortCode(); |
| 85 | + fetchVideoLinkListener.onChangeFileName("Streamable-" + shortCode + ".mp4"); |
| 86 | + |
| 87 | + FetchStreamableVideo.fetchStreamableVideo(executor, handler, streamableApiProvider, shortCode, fetchVideoLinkListener); |
| 88 | + } else if (post.isImgur()) { |
| 89 | + String videoDownloadUrl = post.getVideoDownloadUrl(); |
| 90 | + String videoFileName = "imgur-" + FilenameUtils.getName(videoDownloadUrl); |
| 91 | + fetchVideoLinkListener.onFetchImgurVideoLinkSuccess(post.getVideoUrl(), post.getVideoDownloadUrl(), videoFileName); |
| 92 | + } else { |
| 93 | + if (post.getVideoUrl() != null) { |
| 94 | + String videoFileName = post.getSubredditName() + "-" + post.getId() + ".mp4"; |
| 95 | + fetchVideoLinkListener.onFetchRedditVideoLinkSuccess(post, videoFileName); |
| 96 | + } else { |
| 97 | + fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_video_url); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void fetchPostFailed() { |
| 104 | + fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_post); |
| 105 | + } |
| 106 | + }); |
| 107 | + } else { |
| 108 | + fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_post_id); |
| 109 | + } |
| 110 | + } else { |
| 111 | + fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_redirect_url); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { |
| 117 | + fetchVideoLinkListener.failed(R.string.error_fetching_v_redd_it_video_cannot_get_redirect_url); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
0 commit comments