diff --git a/drupal2spip_lal/base/convert.py b/drupal2spip_lal/base/convert.py index 7c43bc3..6dea37f 100644 --- a/drupal2spip_lal/base/convert.py +++ b/drupal2spip_lal/base/convert.py @@ -56,10 +56,14 @@ def strong_to_dl(html): def download(src, filename, force_download): if src and src.startswith('/'): src = 'http://{}{}'.format(settings.DRUPAL_FQDN, src) + elif src and src.startswith('sites/default/files/'): + src = 'http://{}/{}'.format(settings.DRUPAL_FQDN, src) + elif not re.match(r'^(https?)?://', src): + raise ValueError('Impossible de localiser: {}'.format(src)) path = os.path.join(settings.SPIP_LOGO_DIR, filename) - if not os.access(path, os.R_OK) or force_download: + if not os.access(path, os.F_OK) or force_download: r = request.get(src, stream=True) with open(path, 'wb') as fd: for chunk in r.iter_content(chunk_size=128): @@ -68,8 +72,18 @@ def download(src, filename, force_download): def fetch_document(src, filename, force_download): extension = filename.split('.')[-1] or 'unknown' + directory = os.path.join(settings.SPIP_LOGO_DIR, extension) + if not os.access(directory, os.F_OK): + try: + os.mkdir(directory) + except Exception as e: + logger.warn('Echec à creer le dossier: {}'.format(e)) + cible = os.path.join(extension, filename) - download(src, cible, force_download) + try: + download(src, cible, force_download) + except Exception as e: + logger.warn('Echec au download: {}'.format(e)) return extension, cible @@ -113,7 +127,7 @@ def fetch_and_remove_logo(article, force_download): article.texte = str(soup) article.save() - else: + elif src: logger.warn('Article {} has ignored logo: {}'.format(article.pk, src))