Search in sources :

Example 6 with LbryioRequestException

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

the class FetchRewardsSupplier method get.

@Override
public List<Reward> get() {
    List<Reward> rewards = new ArrayList<>();
    try {
        JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
        rewards = new ArrayList<>();
        if (results != null) {
            for (int i = 0; i < results.length(); i++) {
                rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
            }
        }
    } catch (LbryioResponseException | JSONException | LbryioRequestException e) {
        e.printStackTrace();
    }
    return rewards;
}
Also used : LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Reward(com.odysee.app.model.lbryinc.Reward) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 7 with LbryioRequestException

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

the class NotificationListSupplier method get.

@Override
public List<LbryNotification> get() {
    List<LbryNotification> notifications = new ArrayList<>();
    try {
        JSONArray array = (JSONArray) Lbryio.parseResponse(Lbryio.call("notification", "list", options, null));
        if (array != null) {
            for (int i = 0; i < array.length(); i++) {
                JSONObject item = array.getJSONObject(i);
                if (item.has("notification_parameters")) {
                    LbryNotification notification = new LbryNotification();
                    JSONObject notificationParams = item.getJSONObject("notification_parameters");
                    if (notificationParams.has("device")) {
                        JSONObject device = notificationParams.getJSONObject("device");
                        notification.setTitle(Helper.getJSONString("title", null, device));
                        notification.setDescription(Helper.getJSONString("text", null, device));
                        notification.setTargetUrl(Helper.getJSONString("target", null, device));
                    }
                    if (notificationParams.has("dynamic") && !notificationParams.isNull("dynamic")) {
                        JSONObject dynamic = notificationParams.getJSONObject("dynamic");
                        if (dynamic.has("comment_author") || dynamic.has("channel_thumbnail")) {
                            String url = null;
                            if (dynamic.has("comment_author"))
                                url = Helper.getJSONString("comment_author", null, dynamic);
                            else if (dynamic.has("channel_thumbnail"))
                                url = Helper.getJSONString("channel_thumbnail", null, dynamic);
                            notification.setAuthorThumbnailUrl(url);
                        }
                        if (dynamic.has("channelURI")) {
                            String channelUrl = Helper.getJSONString("channelURI", null, dynamic);
                            if (!Helper.isNullOrEmpty(channelUrl)) {
                                notification.setTargetUrl(channelUrl);
                            }
                        }
                        if (dynamic.has("claim_thumbnail")) {
                            String claimThumbnailUrl = Helper.getJSONString("claim_thumbnail", null, dynamic);
                            if (!Helper.isNullOrEmpty(claimThumbnailUrl)) {
                                notification.setClaimThumbnailUrl(claimThumbnailUrl);
                            }
                        }
                        if (dynamic.has("hash") && "comment".equalsIgnoreCase(Helper.getJSONString("notification_rule", null, item))) {
                            notification.setTargetUrl(String.format("%s?comment_hash=%s", notification.getTargetUrl(), dynamic.getString("hash")));
                        }
                    }
                    notification.setRule(Helper.getJSONString("notification_rule", null, item));
                    notification.setRemoteId(Helper.getJSONLong("id", 0, item));
                    notification.setRead(Helper.getJSONBoolean("is_read", false, item));
                    notification.setSeen(Helper.getJSONBoolean("is_seen", false, item));
                    try {
                        SimpleDateFormat dateFormat = new SimpleDateFormat(Helper.ISO_DATE_FORMAT_JSON, Locale.US);
                        notification.setTimestamp(dateFormat.parse(Helper.getJSONString("created_at", dateFormat.format(new Date()), item)));
                    } catch (ParseException ex) {
                        notification.setTimestamp(new Date());
                    }
                    if (notification.getRemoteId() > 0 && !Helper.isNullOrEmpty(notification.getDescription())) {
                        notifications.add(notification);
                    }
                }
            }
        }
    } catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException | IllegalStateException ex) {
        ex.printStackTrace();
        return null;
    }
    return notifications;
}
Also used : LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryNotification(com.odysee.app.model.lbryinc.LbryNotification) Date(java.util.Date) JSONObject(org.json.JSONObject) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 8 with LbryioRequestException

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

the class FetchInviteStatusTask method doInBackground.

