Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Sep 5, 2022
1 parent 332d5e2 commit e00f92b
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions app/src/main/java/org/nuclearfog/twidda/backend/api/Twitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1244,7 +1249,9 @@ public Metrics getTweetMetrics(long tweetId) throws TwitterException {
*/
public long uploadMedia(MediaUpdate mediaUpdate) throws TwitterException {
List<String> params = new ArrayList<>();
String state;
boolean enableChunk;
int retries = 0;
try {
// step 1 INIT
params.add("command=INIT");
Expand All @@ -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());
Expand All @@ -1287,17 +1293,15 @@ 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;

// step 4 STATUS
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();
Expand All @@ -1309,20 +1313,18 @@ 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");
String message = jsonError.getString("message");
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
}
}

Expand Down

0 comments on commit e00f92b

Please sign in to comment.