Search in sources :

Example 6 with LbryResponseException

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

the class Lbry method fileList.

public static List<LbryFile> fileList(String claimId, boolean downloads, int page, int pageSize) throws ApiCallException {
    List<LbryFile> files = new ArrayList<>();
    Map<String, Object> params = new HashMap<>();
    if (!Helper.isNullOrEmpty(claimId)) {
        params.put("claim_id", claimId);
    }
    if (downloads) {
        params.put("download_path", null);
        params.put("comparison", "ne");
    }
    if (page > 0) {
        params.put("page", page);
    }
    if (pageSize > 0) {
        params.put("page_size", pageSize);
    }
    try {
        JSONObject result = (JSONObject) parseResponse(apiCall(METHOD_FILE_LIST, params));
        JSONArray items = result.getJSONArray("items");
        for (int i = 0; i < items.length(); i++) {
            JSONObject fileObject = items.getJSONObject(i);
            LbryFile file = LbryFile.fromJSONObject(fileObject);
            files.add(file);
            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 | JSONException ex) {
        throw new ApiCallException("Could not execute resolve call", ex);
    }
    return files;
}
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) ClaimCacheKey(com.odysee.app.model.ClaimCacheKey) JSONObject(org.json.JSONObject) LbryFile(com.odysee.app.model.LbryFile) JSONObject(org.json.JSONObject)

Example 7 with LbryResponseException

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

the class UploadImageTask method doInBackground.

protected String doInBackground(Void... params) {
    String thumbnailUrl = null;
    try {
        File file = new File(filePath);
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf('.');
        String extension = "jpg";
        if (dotIndex > -1) {
            extension = fileName.substring(dotIndex + 1);
        }
        String fileType = String.format("image/%s", extension);
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("name", Helper.makeid(24)).addFormDataPart("file", fileName, RequestBody.create(file, MediaType.parse(fileType))).build();
        Request request = new Request.Builder().url("https://spee.ch/api/claim/publish").post(body).build();
        OkHttpClient client = new OkHttpClient.Builder().writeTimeout(300, TimeUnit.SECONDS).readTimeout(300, TimeUnit.SECONDS).build();
        Response response = client.newCall(request).execute();
        JSONObject json = new JSONObject(response.body().string());
        if (json.has("success") && Helper.getJSONBoolean("success", false, json)) {
            JSONObject data = json.getJSONObject("data");
            String url = Helper.getJSONString("url", null, data);
            if (Helper.isNullOrEmpty(url)) {
                throw new LbryResponseException("Invalid thumbnail url returned after upload.");
            }
            thumbnailUrl = String.format("%s.%s", url, extension);
        } else if (json.has("error") || json.has("message")) {
            JSONObject error = Helper.getJSONObject("error", json);
            String message = null;
            if (error != null) {
                message = Helper.getJSONString("message", null, error);
            }
            if (Helper.isNullOrEmpty(message)) {
                message = Helper.getJSONString("message", null, json);
            }
            throw new LbryResponseException(Helper.isNullOrEmpty(message) ? "The image failed to upload." : message);
        }
    } catch (IOException | JSONException | LbryResponseException ex) {
        error = ex;
    }
    return thumbnailUrl;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) IOException(java.io.IOException) Response(okhttp3.Response) JSONObject(org.json.JSONObject) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 8 with LbryResponseException

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

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

the class CommentEnabled method call.

@Override
public Boolean call() {
    Map<String, Object> params = new HashMap<>(3);
    params.put("claim_id", channelId);
    params.put("channel_id", channelId);
    params.put("channel_name", channelName);
    try {
        JSONObject result = (JSONObject) Lbry.parseResponse(Comments.performRequest(Lbry.buildJsonParams(params), METHOD_COMMENT_LIST));
        if (result == null || result.has("error")) {
            return false;
        }
    } catch (LbryResponseException | IOException e) {
        Log.e(TAG, "Error while fetching comments", e);
        return false;
    }
    return true;
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONObject(org.json.JSONObject) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) IOException(java.io.IOException)

Example 10 with LbryResponseException

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

Aggregations

LbryResponseException (com.odysee.app.exceptions.LbryResponseException)17 JSONObject (org.json.JSONObject)15 JSONException (org.json.JSONException)12 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)10 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 ApiCallException (com.odysee.app.exceptions.ApiCallException)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)6 JSONArray (org.json.JSONArray)6 OkHttpClient (okhttp3.OkHttpClient)5 Request (okhttp3.Request)5 Response (okhttp3.Response)5 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