Search in sources :

Example 6 with LbryRequestException

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

the class FileViewFragment method getStreamingUrlAndInitializePlayer.

private void getStreamingUrlAndInitializePlayer(Claim theClaim) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> {
        try {
            // Get the streaming URL
            Map<String, Object> params = new HashMap<>();
            params.put("uri", theClaim.getPermanentUrl());
            JSONObject result = (JSONObject) Lbry.parseResponse(Lbry.apiCall(Lbry.METHOD_GET, params));
            String sourceUrl = (String) result.get("streaming_url");
            currentMediaSourceUrl = sourceUrl;
            // Get the stream type
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(sourceUrl).head().build();
            try (Response response = client.newCall(request).execute()) {
                String contentType = response.header("Content-Type");
                // m3u8
                MainActivity.videoIsTranscoded = contentType.equals("application/x-mpegurl");
            }
            new Handler(Looper.getMainLooper()).post(() -> initializePlayer(sourceUrl));
        } catch (LbryRequestException | LbryResponseException | JSONException | IOException ex) {
            // TODO: How does error handling work here
            ex.printStackTrace();
        }
    });
    executor.shutdown();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Request(okhttp3.Request) WebResourceRequest(android.webkit.WebResourceRequest) Handler(android.os.Handler) AbandonHandler(com.odysee.app.tasks.claim.AbandonHandler) CommentListHandler(com.odysee.app.tasks.CommentListHandler) ClaimSearchResultHandler(com.odysee.app.tasks.claim.ClaimSearchResultHandler) GenericTaskHandler(com.odysee.app.tasks.GenericTaskHandler) ClaimListResultHandler(com.odysee.app.tasks.claim.ClaimListResultHandler) 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) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) JSONObject(org.json.JSONObject)

Example 7 with LbryRequestException

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

the class Lbry method directApiCall.

public static Object directApiCall(String method, String authToken) throws ApiCallException {
    Map<String, Object> params = new HashMap<>();
    params.put("auth_token", authToken);
    Object response = null;
    try {
        response = parseResponse(apiCall(method, params, API_CONNECTION_STRING));
    } catch (LbryRequestException | LbryResponseException ex) {
        throw new ApiCallException(String.format("Could not execute %s call: %s", method, ex.getMessage()), ex);
    }
    return response;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONObject(org.json.JSONObject) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException)

Example 8 with LbryRequestException

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

the class Lbry method get.

public static LbryFile get(boolean saveFile) throws ApiCallException {
    LbryFile file = null;
    Map<String, Object> params = new HashMap<>();
    params.put("save_file", saveFile);
    try {
        JSONObject result = (JSONObject) parseResponse(apiCall(METHOD_GET, params));
        file = LbryFile.fromJSONObject(result);
        if (file != null) {
            String fileClaimId = file.getClaimId();
            if (!Helper.isNullOrEmpty(fileClaimId)) {
                ClaimCacheKey key = new ClaimCacheKey();
                key.setClaimId(fileClaimId);
                if (claimCache.containsKey(key)) {
                    claimCache.get(key).setFile(file);
                }
            }
        }
    } catch (LbryRequestException | LbryResponseException ex) {
        throw new ApiCallException("Could not execute resolve call", ex);
    }
    return file;
}
Also used : ClaimCacheKey(com.odysee.app.model.ClaimCacheKey) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) LbryFile(com.odysee.app.model.LbryFile) JSONObject(org.json.JSONObject) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException)

Example 9 with LbryRequestException

use of com.odysee.app.exceptions.LbryRequestException 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 10 with LbryRequestException

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

LbryRequestException (com.odysee.app.exceptions.LbryRequestException)11 LbryResponseException (com.odysee.app.exceptions.LbryResponseException)10 JSONObject (org.json.JSONObject)9 JSONException (org.json.JSONException)8 ApiCallException (com.odysee.app.exceptions.ApiCallException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 IOException (java.io.IOException)5 OkHttpClient (okhttp3.OkHttpClient)5 Request (okhttp3.Request)5 JSONArray (org.json.JSONArray)5 Response (okhttp3.Response)4 Claim (com.odysee.app.model.Claim)3 Uri (android.net.Uri)2 ClaimCacheKey (com.odysee.app.model.ClaimCacheKey)2 LbryFile (com.odysee.app.model.LbryFile)2 ResponseBody (okhttp3.ResponseBody)2 Handler (android.os.Handler)1 WebResourceRequest (android.webkit.WebResourceRequest)1