core/homeassistant/components/downloader/__init__.py

165 lines
5.1 KiB
Python
Raw Normal View History

"""Support for functionality to download files."""
import logging
2016-02-19 05:27:50 +00:00
import os
import re
import threading
2016-01-29 05:37:08 +00:00
import requests
import voluptuous as vol
2016-01-29 05:37:08 +00:00
from homeassistant.const import HTTP_OK
import homeassistant.helpers.config_validation as cv
from homeassistant.util import sanitize_filename
2016-09-02 04:31:49 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
ATTR_FILENAME = "filename"
ATTR_SUBDIR = "subdir"
ATTR_URL = "url"
ATTR_OVERWRITE = "overwrite"
2019-07-31 19:25:30 +00:00
CONF_DOWNLOAD_DIR = "download_dir"
2019-07-31 19:25:30 +00:00
DOMAIN = "downloader"
DOWNLOAD_FAILED_EVENT = "download_failed"
DOWNLOAD_COMPLETED_EVENT = "download_completed"
2019-07-31 19:25:30 +00:00
SERVICE_DOWNLOAD_FILE = "download_file"
2019-07-31 19:25:30 +00:00
SERVICE_DOWNLOAD_FILE_SCHEMA = vol.Schema(
{
vol.Required(ATTR_URL): cv.url,
vol.Optional(ATTR_SUBDIR): cv.string,
vol.Optional(ATTR_FILENAME): cv.string,
vol.Optional(ATTR_OVERWRITE, default=False): cv.boolean,
}
)
2019-07-31 19:25:30 +00:00
CONFIG_SCHEMA = vol.Schema(
{DOMAIN: vol.Schema({vol.Required(CONF_DOWNLOAD_DIR): cv.string})},
extra=vol.ALLOW_EXTRA,
)
2016-09-02 04:31:49 +00:00
def setup(hass, config):
2016-03-08 16:55:57 +00:00
"""Listen for download events to download files."""
download_path = config[DOMAIN][CONF_DOWNLOAD_DIR]
# If path is relative, we assume relative to Home Assistant config dir
2015-10-26 00:10:32 +00:00
if not os.path.isabs(download_path):
2015-10-26 00:13:47 +00:00
download_path = hass.config.path(download_path)
2015-10-26 00:10:32 +00:00
if not os.path.isdir(download_path):
2016-09-02 04:31:49 +00:00
_LOGGER.error(
2019-07-31 19:25:30 +00:00
"Download path %s does not exist. File Downloader not active", download_path
)
return False
def download_file(service):
2016-03-08 16:55:57 +00:00
"""Start thread to download file specified in the URL."""
2019-07-31 19:25:30 +00:00
def do_download():
2016-03-08 16:55:57 +00:00
"""Download the file."""
try:
url = service.data[ATTR_URL]
subdir = service.data.get(ATTR_SUBDIR)
filename = service.data.get(ATTR_FILENAME)
overwrite = service.data.get(ATTR_OVERWRITE)
if subdir:
subdir = sanitize_filename(subdir)
final_path = None
2014-06-13 06:09:56 +00:00
req = requests.get(url, stream=True, timeout=10)
if req.status_code != HTTP_OK:
_LOGGER.warning(
2019-07-31 19:25:30 +00:00
"downloading '%s' failed, status_code=%d", url, req.status_code
)
hass.bus.fire(
f"{DOMAIN}_{DOWNLOAD_FAILED_EVENT}",
2019-07-31 19:25:30 +00:00
{"url": url, "filename": filename},
)
else:
2019-07-31 19:25:30 +00:00
if filename is None and "content-disposition" in req.headers:
match = re.findall(
r"filename=(\S+)", req.headers["content-disposition"]
)
if match:
filename = match[0].strip("'\" ")
if not filename:
filename = os.path.basename(url).strip()
if not filename:
2019-07-31 19:25:30 +00:00
filename = "ha_download"
# Remove stuff to ruin paths
filename = sanitize_filename(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..
if not overwrite:
tries = 1
final_path = path + ext
while os.path.isfile(final_path):
tries += 1
final_path = f"{path}_{tries}.{ext}"
_LOGGER.debug("%s -> %s", url, final_path)
2019-07-31 19:25:30 +00:00
with open(final_path, "wb") as fil:
for chunk in req.iter_content(1024):
fil.write(chunk)
_LOGGER.debug("Downloading of %s done", url)
hass.bus.fire(
f"{DOMAIN}_{DOWNLOAD_COMPLETED_EVENT}",
2019-07-31 19:25:30 +00:00
{"url": url, "filename": filename},
)
except requests.exceptions.ConnectionError:
_LOGGER.exception("ConnectionError occurred for %s", url)
hass.bus.fire(
f"{DOMAIN}_{DOWNLOAD_FAILED_EVENT}",
2019-07-31 19:25:30 +00:00
{"url": url, "filename": filename},
)
# 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()
2019-07-31 19:25:30 +00:00
hass.services.register(
DOMAIN,
SERVICE_DOWNLOAD_FILE,
download_file,
schema=SERVICE_DOWNLOAD_FILE_SCHEMA,
)
return True