diff options
Diffstat (limited to 'src/main/java/com')
| -rw-r--r-- | src/main/java/com/pina/Holodex.java | 55 | ||||
| -rw-r--r-- | src/main/java/com/pina/HolodexService.java | 26 | ||||
| -rw-r--r-- | src/main/java/com/pina/datatypes/Paginated.java | 12 | ||||
| -rw-r--r-- | src/main/java/com/pina/datatypes/VideoSearchResult.java | 8 | ||||
| -rw-r--r-- | src/main/java/com/pina/factory/VideoSearchResultConverterFactory.java | 51 | ||||
| -rw-r--r-- | src/main/java/com/pina/query/VideoSearchQueryBuilder.java | 130 |
6 files changed, 268 insertions, 14 deletions
diff --git a/src/main/java/com/pina/Holodex.java b/src/main/java/com/pina/Holodex.java index 9e7ef39..793e64f 100644 --- a/src/main/java/com/pina/Holodex.java +++ b/src/main/java/com/pina/Holodex.java @@ -1,21 +1,29 @@ 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 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 @@ -43,7 +51,9 @@ public class Holodex { } private void initializeHolodexService(String apiKey, String baseUrl){ - OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); + OkHttpClient.Builder httpClient = new OkHttpClient.Builder() + .writeTimeout(35, TimeUnit.SECONDS) + .readTimeout(35, TimeUnit.SECONDS); httpClient.addInterceptor(chain -> { Request original = chain.request(); Request request = original.newBuilder() @@ -166,6 +176,34 @@ public class Holodex { 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 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 { @@ -180,4 +218,5 @@ public class Holodex { throw new HolodexException("Failed to execute API call", e); } } + } diff --git a/src/main/java/com/pina/HolodexService.java b/src/main/java/com/pina/HolodexService.java index c7cad0e..40502b7 100644 --- a/src/main/java/com/pina/HolodexService.java +++ b/src/main/java/com/pina/HolodexService.java @@ -1,13 +1,11 @@ package com.pina; -import com.pina.datatypes.Channel; -import com.pina.datatypes.SimpleVideo; -import com.pina.datatypes.Video; +import com.pina.datatypes.*; +import okhttp3.RequestBody; import retrofit2.Call; -import retrofit2.http.GET; -import retrofit2.http.Path; -import retrofit2.http.Query; +import retrofit2.http.*; +import javax.xml.transform.Result; import java.util.List; /** @@ -175,5 +173,21 @@ public interface HolodexService { @Query("sort") String sort ); + /** + * /api/v2/search/videoSearch endpoint + */ + @POST("/api/v2/search/videoSearch") + @Paginated(false) + Call<List<SimpleVideo>> postVideoSearch( + @Body RequestBody videoSearchResult + ); + + @POST("/api/v2/search/videoSearch") + @Paginated(true) + Call<VideoSearchResult> postPaginatedVideoSearch( + @Body RequestBody videoSearchResult + ); + + } diff --git a/src/main/java/com/pina/datatypes/Paginated.java b/src/main/java/com/pina/datatypes/Paginated.java new file mode 100644 index 0000000..8095daf --- /dev/null +++ b/src/main/java/com/pina/datatypes/Paginated.java @@ -0,0 +1,12 @@ +package com.pina.datatypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Paginated { + boolean value() default true; +} diff --git a/src/main/java/com/pina/datatypes/VideoSearchResult.java b/src/main/java/com/pina/datatypes/VideoSearchResult.java new file mode 100644 index 0000000..4a9996f --- /dev/null +++ b/src/main/java/com/pina/datatypes/VideoSearchResult.java @@ -0,0 +1,8 @@ +package com.pina.datatypes; + +import java.util.List; + +public class VideoSearchResult { + public int total; + public List<SimpleVideo> items; +} diff --git a/src/main/java/com/pina/factory/VideoSearchResultConverterFactory.java b/src/main/java/com/pina/factory/VideoSearchResultConverterFactory.java new file mode 100644 index 0000000..de47303 --- /dev/null +++ b/src/main/java/com/pina/factory/VideoSearchResultConverterFactory.java @@ -0,0 +1,51 @@ +package com.pina.factory; + +import com.pina.datatypes.Paginated; +import com.pina.datatypes.SimpleVideo; +import com.pina.datatypes.VideoSearchResult; +import okhttp3.ResponseBody; +import retrofit2.Converter; +import retrofit2.Retrofit; + +import java.lang.annotation.Annotation; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.List; + +public class VideoSearchResultConverterFactory extends Converter.Factory { + + public static VideoSearchResultConverterFactory create() { + return new VideoSearchResultConverterFactory(); + } + + @Override + public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { + boolean isPaginated = false; + for (Annotation annotation : annotations) { + if (annotation instanceof Paginated) { + isPaginated = ((Paginated) annotation).value(); + } + } + if (isPaginated) { + Type listType = new ParameterizedType() { + @Override + public Type[] getActualTypeArguments() { + return new Type[]{VideoSearchResult.class}; + } + + @Override + public Type getRawType() { + return List.class; + } + + @Override + public Type getOwnerType() { + return null; + } + }; + return retrofit.nextResponseBodyConverter(this, listType, annotations); + } else { + return retrofit.nextResponseBodyConverter(this, SimpleVideo.class, annotations); + } + } +} diff --git a/src/main/java/com/pina/query/VideoSearchQueryBuilder.java b/src/main/java/com/pina/query/VideoSearchQueryBuilder.java new file mode 100644 index 0000000..c4a82be --- /dev/null +++ b/src/main/java/com/pina/query/VideoSearchQueryBuilder.java @@ -0,0 +1,130 @@ +package com.pina.query; + +import java.util.ArrayList; +import java.util.List; + +public class VideoSearchQueryBuilder { + private String sort; + private List<String> lang; + private List<String> target; + private List<String> conditions; + private List<String> topic; + private List<String> vch; + private List<String> org; + private List<String> comment; + private boolean paginated; + private int offset; + private int limit; + + public VideoSearchQueryBuilder() { + this.sort = "newest"; + this.paginated = true; + this.offset = 0; + this.limit = 10; + this.topic = new ArrayList<String>(); + this.comment = new ArrayList<String>(); + this.org = List.of("Nijisanji"); + this.vch = new ArrayList<String>(); + this.conditions = new ArrayList<String>(); + this.lang = List.of("en"); + } + + public String getSort() { + return sort; + } + + public VideoSearchQueryBuilder setSort(String sort) { + this.sort = sort; + return this; + } + + public List<String> getLang() { + return lang; + } + + public VideoSearchQueryBuilder setLang(List<String> lang) { + this.lang = lang; + return this; + } + + public List<String> getTarget() { + return target; + } + + public VideoSearchQueryBuilder setTarget(List<String> target) { + this.target = target; + return this; + } + + public List<String> getConditions() { + return conditions; + } + + public VideoSearchQueryBuilder setConditions(List<String> conditions) { + this.conditions = conditions; + return this; + } + + public List<String> getTopic() { + return topic; + } + + public VideoSearchQueryBuilder setTopic(List<String> topic) { + this.topic = topic; + return this; + } + + public List<String> getVch() { + return vch; + } + + public VideoSearchQueryBuilder setVch(List<String> vch) { + this.vch = vch; + return this; + } + + public List<String> getOrg() { + return org; + } + + public VideoSearchQueryBuilder setOrg(List<String> org) { + this.org = org; + return this; + } + + public List<String> getComment() { + return comment; + } + + public VideoSearchQueryBuilder setComment(List<String> comment) { + this.comment = comment; + return this; + } + + public boolean isPaginated() { + return paginated; + } + + public VideoSearchQueryBuilder setPaginated(boolean paginated) { + this.paginated = paginated; + return this; + } + + public int getOffset() { + return offset; + } + + public VideoSearchQueryBuilder setOffset(int offset) { + this.offset = offset; + return this; + } + + public int getLimit() { + return limit; + } + + public VideoSearchQueryBuilder setLimit(int limit) { + this.limit = limit; + return this; + } +} |
