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
|
package com.pina;
import com.pina.datatypes.Channel;
import com.pina.datatypes.SimpleVideo;
import com.pina.datatypes.Video;
import com.pina.query.ChannelQueryBuilder;
import com.pina.query.VideoByVideoIdQueryBuilder;
import com.pina.query.VideoQueryBuilder;
import com.pina.query.VideosByChannelIDQueryBuilder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import java.io.IOException;
import java.util.List;
public class Holodex {
private HolodexService service;
public Holodex(String apiKey) {
initializeHolodexService(apiKey, "https://holodex.net");
}
public Holodex(String apiKey, String baseUrl) {
// purely for unit testing
initializeHolodexService(apiKey, baseUrl);
}
private void initializeHolodexService(String apiKey, String baseUrl){
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
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);
}
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);
}
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);
}
public Channel getChannel(String channelId) throws HolodexException {
Call<Channel> call = service.getChannel(channelId);
return executeCall(call);
}
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);
}
public List<Video> getVideosFromChannels(String[] channels) throws HolodexException{
String channelsString = String.join(",", channels);
Call<List<Video>> call = service.getVideosFromChannels(channelsString);
return executeCall(call);
}
public Video getVideo(VideoByVideoIdQueryBuilder query) throws HolodexException {
Call<Video> call = service.getVideo(query.getVideoId(), query.getLang(), query.getC());
return executeCall(call);
}
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);
}
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);
}
}
}
|