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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
package com.pina;
import com.google.gson.Gson;
import com.pina.datatypes.*;
import com.pina.query.*;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import javax.xml.transform.Result;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;
/**
* The class for interacting with the Holodex API
*/
public class Holodex {
private HolodexService service;
private int readTimeout = 35;
private int writeTimeout = 35;
/**
* Instantiates a new Holodex with the default base url
*
* @param apiKey the api key
*/
public Holodex(String apiKey) {
initializeHolodexService(apiKey, "https://holodex.net");
}
/**
* Instantiates a new Holodex with a custom base url
*
* @param apiKey the api key
* @param baseUrl the base url
*/
public Holodex(String apiKey, String baseUrl) {
initializeHolodexService(apiKey, baseUrl);
}
private void initializeHolodexService(String apiKey, String baseUrl){
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.writeTimeout(writeTimeout, TimeUnit.SECONDS)
.readTimeout(readTimeout, TimeUnit.SECONDS);
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request request = original.newBuilder()
.header("X-APIKEY", apiKey)
.method(original.method(), original.body())
.build();
return chain.proceed(request);
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(JacksonConverterFactory.create())
.client(httpClient.build())
.build();
service = retrofit.create(HolodexService.class);
}
private Holodex setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
private Holodex setWriteTimeout(int writeTimeout) {
this.writeTimeout = writeTimeout;
return this;
}
/**
* Gets a list of upcoming and/or live SimpleVideos matching the VideoQueryBuilder attributes
*
* @param queryBuilder the query builder
* @return A list of upcoming and/or live SimpleVideos
* @throws HolodexException the holodex exception
*/
public List<SimpleVideo> getLiveAndUpcomingVideos(VideoQueryBuilder queryBuilder) throws HolodexException {
Call<List<SimpleVideo>> call = service.getLiveVideos(queryBuilder.getChannelId(), queryBuilder.getId(),
queryBuilder.getInclude(), queryBuilder.getLang(),
queryBuilder.getLimit(), queryBuilder.getMaxUpcomingHours(),
queryBuilder.getMentionedChannelId(), queryBuilder.getOffset(),
queryBuilder.getOrder(), queryBuilder.getOrg(),
queryBuilder.getPaginated(), queryBuilder.getSort(),
queryBuilder.getStatus(), queryBuilder.getTopic(),
queryBuilder.getType());
return executeCall(call);
}
/**
* Gets a list of videos matching the VideoQueryBuilder attributes
*
* @param queryBuilder the query builder
* @return list of videos
* @throws HolodexException the holodex exception
*/
public List<Video> getVideos(VideoQueryBuilder queryBuilder) throws HolodexException {
Call<List<Video>> call = service.getVideos(queryBuilder.getChannelId(), queryBuilder.getId(),
queryBuilder.getInclude(), queryBuilder.getLang(),
queryBuilder.getLimit(), queryBuilder.getMaxUpcomingHours(),
queryBuilder.getMentionedChannelId(), queryBuilder.getOffset(),
queryBuilder.getOrder(), queryBuilder.getOrg(),
queryBuilder.getPaginated(), queryBuilder.getSort(),
queryBuilder.getStatus(), queryBuilder.getTopic(),
queryBuilder.getType());
return executeCall(call);
}
/**
* Gets information about a channel when given a channel id
*
* @param channelId the channel id
* @return the Channel
* @throws HolodexException the holodex exception
*/
public Channel getChannel(String channelId) throws HolodexException {
Call<Channel> call = service.getChannel(channelId);
return executeCall(call);
}
/**
* Gets a list of Videos matching the VideoByVideoIdQueryBuilder attributes for a specific channel
* Used for when the channel id is known
*
* @param query the query
* @return List of videos matching the query
* @throws HolodexException the holodex exception
*/
public List<Video> getVideosByChannelId(VideosByChannelIDQueryBuilder query) throws HolodexException {
Call<List<Video>> call = service.getVideosByChannelId(query.getChannelId(), query.getType(), query.getInclude(),
query.getLang(), query.getLimit(), query.getOffset(), query.getPaginated());
return executeCall(call);
}
/**
* Gets upcoming and live videos from an array of channel ids
* Response will contain videos from all channels together
* This should be used when there is a set of channels that need to be queried
*
* @param channels the channel ids to get videos from
* @return List of live and/or upcoming videos where the channel id is the author
* @throws HolodexException the holodex exception
*/
public List<Video> getVideosFromChannels(String[] channels) throws HolodexException{
String channelsString = String.join(",", channels);
Call<List<Video>> call = service.getVideosFromChannels(channelsString);
return executeCall(call);
}
/**
* Gets the video matching the VideoByVideoIdQueryBuilder attributes
*
* @param query the query
* @return Video matching the query
* @throws HolodexException holodex exception
*/
public Video getVideo(VideoByVideoIdQueryBuilder query) throws HolodexException {
Call<Video> call = service.getVideo(query.getVideoId(), query.getLang(), query.getC());
return executeCall(call);
}
/**
* Gets a list of channels the match the ChannelQueryBuilder attributes
*
* @param query the query
* @return List of channels matching the query
* @throws HolodexException the holodex exception
*/
public List<Channel> getChannels(ChannelQueryBuilder query) throws HolodexException{
Call<List<Channel>> call = service.getChannels(query.getLimit(), query.getOffset(), query.getType(),
query.getLang(), query.getOrder(), query.getOrg(), query.getSort()
);
return executeCall(call);
}
public Object searchVideo(VideoSearchQueryBuilder query) throws HolodexException {
Map<String, Object> payload = toMap(query);
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
new Gson().toJson(payload));
if(query.isPaginated()) {
Call<VideoSearchResult> call = service.postPaginatedVideoSearch(body);
return executeCall(call);
}
Call<List<SimpleVideo>> call = service.postVideoSearch(body);
return executeCall(call);
}
public Object searchComment(CommentSearchQueryBuilder query) throws HolodexException{
Map<String, Object> payload = toMap(query);
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
new Gson().toJson(payload));
if(query.isPaginated()) {
Call<CommentSearchResult> call = service.postPaginatedCommentSearch(body);
return executeCall(call);
}
Call<List<SimpleCommentVideo>> call = service.postCommentSearch(body);
return executeCall(call);
}
public static Map<String, Object> toMap(Object obj) throws HolodexException {
Map<String, Object> map = new HashMap<>();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object value = field.get(obj);
map.put(field.getName(), value);
} catch (IllegalAccessException e) {
throw new HolodexException("Failed to execute API call", e);
}
}
return map;
}
private <T> T executeCall(Call<T> call) throws HolodexException {
try {
Response<T> response = call.execute();
if (response.isSuccessful()) {
return response.body();
} else {
throw new HolodexException("API returned error: " + response.code());
}
} catch (IOException e) {
System.out.println(e);
throw new HolodexException("Failed to execute API call", e);
}
}
}
|