Skip to content

Commit 6c180c9

Browse files
authored
[BACKEND] Enable throttle protection by default (#1257)
To protect new users, all prebuilt preset (excluding Soundcloud and Bandcamp) will include throttle protection by default. > How do I disable? Set the override variable `enable_throttle_protection: False`. This can be done on a per-subscription basis and/or in the top __preset__ section to apply to all presets: ``` __preset__: overrides: enable_throttle_protection: False ``` > What if I already use throttle protection? Your settings will override whatever values are set in the defaults. Closes: - #1121 - #1186
1 parent a055c9b commit 6c180c9

12 files changed

Lines changed: 64 additions & 10 deletions

File tree

src/ytdl_sub/plugins/throttle_protection.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ def subscription_download_probability(self) -> Optional[ProbabilityValidator]:
185185
class ThrottleProtectionPlugin(Plugin[ThrottleProtectionOptions]):
186186
plugin_options_type = ThrottleProtectionOptions
187187

188+
@classmethod
189+
def perform_sleep(cls, sleep_time: float) -> None:
190+
"""
191+
Wrapper to be able to mock
192+
"""
193+
time.sleep(sleep_time)
194+
188195
def __init__(
189196
self,
190197
options: ThrottleProtectionOptions,
@@ -239,7 +246,7 @@ def post_process_entry(self, entry: Entry) -> Optional[FileMetadata]:
239246
self._subscription_max_downloads is not None
240247
and self._subscription_download_counter == 0
241248
):
242-
logger.debug(
249+
logger.info(
243250
"Setting subscription max downloads to %d", self._subscription_max_downloads
244251
)
245252

@@ -248,8 +255,8 @@ def post_process_entry(self, entry: Entry) -> Optional[FileMetadata]:
248255

249256
if self.plugin_options.sleep_per_download_s:
250257
sleep_time = self.plugin_options.sleep_per_download_s.randomized_float()
251-
logger.debug("Sleeping between downloads for %0.2f seconds", sleep_time)
252-
time.sleep(sleep_time)
258+
logger.info("Sleeping between downloads for %0.2f seconds", sleep_time)
259+
self.perform_sleep(sleep_time)
253260

254261
return None
255262

@@ -265,5 +272,5 @@ def post_process_subscription(self):
265272

266273
if self.plugin_options.sleep_per_subscription_s:
267274
sleep_time = self.plugin_options.sleep_per_subscription_s.randomized_float()
268-
logger.debug("Sleeping between subscriptions for %0.2f seconds", sleep_time)
269-
time.sleep(sleep_time)
275+
logger.info("Sleeping between subscriptions for %0.2f seconds", sleep_time)
276+
self.perform_sleep(sleep_time)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
presets:
2+
3+
_throttle_protection:
4+
throttle_protection:
5+
enable: >-
6+
{
7+
%print(
8+
%if(
9+
enable_throttle_protection,
10+
"Throttle protection is enabled. Disable using the override variable `enable_throttle_protection: False`",
11+
"Throttle protection is disabled. Use at your own risk!"
12+
),
13+
enable_throttle_protection
14+
)
15+
}
16+
sleep_per_request_s:
17+
min: 3.5
18+
max: 3.5
19+
sleep_per_download_s:
20+
min: 13.8
21+
max: 28.4
22+
sleep_per_subscription_s:
23+
min: 16.3
24+
max: 26.1
25+
overrides:
26+
enable_throttle_protection: True

src/ytdl_sub/prebuilt_presets/music/albums_from_chapters.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ presets:
2222

2323
"YouTube Full Albums":
2424
preset:
25-
- "_albums_from_chapters"
25+
- "_albums_from_chapters"
26+
- "_throttle_protection"

src/ytdl_sub/prebuilt_presets/music/albums_from_playlists.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ presets:
1818
"YouTube Releases":
1919
preset:
2020
- "_albums_from_playlists"
21+
- "_throttle_protection"
2122

2223
"Bandcamp":
2324
preset:

src/ytdl_sub/prebuilt_presets/music/singles.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ presets:
22

33

44
_music_base:
5+
56
output_options:
67
output_directory: "{music_directory}"
78
file_name: "{track_full_path}"
File renamed without changes.

src/ytdl_sub/prebuilt_presets/music_videos/music_video_base.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ presets:
22
_music_video_base:
33

44
preset:
5+
# ytdl-sub includes throttle prootection by default or all prebuilt presets.
6+
# Can be disabled with override variable `enable_throttle_protection: False`
7+
- "_throttle_protection"
58
- "_url_categorized"
69

710
output_options:

src/ytdl_sub/prebuilt_presets/tv_show/episode.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ presets:
33
####################################################################################################
44

55
_episode_base:
6+
preset:
7+
# ytdl-sub includes throttle prootection by default or all prebuilt presets.
8+
# Can be disabled with override variable `enable_throttle_protection: False`
9+
- "_throttle_protection"
10+
611
output_options:
712
output_directory: "{tv_show_directory}/{tv_show_name_sanitized}"
813
file_name: "{episode_file_path}.{ext}"

src/ytdl_sub/script/functions/print_functions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010

1111
def _log(message: AnyArgument, level: Optional[Integer]) -> None:
12+
if not str(message):
13+
return
14+
1215
if level is None:
1316
logger.info(str(message))
1417
return

tests/integration/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ytdl_sub.downloaders.ytdlp import YTDLP
1616
from ytdl_sub.entries.script.variable_definitions import VARIABLES
1717
from ytdl_sub.entries.script.variable_definitions import VariableDefinitions
18+
from ytdl_sub.plugins.throttle_protection import ThrottleProtectionPlugin
1819

1920
v: VariableDefinitions = VARIABLES
2021

@@ -264,6 +265,9 @@ def _write_entries_to_working_dir(*args, **kwargs) -> List[Dict]:
264265
patch.object(
265266
MultiUrlDownloader, "_extract_entry_info_with_retry", new=lambda _, entry: entry
266267
),
268+
# Throttle protection is included in all prebuilt presets. Mock the sleep avoid
269+
# actual sleeps
270+
patch.object(ThrottleProtectionPlugin, "perform_sleep", new=lambda _1, _2: None),
267271
):
268272
# Stub out metadata. TODO: update this if we do metadata plugins
269273
yield

0 commit comments

Comments
 (0)