Skip to content

Commit 3840c57

Browse files
committed
cli: Add --retry-streams and --retry-open options.
Resolves #333.
1 parent 8717efe commit 3840c57

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

docs/cli.rst

+9
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,15 @@ Command line options
276276

277277
Do not check for new Livestreamer releases
278278

279+
.. cmdoption:: --retry-streams delay
280+
281+
Will retry fetching streams until streams are found
282+
while waiting <delay> (seconds) between each attempt
283+
284+
.. cmdoption:: --retry-open attempts
285+
286+
Will try <attempts> to open the stream until giving up
287+
279288

280289
HTTP options
281290
^^^^^^^^^^^^

src/livestreamer_cli/argparser.py

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ def func(p):
6666
"disable log output, useful for external scripting")
6767
parser.add_argument("--no-version-check", action="store_true",
6868
help="Do not check for new Livestreamer releases")
69+
parser.add_argument("--retry-streams", metavar="delay", type=float,
70+
help="Will retry fetching streams until streams are found "
71+
"while waiting <delay> (seconds) between each attempt")
72+
parser.add_argument("--retry-open", metavar="attempts", type=int, default=1,
73+
help="Will try <attempts> to open the stream until giving up")
6974
parser.add_argument("--yes-run-as-root", action="store_true",
7075
help=argparse.SUPPRESS)
7176

src/livestreamer_cli/main.py

+35-5
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,13 @@ def open_stream(stream):
226226
def output_stream(stream):
227227
"""Open stream, create output and finally write the stream to output."""
228228

229-
try:
230-
stream_fd, prebuffer = open_stream(stream)
231-
except StreamError as err:
232-
console.logger.error("{0}", err)
229+
for i in range(args.retry_open):
230+
try:
231+
stream_fd, prebuffer = open_stream(stream)
232+
break
233+
except StreamError as err:
234+
console.logger.error("{0}", err)
235+
else:
233236
return
234237

235238
output = create_output()
@@ -371,6 +374,29 @@ def fetch_streams(plugin):
371374
sorting_excludes=args.stream_sorting_excludes)
372375

373376

377+
def fetch_streams_infinite(plugin, interval):
378+
"""Attempts to fetch streams until some are returned."""
379+
380+
try:
381+
streams = fetch_streams(plugin)
382+
except PluginError as err:
383+
console.logger.error("{0}", err)
384+
streams = None
385+
386+
if not streams:
387+
console.logger.info("Waiting for streams, retrying every {0} "
388+
"second(s)", args.retry_streams)
389+
while not streams:
390+
sleep(args.retry_streams)
391+
392+
try:
393+
streams = fetch_streams(plugin)
394+
except PluginError as err:
395+
console.logger.error("{0}", err)
396+
397+
return streams
398+
399+
374400
def resolve_stream_name(streams, stream_name):
375401
"""Returns the real stream name of a synonym."""
376402

@@ -424,7 +450,11 @@ def handle_url():
424450
plugin = livestreamer.resolve_url(args.url)
425451
console.logger.info("Found matching plugin {0} for URL {1}",
426452
plugin.module, args.url)
427-
streams = fetch_streams(plugin)
453+
454+
if args.retry_streams:
455+
streams = fetch_streams_infinite(plugin, args.retry_streams)
456+
else:
457+
streams = fetch_streams(plugin)
428458
except NoPluginError:
429459
console.exit("No plugin can handle URL: {0}", args.url)
430460
except PluginError as err:

0 commit comments

Comments
 (0)