From e00f92b0e9e42735e3bbf6fff7cc7c497ad229b9 Mon Sep 17 00:00:00 2001 From: nuclearfog Date: Mon, 5 Sep 2022 16:14:32 +0200 Subject: [PATCH] code cleanup --- .../twidda/backend/api/Twitter.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/api/Twitter.java b/app/src/main/java/org/nuclearfog/twidda/backend/api/Twitter.java index db0c0b5782..7615687e49 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/api/Twitter.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/api/Twitter.java @@ -162,6 +162,11 @@ public class Twitter implements GlobalSettings.SettingsListener { */ private static final int CHUNK_MAX_BYTES = 1024 * 1024; + /** + * maximum polling request + */ + private static final int POLLING_MAX_RETRIES = 12; + private static Twitter instance; private static boolean notifySettingsChange = false; @@ -1244,7 +1249,9 @@ public Metrics getTweetMetrics(long tweetId) throws TwitterException { */ public long uploadMedia(MediaUpdate mediaUpdate) throws TwitterException { List params = new ArrayList<>(); + String state; boolean enableChunk; + int retries = 0; try { // step 1 INIT params.add("command=INIT"); @@ -1262,7 +1269,6 @@ public long uploadMedia(MediaUpdate mediaUpdate) throws TwitterException { } Response response = post(MEDIA_UPLOAD, params); ResponseBody body = response.body(); - if (response.code() < 200 || response.code() >= 300 || body == null) throw new TwitterException(response); JSONObject jsonResponse = new JSONObject(body.string()); @@ -1287,7 +1293,7 @@ public long uploadMedia(MediaUpdate mediaUpdate) throws TwitterException { response = post(MEDIA_UPLOAD, params); if (response.code() < 200 || response.code() >= 300) throw new TwitterException(response); - // skip step 4 if chunking isn#t enabled + // skip step 4 if chunking isn't enabled if (!enableChunk) return mediaId; @@ -1295,9 +1301,7 @@ public long uploadMedia(MediaUpdate mediaUpdate) throws TwitterException { params.clear(); params.add("command=STATUS"); params.add("media_id=" + mediaId); - - int retries = 0; - String state; + // poll media processing information frequently do { response = get(MEDIA_UPLOAD, params); body = response.body(); @@ -1309,8 +1313,7 @@ public long uploadMedia(MediaUpdate mediaUpdate) throws TwitterException { state = processingInfo.optString("state"); // wait until next polling Thread.sleep(retryAfter * 1000L); - } while (state.equals("in_progress") && ++retries <= 10); - + } while (state.equals("in_progress") && ++retries <= POLLING_MAX_RETRIES); // check if media processing was successfully if (!state.equals("succeeded")) { JSONObject jsonError = jsonResponse.getJSONObject("processing_info").getJSONObject("error"); @@ -1318,11 +1321,10 @@ public long uploadMedia(MediaUpdate mediaUpdate) throws TwitterException { throw new TwitterException(message); } return mediaId; - } catch (IOException err) { - err.printStackTrace(); - throw new TwitterException(err); - } catch (JSONException | InterruptedException err) { + } catch (IOException | JSONException err) { throw new TwitterException(err); + } catch (InterruptedException e) { + return -1L; //ignore } }