Skip to content

Commit ced547b

Browse files
authored
[FEATURE] Arg to shuffle subscriptions (#1263)
Closes #1234 Adds `--shuffle` support which shuffles the order of subscriptions when downloading.
1 parent c163f97 commit ced547b

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/ytdl_sub/cli/entrypoint.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import gc
22
import os
3+
import random
34
import sys
45
from datetime import datetime
56
from pathlib import Path
@@ -75,6 +76,7 @@ def _download_subscriptions_from_yaml_files(
7576
subscription_override_dict: Dict,
7677
update_with_info_json: bool,
7778
dry_run: bool,
79+
shuffle: bool,
7880
) -> List[Subscription]:
7981
"""
8082
Downloads all subscriptions from one or many subscription yaml files.
@@ -91,6 +93,8 @@ def _download_subscriptions_from_yaml_files(
9193
Whether to actually download or update using existing info json
9294
dry_run
9395
Whether to dry run or not
96+
shuffle
97+
Whether to shuffle the subscription download order
9498
9599
Returns
96100
-------
@@ -112,6 +116,10 @@ def _download_subscriptions_from_yaml_files(
112116
subscription_override_dict=subscription_override_dict,
113117
)
114118

119+
if shuffle:
120+
logger.info("Shuffling subscriptions")
121+
random.shuffle(subscriptions)
122+
115123
for subscription in subscriptions:
116124
with subscription.exception_handling():
117125
logger.info(
@@ -253,6 +261,7 @@ def main() -> List[Subscription]:
253261
subscription_override_dict=subscription_override_dict,
254262
update_with_info_json=args.update_with_info_json,
255263
dry_run=args.dry_run,
264+
shuffle=args.shuffle,
256265
)
257266

258267
# One-off download

src/ytdl_sub/cli/parsers/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ class SubArguments:
172172
short="-o",
173173
long="--dl-override",
174174
)
175+
SHUFFLE = CLIArgument(
176+
short="-sh",
177+
long="--shuffle",
178+
)
175179

176180

177181
subscription_parser = subparsers.add_parser("sub")
@@ -197,6 +201,13 @@ class SubArguments:
197201
help="override all subscription config values using `dl` syntax, "
198202
"i.e. --dl-override='--ytdl_options.max_downloads 3'",
199203
)
204+
subscription_parser.add_argument(
205+
SubArguments.SHUFFLE.short,
206+
SubArguments.SHUFFLE.long,
207+
action="store_true",
208+
help="shuffle subscription order when downloading",
209+
default=False,
210+
)
200211

201212
###################################################################################################
202213
# DOWNLOAD PARSER

tests/integration/cli/test_entrypoint.py

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

1010
from ytdl_sub.cli.entrypoint import _download_subscriptions_from_yaml_files
1111
from ytdl_sub.cli.entrypoint import main
12+
from ytdl_sub.config.config_file import ConfigFile
1213
from ytdl_sub.subscriptions.subscription import Subscription
1314
from ytdl_sub.utils.exceptions import ExperimentalFeatureNotEnabled
1415

@@ -59,6 +60,7 @@ def test_subscription_logs_write_to_file(
5960
subscription_override_dict={},
6061
update_with_info_json=False,
6162
dry_run=dry_run,
63+
shuffle=False,
6264
)
6365
except ValueError:
6466
assert not mock_success_output
@@ -120,3 +122,40 @@ def test_update_with_info_json_requires_experimental_flag(
120122
pytest.raises(ExperimentalFeatureNotEnabled),
121123
):
122124
_ = main()
125+
126+
127+
def test_subscription_shuffle(
128+
default_config: ConfigFile,
129+
mock_subscription_download_factory: Callable,
130+
music_video_subscription_path: Path,
131+
):
132+
133+
subscription_paths = [str(music_video_subscription_path)]
134+
135+
with (
136+
patch.object(
137+
Subscription,
138+
"download",
139+
new=mock_subscription_download_factory(mock_success_output=True),
140+
),
141+
):
142+
out1 = _download_subscriptions_from_yaml_files(
143+
config=default_config,
144+
subscription_paths=subscription_paths,
145+
subscription_matches=[],
146+
subscription_override_dict={},
147+
update_with_info_json=False,
148+
dry_run=True,
149+
shuffle=True,
150+
)
151+
out2 = _download_subscriptions_from_yaml_files(
152+
config=default_config,
153+
subscription_paths=subscription_paths,
154+
subscription_matches=[],
155+
subscription_override_dict={},
156+
update_with_info_json=False,
157+
dry_run=True,
158+
shuffle=True,
159+
)
160+
161+
assert [sub.name for sub in out1] != [sub.name for sub in out2]

0 commit comments

Comments
 (0)