Add logic

This commit is contained in:
Thomas 2020-09-16 16:21:03 +02:00
parent d6c662434d
commit 106bacb4b1
4 changed files with 344 additions and 102 deletions

View File

@ -40,7 +40,7 @@ public class APIResponse {
private Error error = null; private Error error = null;
private String since_id, max_id; private String since_id, max_id;
private List<PlaylistElement> playlistForVideos; private List<PlaylistElement> playlistForVideos;
private Instance instance; private List<Instance> instances;
private String stringData; private String stringData;
private int statusCode; private int statusCode;
@ -130,13 +130,6 @@ public class APIResponse {
} }
public Instance getInstance() {
return instance;
}
public void setInstance(Instance instance) {
this.instance = instance;
}
public List<Status> getStatuses() { public List<Status> getStatuses() {
return statuses; return statuses;
@ -169,4 +162,12 @@ public class APIResponse {
public void setRelationships(List<Relationship> relationships) { public void setRelationships(List<Relationship> relationships) {
this.relationships = relationships; this.relationships = relationships;
} }
public List<Instance> getInstances() {
return instances;
}
public void setInstances(List<Instance> instances) {
this.instances = instances;
}
} }

View File

@ -50,6 +50,7 @@ import app.fedilab.fedilabtube.client.entities.ChannelCreation;
import app.fedilab.fedilabtube.client.entities.Error; import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.client.entities.Instance; import app.fedilab.fedilabtube.client.entities.Instance;
import app.fedilab.fedilabtube.client.entities.InstanceNodeInfo; import app.fedilab.fedilabtube.client.entities.InstanceNodeInfo;
import app.fedilab.fedilabtube.client.entities.InstanceParams;
import app.fedilab.fedilabtube.client.entities.NodeInfo; import app.fedilab.fedilabtube.client.entities.NodeInfo;
import app.fedilab.fedilabtube.client.entities.Peertube; import app.fedilab.fedilabtube.client.entities.Peertube;
import app.fedilab.fedilabtube.client.entities.PeertubeAccountNotification; import app.fedilab.fedilabtube.client.entities.PeertubeAccountNotification;
@ -589,11 +590,41 @@ public class PeertubeAPI {
* Get info on the current Instance *synchronously* * Get info on the current Instance *synchronously*
* @return APIResponse * @return APIResponse
*/ */
public APIResponse getInstance() { public APIResponse getInstances(InstanceParams instanceParams) {
LinkedHashMap<String, String> params = new LinkedHashMap<>();
params.put("start", "0");
params.put("count", "250");
params.put("healthy", "true");
params.put("signup", "true");
if (instanceParams != null) {
if (instanceParams.getCategoriesOr() != null && instanceParams.getCategoriesOr().size() > 0) {
StringBuilder parameters = new StringBuilder();
parameters.append("[]&");
for (Integer category : instanceParams.getCategoriesOr())
parameters.append("categoriesOr=").append(category).append("&");
String strParam = parameters.toString();
strParam = strParam.substring(0, strParam.length() - 1);
params.put("categoriesOr[]", strParam);
}
if (instanceParams.getLanguagesOr() != null && instanceParams.getLanguagesOr().size() > 0) {
StringBuilder parameters = new StringBuilder();
parameters.append("[]&");
for (String language : instanceParams.getLanguagesOr())
parameters.append("languagesOr=").append(language).append("&");
String strParam = parameters.toString();
strParam = strParam.substring(0, strParam.length() - 1);
params.put("languagesOr[]", strParam);
}
params.put("minUserQuota", instanceParams.getMinUserQuota());
params.put("nsfwPolicy", instanceParams.getNsfwPolicy());
}
try { try {
String response = new HttpsConnection(context).get(getAbsoluteUrl("/instance"), 30, null, prefKeyOauthTokenT); String response = new HttpsConnection(context).get("https://instances.joinpeertube.org/api/v1/instances", 30, params, null);
Instance instanceEntity = parseInstance(new JSONObject(response)); JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
apiResponse.setInstance(instanceEntity); List<Instance> instances = parseInstances(jsonArray);
apiResponse.setInstances(instances);
} catch (HttpsConnection.HttpsConnectionException e) { } catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e); setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
@ -2024,6 +2055,30 @@ public class PeertubeAPI {
return peertubes; return peertubes;
} }
/**
* Parse json array response for instances
*
* @param jsonArray JSONArray
* @return List<Instance>
*/
private List<Instance> parseInstances(JSONArray jsonArray) {
List<Instance> instances = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length()) {
JSONObject resobj = jsonArray.getJSONObject(i);
Instance instance = parseInstance(resobj);
i++;
instances.add(instance);
}
} catch (JSONException e) {
setDefaultError(e);
}
return instances;
}
/** /**
* Parse json response an unique instance * Parse json response an unique instance
* *
@ -2034,11 +2089,48 @@ public class PeertubeAPI {
Instance instance = new Instance(); Instance instance = new Instance();
try { try {
instance.setUri(resobj.getString("uri")); instance.setAutoBlacklistUserVideosEnabled(resobj.getBoolean("autoBlacklistUserVideosEnabled"));
instance.setTitle(resobj.getString("title")); if (resobj.has("categories")) {
instance.setDescription(resobj.getString("description")); JSONArray jsonArray = resobj.getJSONArray("categories");
instance.setEmail(resobj.getString("email")); LinkedHashMap<Integer, Integer> categories = new LinkedHashMap<>();
int i = 0;
while (i < jsonArray.length()) {
categories.put(i, jsonArray.getInt(i));
i++;
}
instance.setCategories(categories);
}
instance.setCountry(resobj.getString("country"));
instance.setCreatedAt(Helper.stringToDate(context, resobj.getString("createdAt")));
instance.setDefaultNSFWPolicy(resobj.getString("defaultNSFWPolicy"));
instance.setHealth(resobj.getInt("health"));
instance.setId(resobj.getString("id"));
instance.setHost(resobj.getString("host"));
instance.setName(resobj.getString("name"));
instance.setVersion(resobj.getString("version")); instance.setVersion(resobj.getString("version"));
instance.setShortDescription(resobj.getString("shortDescription"));
instance.setNSFW(resobj.getBoolean("isNSFW"));
instance.setSignupAllowed(resobj.getBoolean("signupAllowed"));
instance.setSupportsIPv6(resobj.getBoolean("supportsIPv6"));
instance.setTotalInstanceFollowers(resobj.getInt("totalInstanceFollowers"));
instance.setTotalInstanceFollowing(resobj.getInt("totalInstanceFollowing"));
instance.setTotalLocalVideos(resobj.getInt("totalLocalVideos"));
instance.setTotalUsers(resobj.getInt("totalUsers"));
instance.setTotalVideos(resobj.getInt("totalVideos"));
instance.setUserVideoQuota(resobj.getString("userVideoQuota"));
if (resobj.has("languages")) {
JSONArray jsonArray = resobj.getJSONArray("languages");
LinkedHashMap<Integer, String> languages = new LinkedHashMap<>();
int i = 0;
while (i < jsonArray.length()) {
languages.put(i, jsonArray.getString(i));
i++;
}
instance.setLanguages(languages);
}
} catch (JSONException e) { } catch (JSONException e) {
setDefaultError(e); setDefaultError(e);
} }

View File

@ -14,57 +14,177 @@ package app.fedilab.fedilabtube.client.entities;
* You should have received a copy of the GNU General Public License along with TubeLab; if not, * You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import java.util.HashMap; import java.util.Date;
import java.util.LinkedHashMap;
@SuppressWarnings("unused")
public class Instance { public class Instance {
private String uri; private boolean autoBlacklistUserVideosEnabled;
private String title; private LinkedHashMap<Integer, Integer> categories;
private String description; private String country;
private String email; private Date createdAt;
private String defaultNSFWPolicy;
private int health;
private String host;
private String id;
private LinkedHashMap<Integer, String> languages;
private String name;
private String shortDescription;
private boolean signupAllowed;
private boolean supportsIPv6;
private int totalInstanceFollowers;
private int totalInstanceFollowing;
private int totalLocalVideos;
private int totalUsers;
private int totalVideos;
private String userVideoQuota;
private String version; private String version;
private boolean registration; private boolean isNSFW;
private boolean approval_required;
private Account contactAccount;
private int userCount;
private int statusCount;
private int domainCount;
private String thumbnail;
private HashMap<String, Integer> poll_limits;
public String getUri() { public boolean isAutoBlacklistUserVideosEnabled() {
return uri; return autoBlacklistUserVideosEnabled;
} }
public void setUri(String uri) { public void setAutoBlacklistUserVideosEnabled(boolean autoBlacklistUserVideosEnabled) {
this.uri = uri; this.autoBlacklistUserVideosEnabled = autoBlacklistUserVideosEnabled;
} }
public String getTitle() { public LinkedHashMap<Integer, Integer> getCategories() {
return title; return categories;
} }
public void setTitle(String title) { public void setCategories(LinkedHashMap<Integer, Integer> categories) {
this.title = title; this.categories = categories;
} }
public String getDescription() { public String getCountry() {
return description; return country;
} }
public void setDescription(String description) { public void setCountry(String country) {
this.description = description; this.country = country;
} }
public String getEmail() { public Date getCreatedAt() {
return email; return createdAt;
} }
public void setEmail(String email) { public void setCreatedAt(Date createdAt) {
this.email = email; this.createdAt = createdAt;
}
public String getDefaultNSFWPolicy() {
return defaultNSFWPolicy;
}
public void setDefaultNSFWPolicy(String defaultNSFWPolicy) {
this.defaultNSFWPolicy = defaultNSFWPolicy;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public LinkedHashMap<Integer, String> getLanguages() {
return languages;
}
public void setLanguages(LinkedHashMap<Integer, String> languages) {
this.languages = languages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public boolean isSignupAllowed() {
return signupAllowed;
}
public void setSignupAllowed(boolean signupAllowed) {
this.signupAllowed = signupAllowed;
}
public boolean isSupportsIPv6() {
return supportsIPv6;
}
public void setSupportsIPv6(boolean supportsIPv6) {
this.supportsIPv6 = supportsIPv6;
}
public int getTotalInstanceFollowers() {
return totalInstanceFollowers;
}
public void setTotalInstanceFollowers(int totalInstanceFollowers) {
this.totalInstanceFollowers = totalInstanceFollowers;
}
public int getTotalInstanceFollowing() {
return totalInstanceFollowing;
}
public void setTotalInstanceFollowing(int totalInstanceFollowing) {
this.totalInstanceFollowing = totalInstanceFollowing;
}
public int getTotalLocalVideos() {
return totalLocalVideos;
}
public void setTotalLocalVideos(int totalLocalVideos) {
this.totalLocalVideos = totalLocalVideos;
}
public int getTotalUsers() {
return totalUsers;
}
public void setTotalUsers(int totalUsers) {
this.totalUsers = totalUsers;
}
public int getTotalVideos() {
return totalVideos;
}
public void setTotalVideos(int totalVideos) {
this.totalVideos = totalVideos;
} }
public String getVersion() { public String getVersion() {
@ -75,67 +195,19 @@ public class Instance {
this.version = version; this.version = version;
} }
public HashMap<String, Integer> getPoll_limits() { public boolean isNSFW() {
return poll_limits; return isNSFW;
} }
public void setPoll_limits(HashMap<String, Integer> poll_limits) { public void setNSFW(boolean NSFW) {
this.poll_limits = poll_limits; isNSFW = NSFW;
} }
public boolean isRegistration() { public String getUserVideoQuota() {
return registration; return userVideoQuota;
} }
public void setRegistration(boolean registration) { public void setUserVideoQuota(String userVideoQuota) {
this.registration = registration; this.userVideoQuota = userVideoQuota;
}
public boolean isApproval_required() {
return approval_required;
}
public void setApproval_required(boolean approval_required) {
this.approval_required = approval_required;
}
public Account getContactAccount() {
return contactAccount;
}
public void setContactAccount(Account contactAccount) {
this.contactAccount = contactAccount;
}
public int getUserCount() {
return userCount;
}
public void setUserCount(int userCount) {
this.userCount = userCount;
}
public int getStatusCount() {
return statusCount;
}
public void setStatusCount(int statusCount) {
this.statusCount = statusCount;
}
public int getDomainCount() {
return domainCount;
}
public void setDomainCount(int domainCount) {
this.domainCount = domainCount;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
} }
} }

View File

@ -0,0 +1,77 @@
package app.fedilab.fedilabtube.client.entities;
/* 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>. */
import java.util.List;
public class InstanceParams {
private boolean healthy = true;
private boolean signup = true;
private List<Integer> categoriesOr;
private String nsfwPolicy = "do_not_list";
private List<String> languagesOr;
private String minUserQuota = "5000000000";
public boolean isHealthy() {
return healthy;
}
public void setHealthy(boolean healthy) {
this.healthy = healthy;
}
public boolean isSignup() {
return signup;
}
public void setSignup(boolean signup) {
this.signup = signup;
}
public List<Integer> getCategoriesOr() {
return categoriesOr;
}
public void setCategoriesOr(List<Integer> categoriesOr) {
this.categoriesOr = categoriesOr;
}
public String getNsfwPolicy() {
return nsfwPolicy;
}
public void setNsfwPolicy(String nsfwPolicy) {
this.nsfwPolicy = nsfwPolicy;
}
public String getMinUserQuota() {
return minUserQuota;
}
public void setMinUserQuota(String minUserQuota) {
this.minUserQuota = minUserQuota;
}
public List<String> getLanguagesOr() {
return languagesOr;
}
public void setLanguagesOr(List<String> languagesOr) {
this.languagesOr = languagesOr;
}
}