feat(convert): ajoute le support pour les documents liés

This commit is contained in:
François Poulain 2020-08-09 16:34:23 +02:00
parent 734deddfc5
commit 926b1bd91f
1 changed files with 17 additions and 3 deletions

View File

@ -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))