TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitSepiaSearchAPI.java

87 lines
2.9 KiB
Java
Raw Normal View History

2020-10-09 10:19:46 +02:00
package app.fedilab.fedilabtube.client;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
2020-10-09 14:54:01 +02:00
import java.io.IOException;
2020-10-09 18:22:05 +02:00
import java.text.SimpleDateFormat;
import java.util.Locale;
2020-10-09 14:54:01 +02:00
import app.fedilab.fedilabtube.client.data.VideoData;
import app.fedilab.fedilabtube.client.entities.SepiaSearch;
import retrofit2.Call;
import retrofit2.Response;
2020-10-09 10:19:46 +02:00
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
2020-10-09 14:54:01 +02:00
public class RetrofitSepiaSearchAPI {
2020-10-09 10:19:46 +02:00
2020-10-17 18:50:20 +02:00
private final String finalUrl;
2020-10-09 10:19:46 +02:00
2020-10-09 18:22:05 +02:00
public RetrofitSepiaSearchAPI() {
2020-10-09 10:19:46 +02:00
finalUrl = "https://search.joinpeertube.org/api/v1/";
}
private SepiaSearchService init() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(finalUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(SepiaSearchService.class);
}
2020-10-09 14:54:01 +02:00
/**
* Return videos for a sepia search
2020-10-17 18:50:20 +02:00
*
2020-10-09 14:54:01 +02:00
* @param sepiaSearch SepiaSearch
* @return VideoData
*/
public VideoData getVideos(SepiaSearch sepiaSearch) {
SepiaSearchService sepiaSearchService = init();
2020-10-09 18:22:05 +02:00
SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
String startDate = null;
2020-10-17 18:50:20 +02:00
if (sepiaSearch.getStartDate() != null) {
2020-10-09 18:22:05 +02:00
startDate = fmtOut.format(sepiaSearch.getStartDate());
}
2020-10-09 14:54:01 +02:00
Call<VideoData> videoDataCall = sepiaSearchService.getVideos(
sepiaSearch.getStart(),
2020-10-09 18:22:05 +02:00
sepiaSearch.getCount(),
2020-10-09 14:54:01 +02:00
sepiaSearch.getSearch(),
2020-10-09 18:22:05 +02:00
sepiaSearch.getDurationMin(),
2020-10-09 14:54:01 +02:00
sepiaSearch.getDurationMax(),
2020-10-09 18:22:05 +02:00
startDate,
2020-10-09 14:54:01 +02:00
sepiaSearch.getBoostLanguages(),
sepiaSearch.getCategoryOneOf(),
sepiaSearch.getLicenceOneOf(),
sepiaSearch.getTagsOneOf(),
sepiaSearch.getTagsAllOf(),
sepiaSearch.isNsfw(),
sepiaSearch.getSort());
2020-10-09 18:22:05 +02:00
2020-10-09 14:54:01 +02:00
try {
Response<VideoData> response = videoDataCall.execute();
if (response.isSuccessful() && response.body() != null) {
2020-10-17 18:50:20 +02:00
return response.body();
2020-10-09 14:54:01 +02:00
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
2020-10-09 10:19:46 +02:00
}