From a09fb7b20d5a181746fc76d46792901444cdd545 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 27 Jan 2019 21:28:46 +0530 Subject: [PATCH] icat kitten: Add support for displaying images at http(s) URLs Fixes #1340 --- docs/changelog.rst | 2 ++ kittens/icat/main.py | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 6adc0f701..760bccf04 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,8 @@ Changelog 0.13.4 [future] --------------------- +- icat kitten: Add support for displaying images at http(s) URLs (:iss:`1340`) + - A new option :opt:`strip_trailing_spaces` to optionally remove trailing spaces from lines when copying to clipboard. diff --git a/kittens/icat/main.py b/kittens/icat/main.py index 6946b6e0f..b5e4acec3 100755 --- a/kittens/icat/main.py +++ b/kittens/icat/main.py @@ -318,6 +318,7 @@ def main(args=sys.argv): if len(items) > 1 or (isinstance(items[0], str) and os.path.isdir(items[0])): raise SystemExit(f'The --place option can only be used with a single image, not {items}') sys.stdout.buffer.write(b'\0337') # save cursor + url_pat = re.compile(r'https?://', flags=re.I) for item in items: is_tempfile = False try: @@ -326,11 +327,22 @@ def main(args=sys.argv): tf.write(item), tf.close() item = tf.name is_tempfile = True - if os.path.isdir(item): - for x in scan(item): - process(item, args) - else: + if url_pat.match(item) is not None: + from urllib.request import urlretrieve + with NamedTemporaryFile(prefix='url-image-data-', delete=False) as tf: + try: + urlretrieve(item, filename=tf.name) + except Exception as e: + raise SystemExit('Failed to download image at URL: {} with error: {}'.format(item, e)) + item = tf.name + is_tempfile = True process(item, args, is_tempfile) + else: + if os.path.isdir(item): + for x in scan(item): + process(item, args) + else: + process(item, args, is_tempfile) except NoImageMagick as e: raise SystemExit(str(e)) except ConvertFailed as e: