From 49e6386794cd0d3608738c20572e6e7352874e0c Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 24 Mar 2014 18:00:57 -0700 Subject: [PATCH] Downloader now supports downloading to subdirs --- homeassistant/components/downloader.py | 37 ++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/downloader.py b/homeassistant/components/downloader.py index 9dd67bc67fa..34c21d6c3a8 100644 --- a/homeassistant/components/downloader.py +++ b/homeassistant/components/downloader.py @@ -15,6 +15,9 @@ DOMAIN = "downloader" SERVICE_DOWNLOAD_FILE = "download_file" +ATTR_URL = "url" +ATTR_SUBDIR = "subdir" + # pylint: disable=too-many-branches def setup(bus, download_path): @@ -41,14 +44,22 @@ def setup(bus, download_path): def download_file(service): """ Starts thread to download file specified in the url. """ - if not 'url' in service.data: + if not ATTR_URL in service.data: logger.error("Service called but 'url' parameter not specified.") return def do_download(): """ Downloads the file. """ try: - url = service.data['url'] + url = service.data[ATTR_URL] + + subdir = service.data.get(ATTR_SUBDIR) + + if subdir: + subdir = util.sanitize_filename(subdir) + + final_path = None + req = requests.get(url, stream=True) if req.status_code == 200: @@ -71,8 +82,20 @@ def setup(bus, download_path): # Remove stuff to ruin paths filename = util.sanitize_filename(filename) - path, ext = os.path.splitext(os.path.join(download_path, - filename)) + # Do we want to download to subdir, create if needed + if subdir: + subdir_path = os.path.join(download_path, subdir) + + # Ensure subdir exist + if not os.path.isdir(subdir_path): + os.makedirs(subdir_path) + + final_path = os.path.join(subdir_path, filename) + + else: + final_path = os.path.join(download_path, filename) + + path, ext = os.path.splitext(final_path) # If file exist append a number. # We test filename, filename_2.. @@ -81,7 +104,7 @@ def setup(bus, download_path): while os.path.isfile(final_path): tries += 1 - final_path = path + "_{}".format(tries) + ext + final_path = "{}_{}.{}".format(path, tries, ext) logger.info("{} -> {}".format( url, final_path)) @@ -97,6 +120,10 @@ def setup(bus, download_path): logger.exception("ConnectionError occured for {}". format(url)) + # Remove file if we started downloading but failed + if final_path and os.path.isfile(final_path): + os.remove(final_path) + threading.Thread(target=do_download).start() bus.register_service(DOMAIN, SERVICE_DOWNLOAD_FILE,