Skip to content

Commit

Permalink
Make default delay to wait after HTTP 429 an advanced setting (defaul…
Browse files Browse the repository at this point in the history
…t : 10s)
  • Loading branch information
RobbWatershed committed Feb 18, 2023
1 parent c2caf25 commit b788cf1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import android.content.SharedPreferences
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.InputType
import android.view.View
import android.webkit.CookieManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceScreen
Expand Down Expand Up @@ -99,6 +101,13 @@ class PreferencesFragment : PreferenceFragmentCompat(),
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
onExternalFolderChanged()

// Numbers-only on delay input
val editTextPreference =
preferenceManager.findPreference<EditTextPreference>(Preferences.Key.DL_HTTP_429_DEFAULT_DELAY)
editTextPreference?.setOnBindEditTextListener { editText ->
editText.inputType = InputType.TYPE_CLASS_NUMBER
}
}

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import me.devsaki.hentoid.parsers.ParseHelper;
import me.devsaki.hentoid.retrofit.sources.PixivServer;
import me.devsaki.hentoid.util.ContentHelper;
import me.devsaki.hentoid.util.Preferences;
import me.devsaki.hentoid.util.StringHelper;
import me.devsaki.hentoid.util.download.DownloadRateLimiter;
import me.devsaki.hentoid.util.exception.EmptyResultException;
Expand Down Expand Up @@ -189,7 +190,7 @@ private List<ImageFile> parseUser(@NonNull Content onlineContent, @Nullable Cont
int waited = 0;
DownloadRateLimiter.INSTANCE.take();
Response<PixivUserIllustMetadata> userIllustResp = PixivServer.api.getUserIllusts(userId, cookieStr).execute();
if (HttpHelper.waitBlocking429(userIllustResp)) {
if (HttpHelper.waitBlocking429(userIllustResp, Preferences.getHttp429DefaultDelaySecs() * 1000)) {
waited++;
userIllustResp = PixivServer.api.getUserIllusts(userId, cookieStr).execute();
}
Expand Down Expand Up @@ -231,7 +232,7 @@ private List<ImageFile> parseUser(@NonNull Content onlineContent, @Nullable Cont
waited = 0;
DownloadRateLimiter.INSTANCE.take();
Response<PixivIllustMetadata> illustResp = PixivServer.api.getIllustMetadata(illustId, cookieStr).execute();
while (HttpHelper.waitBlocking429(illustResp) && waited < 2) {
while (HttpHelper.waitBlocking429(illustResp, Preferences.getHttp429DefaultDelaySecs() * 1000) && waited < 2) {
waited++;
illustResp = PixivServer.api.getIllustMetadata(illustId, cookieStr).execute();
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/me/devsaki/hentoid/util/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ public static int getDownloadThreadCount() {
return getIntPref(Key.DL_THREADS_QUANTITY_LISTS, Default.DL_THREADS_QUANTITY);
}

public static int getHttp429DefaultDelaySecs() {
return getIntPref(Key.DL_HTTP_429_DEFAULT_DELAY, Default.DL_HTTP_429_DEFAULT_DELAY);
}

static int getFolderTruncationNbChars() {
return getIntPref(Key.FOLDER_TRUNCATION_LISTS, Default.FOLDER_TRUNCATION);
}
Expand Down Expand Up @@ -916,6 +920,7 @@ private Key() {
static final String DL_BLOCKED_TAG_BEHAVIOUR = "pref_dl_blocked_tags_behaviour";
static final String DL_EH_HIRES = "pref_dl_eh_hires";
public static final String DL_THREADS_QUANTITY_LISTS = "pref_dl_threads_quantity_lists";
public static final String DL_HTTP_429_DEFAULT_DELAY = "pref_dl_http_429_default_delay";
public static final String ACTIVE_SITES = "active_sites";
static final String LOCK_ON_APP_RESTORE = "pref_lock_on_app_restore";
static final String LOCK_TIMER = "pref_lock_timer";
Expand Down Expand Up @@ -987,6 +992,7 @@ private Default() {
static final int BROWSER_DNS_OVER_HTTPS = -1; // No DNS
static final boolean BROWSER_NHENTAI_INVISIBLE_BLACKLIST = false;
static final int DL_THREADS_QUANTITY = Constant.DOWNLOAD_THREAD_COUNT_AUTO;
static final int DL_HTTP_429_DEFAULT_DELAY = 10;
static final int FOLDER_TRUNCATION = Constant.TRUNCATE_FOLDER_100;
static final boolean VIEWER_RESUME_LAST_LEFT = true;
static final boolean VIEWER_KEEP_SCREEN_ON = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,9 @@ public static String simplifyUrl(@NonNull String url) {
* @param response Response to examine
* @return True if the response is an HTTP 429 _and_ a delay has been supplied and waited out
*/
public static boolean waitBlocking429(retrofit2.Response<?> response) {
public static boolean waitBlocking429(retrofit2.Response<?> response, int defaultDelayMs) {
if (429 == response.code()) {
int delay = 3000; // Pause 3 secs by default
int delay = defaultDelayMs;
String retryDelay = response.headers().get("Retry-After");
if (null == retryDelay) retryDelay = response.headers().get("retry-after");
if (retryDelay != null && StringHelper.isNumeric(retryDelay)) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@
<string name="pref_dl_eh_hires_default" translatable="false">false</string>
<string name="pref_dl_threads_quantity_default" translatable="false">0</string>
<string name="pref_dl_speed_cap_default" translatable="false">-1</string>
<string name="pref_dl_http_429_default_delay_default" translatable="false">10</string>
<!-- Strings: Settings: Privacy -->
<string name="pref_app_preview_default" translatable="false">false</string>
<string name="pref_analytics_preference_default" translatable="false">true</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<string name="pref_dl_threads_quantity_title">Number of parallel downloads</string>
<string name="pref_dl_threads_quantity_summary">Number of images downloaded simultaneously\nCurrently: <x:g example="5" id="number">%s</x:g></string>
<string name="pref_dl_speed_cap_title">Download speed limit</string>
<string name="pref_dl_http_429_default_delay_title">HTTP 429 default delay (seconds)</string>

<!-- Privacy -->
<string name="pref_screen_privacy">Privacy</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@
android:summary="@string/pref_generic_summary"
android:title="@string/pref_dl_speed_cap_title"
app:iconSpaceReserved="false" />
<EditTextPreference
android:defaultValue="@string/pref_dl_http_429_default_delay_default"
android:key="pref_dl_http_429_default_delay"
android:title="@string/pref_dl_http_429_default_delay_title"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>

Expand Down

0 comments on commit b788cf1

Please sign in to comment.