mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-11-10 12:47:26 +01:00
Support REdgifs and Gfycat videos in post lists and links.
This commit is contained in:
parent
d9de2938b4
commit
ca957e8904
@ -6,5 +6,5 @@ import retrofit2.http.Path;
|
||||
|
||||
public interface GfycatAPI {
|
||||
@GET("{gfyid}")
|
||||
Call<String> getSubredditData(@Path("gfyid") String gfyId);
|
||||
Call<String> getGfycatData(@Path("gfyid") String gfyId);
|
||||
}
|
||||
|
@ -39,6 +39,7 @@ public class LinkResolverActivity extends AppCompatActivity {
|
||||
private static final String MULTIREDDIT_PATTERN_2 = "/[rR]/(\\w+\\+?)+/?";
|
||||
private static final String REDD_IT_POST_PATTERN = "/\\w+/?";
|
||||
private static final String GFYCAT_PATTERN = "/[\\w-]+$";
|
||||
private static final String REDGIFS_PATTERN = "/watch/[\\w-]+$";
|
||||
private static final String IMGUR_GALLERY_PATTERN = "/gallery/\\w+/?";
|
||||
private static final String IMGUR_ALBUM_PATTERN = "/(album|a)/\\w+/?";
|
||||
private static final String IMGUR_IMAGE_PATTERN = "/\\w+/?";
|
||||
@ -76,6 +77,28 @@ public class LinkResolverActivity extends AppCompatActivity {
|
||||
path = path.substring(0, path.length() - 1);
|
||||
}
|
||||
|
||||
if (path.endsWith("jpg") || path.endsWith("png")) {
|
||||
Intent intent = new Intent(this, ViewImageActivity.class);
|
||||
String url = uri.toString();
|
||||
String fileName = url.substring(url.lastIndexOf('/') + 1);
|
||||
intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, url);
|
||||
intent.putExtra(ViewImageActivity.FILE_NAME_KEY, fileName);
|
||||
intent.putExtra(ViewImageActivity.POST_TITLE_KEY, fileName);
|
||||
startActivity(intent);
|
||||
} else if (path.endsWith("gif")) {
|
||||
Intent intent = new Intent(this, ViewGIFActivity.class);
|
||||
String url = uri.toString();
|
||||
String fileName = url.substring(url.lastIndexOf('/') + 1);
|
||||
intent.putExtra(ViewGIFActivity.GIF_URL_KEY, url);
|
||||
intent.putExtra(ViewGIFActivity.FILE_NAME_KEY, fileName);
|
||||
intent.putExtra(ViewGIFActivity.POST_TITLE_KEY, fileName);
|
||||
startActivity(intent);
|
||||
} else if (path.endsWith("mp4")) {
|
||||
Intent intent = new Intent(this, ViewVideoActivity.class);
|
||||
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_DIRECT);
|
||||
intent.setData(uri);
|
||||
startActivity(intent);
|
||||
} else {
|
||||
String messageFullname = getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME);
|
||||
String newAccountName = getIntent().getStringExtra(EXTRA_NEW_ACCOUNT_NAME);
|
||||
|
||||
@ -165,6 +188,15 @@ public class LinkResolverActivity extends AppCompatActivity {
|
||||
} else {
|
||||
deepLinkError(uri);
|
||||
}
|
||||
} else if (authority.contains("redgifs.com")) {
|
||||
if (path.matches(REDGIFS_PATTERN)) {
|
||||
Intent intent = new Intent(this, ViewVideoActivity.class);
|
||||
intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, path.substring(7));
|
||||
intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS);
|
||||
startActivity(intent);
|
||||
} else {
|
||||
deepLinkError(uri);
|
||||
}
|
||||
} else if (authority.contains("imgur.com")) {
|
||||
if (path.matches(IMGUR_GALLERY_PATTERN)) {
|
||||
Intent intent = new Intent(this, ViewImgurMediaActivity.class);
|
||||
@ -191,6 +223,7 @@ public class LinkResolverActivity extends AppCompatActivity {
|
||||
deepLinkError(uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
finish();
|
||||
}
|
||||
|
@ -78,6 +78,8 @@ public class ViewVideoActivity extends AppCompatActivity {
|
||||
public static final String EXTRA_PROGRESS_SECONDS = "EPS";
|
||||
public static final String EXTRA_VIDEO_TYPE = "EVT";
|
||||
public static final String EXTRA_GFYCAT_ID = "EGI";
|
||||
public static final int VIDEO_TYPE_DIRECT = 3;
|
||||
public static final int VIDEO_TYPE_REDGIFS = 2;
|
||||
public static final int VIDEO_TYPE_GFYCAT = 1;
|
||||
private static final int VIDEO_TYPE_NORMAL = 0;
|
||||
private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
|
||||
@ -114,6 +116,10 @@ public class ViewVideoActivity extends AppCompatActivity {
|
||||
@Named("gfycat")
|
||||
Retrofit gfycatRetrofit;
|
||||
|
||||
@Inject
|
||||
@Named("redgifs")
|
||||
Retrofit redgifsRetrofit;
|
||||
|
||||
@Inject
|
||||
@Named("default")
|
||||
SharedPreferences mSharedPreferences;
|
||||
@ -335,7 +341,8 @@ public class ViewVideoActivity extends AppCompatActivity {
|
||||
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
|
||||
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
|
||||
videoPlayerView.setPlayer(player);
|
||||
if (getIntent().getIntExtra(EXTRA_VIDEO_TYPE, VIDEO_TYPE_NORMAL) == VIDEO_TYPE_GFYCAT) {
|
||||
int videoType = getIntent().getIntExtra(EXTRA_VIDEO_TYPE, VIDEO_TYPE_NORMAL);
|
||||
if (videoType == VIDEO_TYPE_GFYCAT || videoType == VIDEO_TYPE_REDGIFS) {
|
||||
if (savedInstanceState != null) {
|
||||
String videoUrl = savedInstanceState.getString(VIDEO_URI_STATE);
|
||||
if (videoUrl != null) {
|
||||
@ -350,8 +357,15 @@ public class ViewVideoActivity extends AppCompatActivity {
|
||||
if (gfycatId != null && gfycatId.contains("-")) {
|
||||
gfycatId = gfycatId.substring(0, gfycatId.indexOf('-'));
|
||||
}
|
||||
Retrofit retrofit;
|
||||
if (videoType == VIDEO_TYPE_GFYCAT) {
|
||||
retrofit = gfycatRetrofit;
|
||||
videoFileName = "Gfycat-" + gfycatId + ".mp4";
|
||||
FetchGfycatVideoLinks.fetchGfycatVideoLinks(gfycatRetrofit, gfycatId,
|
||||
} else {
|
||||
retrofit = redgifsRetrofit;
|
||||
videoFileName = "Redgifs-" + gfycatId + ".mp4";
|
||||
}
|
||||
FetchGfycatVideoLinks.fetchGfycatVideoLinks(retrofit, gfycatId,
|
||||
new FetchGfycatVideoLinks.FetchGfycatVideoLinksListener() {
|
||||
@Override
|
||||
public void success(String webm, String mp4) {
|
||||
@ -367,7 +381,11 @@ public class ViewVideoActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void failed() {
|
||||
progressBar.setVisibility(View.GONE);
|
||||
if (videoType == VIDEO_TYPE_GFYCAT) {
|
||||
Toast.makeText(ViewVideoActivity.this, R.string.fetch_gfycat_video_failed, Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(ViewVideoActivity.this, R.string.fetch_redgifs_video_failed, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@ -376,6 +394,15 @@ public class ViewVideoActivity extends AppCompatActivity {
|
||||
player.prepare(new DashMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
|
||||
preparePlayer(savedInstanceState);
|
||||
}
|
||||
} if (videoType == VIDEO_TYPE_REDGIFS) {
|
||||
|
||||
} else if (videoType == VIDEO_TYPE_DIRECT) {
|
||||
videoDownloadUrl = mVideoUri.toString();
|
||||
videoFileName = videoDownloadUrl.substring(videoDownloadUrl.lastIndexOf('/') + 1);
|
||||
// Produces DataSource instances through which media data is loaded.
|
||||
dataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, "Infinity"));
|
||||
// Prepare the player with the source.
|
||||
player.prepare(new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mVideoUri));
|
||||
} else {
|
||||
videoDownloadUrl = intent.getStringExtra(EXTRA_VIDEO_DOWNLOAD_URL);
|
||||
videoFileName = intent.getStringExtra(EXTRA_SUBREDDIT) + "-" + intent.getStringExtra(EXTRA_ID) + ".mp4";
|
||||
|
@ -100,6 +100,16 @@ class AppModule {
|
||||
.build();
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Named("redgifs")
|
||||
@Singleton
|
||||
Retrofit provideRedgifsRetrofit() {
|
||||
return new Retrofit.Builder()
|
||||
.baseUrl(APIUtils.REDGIFS_API_BASE_URI)
|
||||
.addConverterFactory(ScalarsConverterFactory.create())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Named("imgur")
|
||||
@Singleton
|
||||
|
@ -22,7 +22,7 @@ public class FetchGfycatVideoLinks {
|
||||
|
||||
public static void fetchGfycatVideoLinks(Retrofit gfycatRetrofit, String gfycatId,
|
||||
FetchGfycatVideoLinksListener fetchGfycatVideoLinksListener) {
|
||||
gfycatRetrofit.create(GfycatAPI.class).getSubredditData(gfycatId).enqueue(new Callback<String>() {
|
||||
gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback<String>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||
if (response.isSuccessful()) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ml.docilealligator.infinityforreddit.Post;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.text.Html;
|
||||
|
||||
@ -277,6 +278,19 @@ public class ParsePost {
|
||||
post.setPreviewWidth(previewWidth);
|
||||
post.setPreviewHeight(previewHeight);
|
||||
post.setVideoUrl(url);
|
||||
} else if (url.endsWith("mp4")) {
|
||||
//Video post
|
||||
int postType = Post.VIDEO_TYPE;
|
||||
|
||||
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
|
||||
authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title,
|
||||
previewUrl, thumbnailPreviewUrl, url, permalink, score, postType,
|
||||
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied,
|
||||
archived, locked, saved, isCrosspost);
|
||||
post.setPreviewWidth(previewWidth);
|
||||
post.setPreviewHeight(previewHeight);
|
||||
post.setVideoUrl(url);
|
||||
post.setVideoDownloadUrl(url);
|
||||
} else {
|
||||
if (url.contains(permalink)) {
|
||||
//Text post but with a preview
|
||||
@ -339,6 +353,19 @@ public class ParsePost {
|
||||
archived, locked, saved, isCrosspost);
|
||||
post.setPreviewWidth(previewWidth);
|
||||
post.setPreviewHeight(previewHeight);
|
||||
} else if (url.endsWith("mp4")) {
|
||||
//Video post
|
||||
int postType = Post.VIDEO_TYPE;
|
||||
|
||||
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
|
||||
authorFlair, authorFlairHTML, formattedPostTime, postTimeMillis, title,
|
||||
previewUrl, thumbnailPreviewUrl, url, permalink, score, postType,
|
||||
voteType, nComments, flair, awards, nAwards, hidden, spoiler, nsfw, stickied,
|
||||
archived, locked, saved, isCrosspost);
|
||||
post.setPreviewWidth(previewWidth);
|
||||
post.setPreviewHeight(previewHeight);
|
||||
post.setVideoUrl(url);
|
||||
post.setVideoDownloadUrl(url);
|
||||
} else {
|
||||
//CP No Preview Link post
|
||||
int postType = Post.NO_PREVIEW_LINK_TYPE;
|
||||
@ -352,6 +379,17 @@ public class ParsePost {
|
||||
}
|
||||
}
|
||||
|
||||
if (post.getPostType() == Post.VIDEO_TYPE) {
|
||||
try {
|
||||
Uri uri = Uri.parse(url);
|
||||
String authority = uri.getAuthority();
|
||||
if (authority != null && (authority.contains("gfycat.com") || authority.contains("redgifs.com"))) {
|
||||
post.setPostType(Post.LINK_TYPE);
|
||||
post.setUrl(url);
|
||||
}
|
||||
} catch (IllegalArgumentException ignore) { }
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
|
||||
|
@ -324,6 +324,10 @@ public class Post implements Parcelable {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getVideoUrl() {
|
||||
return videoUrl;
|
||||
}
|
||||
@ -372,6 +376,10 @@ public class Post implements Parcelable {
|
||||
return postType;
|
||||
}
|
||||
|
||||
public void setPostType(int postType) {
|
||||
this.postType = postType;
|
||||
}
|
||||
|
||||
public int getVoteType() {
|
||||
return voteType;
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ public class APIUtils {
|
||||
public static final String API_UPLOAD_MEDIA_URI = "https://reddit-uploaded-media.s3-accelerate.amazonaws.com";
|
||||
public static final String API_UPLOAD_VIDEO_URI = "https://reddit-uploaded-video.s3-accelerate.amazonaws.com";
|
||||
public static final String GFYCAT_API_BASE_URI = "https://api.gfycat.com/v1/gfycats/";
|
||||
public static final String REDGIFS_API_BASE_URI = "https://api.redgifs.com/v1/gfycats/";
|
||||
public static final String IMGUR_API_BASE_URI = "https://api.imgur.com/3/";
|
||||
|
||||
public static final String CLIENT_ID_KEY = "client_id";
|
||||
|
@ -734,6 +734,7 @@
|
||||
<string name="popular">Popular</string>
|
||||
|
||||
<string name="fetch_gfycat_video_failed">Fetch Gfycat video failed</string>
|
||||
<string name="fetch_redgifs_video_failed">Fetch Redgifs video failed</string>
|
||||
<string name="fetching_video_info_please_wait">Fetching video info. Please wait.</string>
|
||||
|
||||
<string name="error_fetching_imgur_media">Cannot load images</string>
|
||||
|
Loading…
Reference in New Issue
Block a user