-
-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use VideoLinkFetcher to load the video link in posts.
- Loading branch information
1 parent
04c8a6d
commit 341f1b3
Showing
16 changed files
with
421 additions
and
276 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
app/src/main/java/ml/docilealligator/infinityforreddit/FetchVideoLinkListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
121 changes: 121 additions & 0 deletions
121
app/src/main/java/ml/docilealligator/infinityforreddit/VideoLinkFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.