protected List<Invitee> doInBackground(Void... params) {
    List<Invitee> invitees = null;
    try {
        JSONObject status = (JSONObject) Lbryio.parseResponse(Lbryio.call("user", "invite_status", null, null));
        JSONArray inviteesArray = status.getJSONArray("invitees");
        invitees = new ArrayList<>();
        for (int i = 0; i < inviteesArray.length(); i++) {
            JSONObject inviteeObject = inviteesArray.getJSONObject(i);
            Invitee invitee = new Invitee();
            invitee.setEmail(Helper.getJSONString("email", null, inviteeObject));
            invitee.setInviteRewardClaimable(Helper.getJSONBoolean("invite_reward_claimable", false, inviteeObject));
            invitee.setInviteRewardClaimed(Helper.getJSONBoolean("invite_reward_claimed", false, inviteeObject));
            if (!Helper.isNullOrEmpty(invitee.getEmail())) {
                invitees.add(invitee);
            }
        }
    } catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException ex) {
        error = ex;
    }
    return invitees;
}
Also used : Invitee(com.odysee.app.model.lbryinc.Invitee) JSONObject(org.json.JSONObject) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 9 with LbryioRequestException

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

the class FileViewFragment method logFileView.

private void logFileView(String url, long timeToStart) {
    Claim actualClaim = collectionClaimItem != null ? collectionClaimItem : fileClaim;
    if (actualClaim != null) {
        String authToken = Lbryio.AUTH_TOKEN;
        Map<String, String> options = new HashMap<>();
        options.put("uri", url);
        options.put("claim_id", actualClaim.getClaimId());
        options.put("outpoint", String.format("%s:%d", actualClaim.getTxid(), actualClaim.getNout()));
        if (timeToStart > 0) {
            options.put("time_to_start", String.valueOf(timeToStart));
        }
        if (!Helper.isNullOrEmpty(authToken)) {
            options.put("auth_token", authToken);
        }
        Activity activity = getActivity();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            Supplier<Boolean> s = new Supplier<Boolean>() {

                @Override
                public Boolean get() {
                    try {
                        Lbryio.call("file", "view", options, null).close();
                        return true;
                    } catch (LbryioRequestException | LbryioResponseException ex) {
                        return false;
                    }
                }
            };
            CompletableFuture<Boolean> cf = CompletableFuture.supplyAsync(s);
            cf.whenComplete((result, ex) -> {
                if (result) {
                    if (activity != null) {
                        activity.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                claimEligibleRewards();
                            }
                        });
                    }
                    if (ex != null) {
                        if (activity != null) {
                            activity.runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    ((MainActivity) activity).showError(ex.getMessage());
                                }
                            });
                        }
                    }
                }
            });
        } else {
            Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        Lbryio.call("file", "view", options, null).close();
                        if (activity != null) {
                            activity.runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    claimEligibleRewards();
                                }
                            });
                        }
                    } catch (LbryioRequestException | LbryioResponseException ex) {
                        if (activity != null) {
                            activity.runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    ((MainActivity) activity).showError(ex.getMessage());
                                }
                            });
                        }
                    }
                }
            });
            t.start();
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) Activity(android.app.Activity) MainActivity(com.odysee.app.MainActivity) AnyThread(androidx.annotation.AnyThread) Supplier(java.util.function.Supplier) TrackSelectionOverride(com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride) Claim(com.odysee.app.model.Claim) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Example 10 with LbryioRequestException

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

the class UserExistsWithPassword method call.

@Override
public Boolean call() throws Exception {
    Map<String, String> options = new HashMap<>();
    options.put("email", email);
    try {
        Response response = Lbryio.call("user", "exists", options, Helper.METHOD_POST, ctx);
        if (response.isSuccessful()) {
            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                String responseString = responseBody.string();
                response.close();
                JSONObject jsonData = new JSONObject(responseString);
                if (jsonData.has("data"))
                    return (jsonData.getJSONObject("data").getBoolean("has_password"));
            } else {
                response.close();
            }
        }
        return false;
    } catch (LbryioRequestException | LbryioResponseException e) {
        Log.e(TAG, e.toString());
        return false;
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ResponseBody(okhttp3.ResponseBody)

Aggregations

LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)17 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)16 JSONObject (org.json.JSONObject)13 HashMap (java.util.HashMap)11 JSONException (org.json.JSONException)7 JSONArray (org.json.JSONArray)5 AccountManager (android.accounts.AccountManager)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 Supplier (java.util.function.Supplier)4 Response (okhttp3.Response)4 Account (android.accounts.Account)3 Activity (android.app.Activity)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 MainActivity (com.odysee.app.MainActivity)3 LbryNotification (com.odysee.app.model.lbryinc.LbryNotification)3 Reward (com.odysee.app.model.lbryinc.Reward)3 FetchRewardsSupplier (com.odysee.app.supplier.FetchRewardsSupplier)3 Context (android.content.Context)2 Color (android.graphics.Color)2