1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
package ml.docilealligator.infinityforreddit.post;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.concurrent.Executor;
import javax.inject.Provider;
import ml.docilealligator.infinityforreddit.FetchVideoLinkListener;
import ml.docilealligator.infinityforreddit.thing.StreamableVideo;
import ml.docilealligator.infinityforreddit.apis.StreamableAPI;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import retrofit2.Call;
import retrofit2.Response;
public class FetchStreamableVideo {
public static void fetchStreamableVideo(Executor executor, Handler handler, Provider<StreamableAPI> streamableApiProvider,
String videoUrl, FetchVideoLinkListener fetchVideoLinkListener) {
executor.execute(() -> {
try {
Response<String> response = streamableApiProvider.get().getStreamableData(videoUrl).execute();
if (response.isSuccessful()) {
JSONObject jsonObject = new JSONObject(response.body());
String title = jsonObject.getString(JSONUtils.TITLE_KEY);
JSONObject filesObject = jsonObject.getJSONObject(JSONUtils.FILES_KEY);
StreamableVideo.Media mp4 = parseMedia(filesObject.getJSONObject(JSONUtils.MP4_KEY));
StreamableVideo.Media mp4MobileTemp = null;
if (filesObject.has(JSONUtils.MP4_MOBILE_KEY)) {
mp4MobileTemp = parseMedia(filesObject.getJSONObject(JSONUtils.MP4_MOBILE_KEY));
}
if (mp4 == null && mp4MobileTemp == null) {
handler.post(() -> fetchVideoLinkListener.failed(null));
return;
}
StreamableVideo.Media mp4Mobile = mp4MobileTemp;
handler.post(() -> fetchVideoLinkListener.onFetchStreamableVideoLinkSuccess(new StreamableVideo(title, mp4, mp4Mobile)));
} else {
handler.post(() -> fetchVideoLinkListener.failed(null));
}
} catch (IOException | JSONException e) {
e.printStackTrace();
handler.post(() -> fetchVideoLinkListener.failed(null));
}
});
}
@WorkerThread
@Nullable
public static StreamableVideo fetchStreamableVideoSync(Provider<StreamableAPI> streamableApiProvider,
String videoUrl) {
try {
Response<String> response = streamableApiProvider.get().getStreamableData(videoUrl).execute();
if (response.isSuccessful()) {
JSONObject jsonObject = new JSONObject(response.body());
String title = jsonObject.getString(JSONUtils.TITLE_KEY);
JSONObject filesObject = jsonObject.getJSONObject(JSONUtils.FILES_KEY);
StreamableVideo.Media mp4 = parseMedia(filesObject.getJSONObject(JSONUtils.MP4_KEY));
StreamableVideo.Media mp4MobileTemp = null;
if (filesObject.has(JSONUtils.MP4_MOBILE_KEY)) {
mp4MobileTemp = parseMedia(filesObject.getJSONObject(JSONUtils.MP4_MOBILE_KEY));
}
if (mp4 == null && mp4MobileTemp == null) {
return null;
}
StreamableVideo.Media mp4Mobile = mp4MobileTemp;
return new StreamableVideo(title, mp4, mp4Mobile);
} else {
return null;
}
} catch (IOException | JSONException e) {
e.printStackTrace();
return null;
}
}
public static void fetchStreamableVideoInRecyclerViewAdapter(Executor executor, Handler handler, Call<String> streamableCall,
FetchVideoLinkListener fetchVideoLinkListener) {
executor.execute(() -> {
try {
Response<String> response = streamableCall.execute();
if (response.isSuccessful()) {
JSONObject jsonObject = new JSONObject(response.body());
String title = jsonObject.getString(JSONUtils.TITLE_KEY);
JSONObject filesObject = jsonObject.getJSONObject(JSONUtils.FILES_KEY);
StreamableVideo.Media mp4 = parseMedia(filesObject.getJSONObject(JSONUtils.MP4_KEY));
StreamableVideo.Media mp4MobileTemp = null;
if (filesObject.has(JSONUtils.MP4_MOBILE_KEY)) {
mp4MobileTemp = parseMedia(filesObject.getJSONObject(JSONUtils.MP4_MOBILE_KEY));
}
if (mp4 == null && mp4MobileTemp == null) {
handler.post(() -> fetchVideoLinkListener.failed(null));
return;
}
StreamableVideo.Media mp4Mobile = mp4MobileTemp;
handler.post(() -> fetchVideoLinkListener.onFetchStreamableVideoLinkSuccess(new StreamableVideo(title, mp4, mp4Mobile)));
} else {
handler.post(() -> fetchVideoLinkListener.failed(null));
}
} catch (IOException | JSONException e) {
e.printStackTrace();
handler.post(() -> fetchVideoLinkListener.failed(null));
}
});
}
@Nullable
private static StreamableVideo.Media parseMedia(JSONObject jsonObject) {
try {
return new StreamableVideo.Media(
jsonObject.getString(JSONUtils.URL_KEY),
jsonObject.getInt(JSONUtils.WIDTH_KEY),
jsonObject.getInt(JSONUtils.HEIGHT_KEY));
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
|