-Mega API fatal error codes
This commit is contained in:
tonikelope 2017-05-23 22:54:14 +02:00
parent fd66676587
commit a3e4831bd7
8 changed files with 100 additions and 107 deletions

View File

@ -19,6 +19,7 @@ import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import static java.util.concurrent.Executors.newCachedThreadPool;
@ -800,6 +801,8 @@ public final class Download implements Transference, Runnable, SecureSingleThrea
if (!_provision_ok) {
_status_error = true;
getView().hideAllExceptStatus();
if (_fatal_error != null) {
@ -1192,58 +1195,66 @@ public final class Download implements Transference, Runnable, SecureSingleThrea
error_code = parseInt(ex.getMessage());
switch (error_code) {
case -2:
emergencyStopDownloader("Mega link is not valid! " + MiscTools.truncateText(link, 80));
break;
if (Arrays.asList(FATAL_ERROR_API_CODES).contains(error_code)) {
case -14:
emergencyStopDownloader("Mega link is not valid! " + MiscTools.truncateText(link, 80));
break;
switch (error_code) {
case 22:
emergencyStopDownloader("MegaCrypter link is not valid! " + MiscTools.truncateText(link, 80));
break;
case -2:
emergencyStopDownloader("Mega link is not valid! " + MiscTools.truncateText(link, 80));
break;
case 23:
emergencyStopDownloader("MegaCrypter link is blocked! " + MiscTools.truncateText(link, 80));
break;
case -14:
emergencyStopDownloader("Mega link is not valid! " + MiscTools.truncateText(link, 80));
break;
case 24:
emergencyStopDownloader("MegaCrypter link has expired! " + MiscTools.truncateText(link, 80));
break;
case 22:
emergencyStopDownloader("MegaCrypter link is not valid! " + MiscTools.truncateText(link, 80));
break;
case 25:
emergencyStopDownloader("MegaCrypter link pass error! " + MiscTools.truncateText(link, 80));
break;
case 23:
emergencyStopDownloader("MegaCrypter link is blocked! " + MiscTools.truncateText(link, 80));
break;
default:
case 24:
emergencyStopDownloader("MegaCrypter link has expired! " + MiscTools.truncateText(link, 80));
break;
if (!retry_request) {
case 25:
emergencyStopDownloader("MegaCrypter link pass error! " + MiscTools.truncateText(link, 80));
break;
throw ex;
default:
emergencyStopDownloader("MEGA/MC API FATAL ERROR: " + ex.getMessage() + " " + MiscTools.truncateText(link, 80));
break;
}
} else {
if (!retry_request) {
throw ex;
}
_retrying_request = true;
swingReflectionInvoke("setEnabled", getMain_panel().getView().getNew_download_menu(), true);
swingReflectionInvoke("setVisible", getView().getStop_button(), true);
swingReflectionInvoke("setText", getView().getStop_button(), "CANCEL RETRY");
for (long i = getWaitTimeExpBackOff(retry++); i > 0 && !_exit; i--) {
if (error_code == -18) {
getView().printStatusError("File temporarily unavailable! (Retrying in " + i + " secs...)");
} else {
getView().printStatusError("Mega/MC APIException error " + ex.getMessage() + " (Retrying in " + i + " secs...)");
}
_retrying_request = true;
swingReflectionInvoke("setEnabled", getMain_panel().getView().getNew_download_menu(), true);
swingReflectionInvoke("setVisible", getView().getStop_button(), true);
swingReflectionInvoke("setText", getView().getStop_button(), "CANCEL RETRY");
for (long i = getWaitTimeExpBackOff(retry++); i > 0 && !_exit; i--) {
if (error_code == -18) {
getView().printStatusError("File temporarily unavailable! (Retrying in " + i + " secs...)");
} else {
getView().printStatusError("Mega/MC APIException error " + ex.getMessage() + " (Retrying in " + i + " secs...)");
}
try {
sleep(1000);
} catch (InterruptedException ex2) {
}
try {
sleep(1000);
} catch (InterruptedException ex2) {
}
}
}
} catch (Exception ex) {

View File

@ -30,10 +30,10 @@ public final class DownloadManager extends TransferenceManager {
getTransference_finished_queue().remove(d);
_total_transferences_size -= d.getFile_size();
if (((Download) d).isProvision_ok()) {
_total_transferences_size -= d.getFile_size();
delete_down.add(((Download) d).getUrl());
}
}

View File

@ -61,7 +61,7 @@ import org.apache.http.auth.UsernamePasswordCredentials;
*/
public final class MainPanel {
public static final String VERSION = "2.05";
public static final String VERSION = "2.06";
public static final int THROTTLE_SLICE_SIZE = 16 * 1024;
public static final int STREAMER_PORT = 1337;
public static final int WATCHDOG_PORT = 1338;

View File

@ -38,6 +38,7 @@ public final class MegaAPI {
public static final String API_URL = "https://g.api.mega.co.nz";
public static final String API_KEY = null;
public static final int REQ_ID_LENGTH = 10;
public static final Integer[] MEGA_ERROR_EXCEPTION_CODES = {-2, -8, -9, -10, -11, -12, -13, -14, -15, -16};
public static int checkMEGAError(String data) {
String error = findFirstRegex("^\\[?(\\-[0-9]+)\\]?$", data, 1);
@ -277,14 +278,12 @@ public final class MegaAPI {
try (CloseableHttpClient httpclient = MiscTools.getApacheKissHttpClient()) {
boolean error;
int conta_error = 0;
int error, conta_error = 0;
HttpPost httppost;
do {
error = true;
error = 0;
try {
@ -318,9 +317,8 @@ public final class MegaAPI {
if (response.length() > 0) {
if (checkMEGAError(response) == 0) {
error = false;
}
error = checkMEGAError(response);
}
}
@ -332,9 +330,14 @@ public final class MegaAPI {
Logger.getLogger(MegaAPI.class.getName()).log(Level.SEVERE, null, ex);
}
if (error) {
if (error != 0) {
System.out.println("MegaAPI ERROR. Waiting for retry...");
if (Arrays.asList(MEGA_ERROR_EXCEPTION_CODES).contains(error)) {
throw new MegaAPIException(String.valueOf(error));
}
System.out.println("MegaAPI ERROR " + String.valueOf(error) + " Waiting for retry...");
try {
Thread.sleep(getWaitTimeExpBackOff(conta_error++) * 1000);
@ -347,8 +350,11 @@ public final class MegaAPI {
conta_error = 0;
}
} while (error);
} while (error != 0);
} catch (MegaAPIException | IOException exception) {
throw exception;
}
_seqno++;

View File

@ -13,7 +13,6 @@ import java.util.LinkedList;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.util.logging.Logger.getLogger;
import javax.crypto.Cipher;
import javax.swing.JOptionPane;
import static megabasterd.MiscTools.BASE642Bin;
@ -22,7 +21,6 @@ import static megabasterd.MiscTools.Bin2UrlBASE64;
import static megabasterd.MiscTools.cleanFilePath;
import static megabasterd.MiscTools.cleanFilename;
import static megabasterd.MiscTools.findFirstRegex;
import static megabasterd.MiscTools.getWaitTimeExpBackOff;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
@ -44,82 +42,57 @@ public final class MegaCrypterAPI {
String response = null;
try (CloseableHttpClient httpclient = MiscTools.getApacheKissHttpClient()) {
boolean error;
int conta_error = 0;
HttpPost httppost;
do {
try {
httppost = new HttpPost(url_api.toURI());
error = true;
httppost.setHeader("Content-type", "application/json");
try {
httppost = new HttpPost(url_api.toURI());
httppost.setHeader("Custom-User-Agent", MainPanel.DEFAULT_USER_AGENT);
httppost.setHeader("Content-type", "application/json");
httppost.setEntity(new StringEntity(request));
httppost.setHeader("Custom-User-Agent", MainPanel.DEFAULT_USER_AGENT);
try (CloseableHttpResponse httpresponse = httpclient.execute(httppost)) {
httppost.setEntity(new StringEntity(request));
if (httpresponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.out.println("Failed : HTTP error code : " + httpresponse.getStatusLine().getStatusCode());
try (CloseableHttpResponse httpresponse = httpclient.execute(httppost)) {
} else {
if (httpresponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.out.println("Failed : HTTP error code : " + httpresponse.getStatusLine().getStatusCode());
InputStream is = httpresponse.getEntity().getContent();
} else {
try (ByteArrayOutputStream byte_res = new ByteArrayOutputStream()) {
InputStream is = httpresponse.getEntity().getContent();
byte[] buffer = new byte[16 * 1024];
try (ByteArrayOutputStream byte_res = new ByteArrayOutputStream()) {
int reads;
byte[] buffer = new byte[16 * 1024];
while ((reads = is.read(buffer)) != -1) {
int reads;
byte_res.write(buffer, 0, reads);
}
while ((reads = is.read(buffer)) != -1) {
response = new String(byte_res.toByteArray());
byte_res.write(buffer, 0, reads);
}
if (response.length() > 0) {
response = new String(byte_res.toByteArray());
int mc_error;
if (response.length() > 0) {
if ((mc_error = MegaCrypterAPI.checkMCError(response)) != 0) {
throw new MegaCrypterAPIException(String.valueOf(mc_error));
int mc_error;
if ((mc_error = MegaCrypterAPI.checkMCError(response)) != 0) {
throw new MegaCrypterAPIException(String.valueOf(mc_error));
} else {
error = false;
}
}
}
}
}
} catch (URISyntaxException ex) {
Logger.getLogger(MegaCrypterAPI.class.getName()).log(Level.SEVERE, null, ex);
}
if (error) {
} catch (URISyntaxException ex) {
Logger.getLogger(MegaCrypterAPI.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("MegaAPI ERROR. Waiting for retry...");
try {
Thread.sleep(getWaitTimeExpBackOff(conta_error++) * 1000);
} catch (InterruptedException ex) {
getLogger(MegaAPI.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
conta_error = 0;
}
} while (error);
}
return response;

View File

@ -15,6 +15,7 @@ public interface Transference {
boolean LIMIT_TRANSFERENCE_SPEED_DEFAULT = false;
int MAX_TRANSFERENCE_SPEED_DEFAULT = 5;
int MAX_WAIT_WORKERS_SHUTDOWN = 15;
Integer[] FATAL_ERROR_API_CODES = {-2, -8, -9, -10, -11, -12, -13, -14, -15, -16, 22, 23, 24, 25};
void start();

View File

@ -419,6 +419,8 @@ public final class Upload implements Transference, Runnable, SecureSingleThreadN
if (!_provision_ok) {
_status_error = true;
getView().hideAllExceptStatus();
if (_fatal_error != null) {

View File

@ -73,10 +73,10 @@ public final class UploadManager extends TransferenceManager {
getTransference_finished_queue().remove(u);
_total_transferences_size -= u.getFile_size();
if (((Upload) u).isProvision_ok()) {
_total_transferences_size -= u.getFile_size();
delete_up.add(new String[]{u.getFile_name(), ((Upload) u).getMa().getEmail()});
try {