Search in sources :

Example 1 with ClaimSearchCacheValue

use of com.odysee.app.model.ClaimSearchCacheValue in project odysee-android by OdyseeTeam.

the class Lbry method claimSearch.

public static List<Claim> claimSearch(Map<String, Object> options, String connectionString) throws ApiCallException {
    if (claimSearchCache.containsKey(options)) {
        ClaimSearchCacheValue value = claimSearchCache.get(options);
        if (!value.isExpired(TTL_CLAIM_SEARCH_VALUE)) {
            return claimSearchCache.get(options).getClaims();
        }
    }
    List<Claim> claims = new ArrayList<>();
    try {
        JSONObject result = (JSONObject) parseResponse(apiCall(METHOD_CLAIM_SEARCH, options, connectionString));
        JSONArray items = result.getJSONArray("items");
        if (items != null) {
            for (int i = 0; i < items.length(); i++) {
                Claim claim = Claim.fromJSONObject(items.getJSONObject(i));
                // audio and video, even for reposted claims
                if (!Claim.TYPE_COLLECTION.equalsIgnoreCase(claim.getValueType()) && !claim.hasSource() && claim.getSigningChannel() != null) {
                    String urlLivestream = String.format("https://api.live.odysee.com/v1/odysee/live/%s", claim.getSigningChannel().getClaimId());
                    Request.Builder builder = new Request.Builder().url(urlLivestream);
                    Request request = builder.build();
                    OkHttpClient client = new OkHttpClient.Builder().build();
                    try {
                        Response resp = client.newCall(request).execute();
                        String responseString = resp.body().string();
                        resp.close();
                        JSONObject json = new JSONObject(responseString);
                        if (resp.code() >= 200 && resp.code() < 300) {
                            if (!json.isNull("data") && (json.has("success") && json.getBoolean("success"))) {
                                JSONObject jsonData = (JSONObject) json.get("data");
                                if (jsonData.has("live")) {
                                    claim.setLive(jsonData.getBoolean("live"));
                                    claim.setLivestreamUrl(jsonData.getString("url"));
                                }
                            }
                        }
                    } catch (IOException | JSONException e) {
                        e.printStackTrace();
                    }
                }
                // For now, only claims which are audio, videos, playlists or livestreaming right now can be viewed
                if (Arrays.asList(Claim.TYPE_REPOST, Claim.TYPE_COLLECTION, Claim.TYPE_CHANNEL).contains(claim.getValueType().toLowerCase()) || (!claim.hasSource() && claim.isLive()) || (claim.hasSource() && (claim.getMediaType().contains("video") || claim.getMediaType().contains("audio")))) {
                    claims.add(claim);
                }
                addClaimToCache(claim);
            }
        }
        claimSearchCache.put(options, new ClaimSearchCacheValue(claims, System.currentTimeMillis()));
    } catch (LbryRequestException | LbryResponseException | JSONException ex) {
        throw new ApiCallException("Could not execute resolve call", ex);
    }
    return claims;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ApiCallException(com.odysee.app.exceptions.ApiCallException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Request(okhttp3.Request) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) IOException(java.io.IOException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) Response(okhttp3.Response) JSONObject(org.json.JSONObject) ClaimSearchCacheValue(com.odysee.app.model.ClaimSearchCacheValue) Claim(com.odysee.app.model.Claim)

Aggregations

ApiCallException (com.odysee.app.exceptions.ApiCallException)1 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)1 LbryResponseException (com.odysee.app.exceptions.LbryResponseException)1 Claim (com.odysee.app.model.Claim)1 ClaimSearchCacheValue (com.odysee.app.model.ClaimSearchCacheValue)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1