Search in sources :

Example 26 with ApiCallException

use of com.odysee.app.exceptions.ApiCallException in project odysee-android by OdyseeTeam.

the class Lbry method transactionList.

public static List<Transaction> transactionList(int page, int pageSize, String authToken) throws ApiCallException {
    List<Transaction> transactions = new ArrayList<>();
    Map<String, Object> params = new HashMap<>();
    if (page > 0) {
        params.put("page", page);
    }
    if (pageSize > 0) {
        params.put("page_size", pageSize);
    }
    if (authToken != "") {
        params.put("auth_token", authToken);
    }
    try {
        JSONObject result = (JSONObject) parseResponse(apiCall(METHOD_TRANSACTION_LIST, params, API_CONNECTION_STRING));
        JSONArray items = result.getJSONArray("items");
        for (int i = 0; i < items.length(); i++) {
            Transaction tx = Transaction.fromJSONObject(items.getJSONObject(i));
            transactions.add(tx);
        }
    } catch (LbryRequestException | LbryResponseException | JSONException ex) {
        throw new ApiCallException("Could not execute transaction_list call", ex);
    }
    return transactions;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) Transaction(com.odysee.app.model.Transaction) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Example 27 with ApiCallException

use of com.odysee.app.exceptions.ApiCallException 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)

Example 28 with ApiCallException

use of com.odysee.app.exceptions.ApiCallException in project odysee-android by OdyseeTeam.

the class Lbry method resolve.

public static List<Claim> resolve(List<String> urls, String connectionString) throws ApiCallException {
    List<Claim> claims = new ArrayList<>();
    Map<String, Object> params = new HashMap<>();
    params.put("urls", urls);
    try {
        JSONObject result = (JSONObject) parseResponse(apiCall(METHOD_RESOLVE, params, connectionString));
        Iterator<String> keys = result.keys();
        if (keys != null) {
            while (keys.hasNext()) {
                Claim claim = Claim.fromJSONObject(result.getJSONObject(keys.next()));
                claims.add(claim);
                addClaimToCache(claim);
            }
        }
    } catch (LbryRequestException | LbryResponseException | JSONException ex) {
        throw new ApiCallException("Could not execute resolve call", ex);
    }
    return claims;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) Claim(com.odysee.app.model.Claim)

Example 29 with ApiCallException

use of com.odysee.app.exceptions.ApiCallException in project odysee-android by OdyseeTeam.

the class ClaimListSupplier method get.

@Override
public List<Claim> get() {
    List<Claim> claims = null;
    try {
        Map<String, Object> options = new HashMap<>();
        if (types != null && types.size() > 0) {
            options.put("claim_type", types);
        }
        options.put("page", 1);
        options.put("page_size", 999);
        options.put("resolve", true);
        JSONObject result;
        if (authToken != null && !authToken.equals(""))
            result = (JSONObject) Lbry.authenticatedGenericApiCall(Lbry.METHOD_CLAIM_LIST, options, authToken);
        else
            result = (JSONObject) Lbry.genericApiCall(Lbry.METHOD_CLAIM_LIST, options);
        JSONArray items = result.getJSONArray("items");
        claims = new ArrayList<>();
        for (int i = 0; i < items.length(); i++) {
            claims.add(Claim.fromJSONObject(items.getJSONObject(i)));
        }
    } catch (ApiCallException | JSONException ex) {
        ex.printStackTrace();
    }
    return claims;
}
Also used : HashMap(java.util.HashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) Claim(com.odysee.app.model.Claim)

Example 30 with ApiCallException

use of com.odysee.app.exceptions.ApiCallException in project odysee-android by OdyseeTeam.

the class AbandonStreamTask method doInBackground.

public Boolean doInBackground(Void... params) {
    successfulClaimIds = new ArrayList<>();
    failedClaimIds = new ArrayList<>();
    failedExceptions = new ArrayList<>();
    for (String claimId : claimIds) {
        try {
            Map<String, Object> options = new HashMap<>();
            options.put("claim_id", claimId);
            options.put("blocking", true);
            JSONObject result = (JSONObject) Lbry.genericApiCall(Lbry.METHOD_STREAM_ABANDON, options);
            successfulClaimIds.add(claimId);
        } catch (ApiCallException ex) {
            failedClaimIds.add(claimId);
            failedExceptions.add(ex);
        }
    }
    return true;
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONObject(org.json.JSONObject)

Aggregations

ApiCallException (com.odysee.app.exceptions.ApiCallException)31 JSONObject (org.json.JSONObject)28 HashMap (java.util.HashMap)22 JSONException (org.json.JSONException)17 Claim (com.odysee.app.model.Claim)10 JSONArray (org.json.JSONArray)10 ArrayList (java.util.ArrayList)8 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)7 LbryResponseException (com.odysee.app.exceptions.LbryResponseException)7 MainActivity (com.odysee.app.MainActivity)6 DecimalFormat (java.text.DecimalFormat)5 DecimalFormatSymbols (java.text.DecimalFormatSymbols)5 LinkedHashMap (java.util.LinkedHashMap)5 ExecutorService (java.util.concurrent.ExecutorService)5 AccountManager (android.accounts.AccountManager)4 Response (okhttp3.Response)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 View (android.view.View)3 TextView (android.widget.TextView)3 Subscription (com.odysee.app.model.lbryinc.Subscription)3