Skip to content

Commit

Permalink
Ajout de la fonctionnalité import média inpn sous la forme d'une comm…
Browse files Browse the repository at this point in the history
…ande
  • Loading branch information
amandine-sahl committed Aug 7, 2024
1 parent 03705c1 commit 52ec95d
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 262 deletions.
39 changes: 39 additions & 0 deletions apptax/taxonomie/commands/taxref.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import click
import csv

from flask.cli import with_appcontext
from sqlalchemy.orm.exc import NoResultFound

from apptax.database import db
from apptax.taxonomie.commands.migrate_taxref.commands_v17 import migrate_to_v17
Expand All @@ -21,6 +24,12 @@
from .migrate_taxref.commands_v16 import migrate_to_v16
from .migrate_taxref.test_commands_migrate import test_migrate_taxref

from apptax.taxonomie.models import Taxref

import logging

logger = logging.getLogger("taxref_commands")


@click.group(help="Manager TaxRef referentials.")
def taxref():
Expand Down Expand Up @@ -84,6 +93,35 @@ def delete_bdc():
db.session.commit()


@taxref.command(
help="Importer des médias de l'INPN TaxRef à partir d'une liste de cd_ref de référence."
)
@click.argument("file", type=click.Path(exists=True))
@with_appcontext
def import_inpn_media(file):
"""
Importer des médias de l'INPN TaxRef à partir d'une liste de cd_ref de référence
Le fichier doit contenir une colonne avec la liste des cd_ref à traiter
"""
from apptax.utils.taxref_api import import_inpn_media

with open(file, "r") as file:
csvreader = csv.reader(file)
for row in csvreader:
value = row[0]
if value in ("cd_nom", "cd_ref"):
continue

# Get Taxon
try:
taxon = Taxref.query.get(int(value))
except (NoResultFound, ValueError):
logger.error(f"{value} is not a valid cd_ref")
continue

import_inpn_media(taxon.cd_ref, taxon.cd_nom, logger)


taxref.add_command(import_v14)
taxref.add_command(import_bdc_v14)
taxref.add_command(import_v15)
Expand All @@ -98,3 +136,4 @@ def delete_bdc():
taxref.add_command(test_migrate_taxref)
taxref.add_command(link_bdc_statut_to_areas)
taxref.add_command(enable_bdc_statut_text)
taxref.add_command(import_inpn_media)
75 changes: 75 additions & 0 deletions apptax/utils/taxref_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import requests

from sqlalchemy.orm.exc import NoResultFound

from apptax.database import db

from apptax.taxonomie.models import TMedias, BibTypesMedia


API_URL = "https://taxref.mnhn.fr/api"


def import_inpn_media(cd_ref, cd_nom, logger=None):
"""
Fonction qui interroge l'api média de taxref
et qui peuple la table média
"""
url = f"{API_URL}/taxa/{cd_ref}/media"
inpn_response = requests.get(url)

if inpn_response.status_code != 200:
if logger:
logger.warning(
f"{cd_ref} : l'URL {url} retourne le code HTTP {inpn_response.status_code} !"
)
return

data = inpn_response.json()
if "_embedded" not in data:
if logger:
logger.warning(f"{cd_ref} : l'URL {url} ne retourne aucune réponse!")
return

if not data["_embedded"]["media"]:
if logger:
logger.warning(f"{cd_ref} : l'URL {url} ne retourne aucun media !")
return

medias = data["_embedded"]["media"]

# Get media type
type = BibTypesMedia.query.get(1)
for m_inpn in medias:
url = m_inpn["_links"]["file"]["href"]
# Test si l'URL du média courant n'est pas morte
r = requests.get(url)
if not r.status_code == 200:
if logger:
logger.warning(
f"ERREUR : l'URL du média {url} retourne le code HTTP {r.status_code} !"
)
break
# Check if media exists
try:
m_obj = TMedias.query.filter_by(url=url).one()
except NoResultFound:
m_obj = TMedias(
url=url,
)
m_obj.cd_ref = cd_nom
m_obj.titre = m_inpn["taxon"]["referenceNameHtml"]
m_obj.nom = m_inpn["taxon"]["scientificName"]
m_obj.auteur = m_inpn["copyright"]
m_obj.desc_media = m_inpn["title"]
m_obj.licence = m_inpn["licence"]
m_obj.is_public = True
m_obj.supprime = False
m_obj.source = "INPN"
m_obj.id_type = type.id_type
db.session.add(m_obj)

db.session.commit()
if logger:
logger.info(f"{cd_ref} :Ajout média: {m_obj.titre}")
82 changes: 0 additions & 82 deletions data/scripts/import_inpn_media/README.rst

This file was deleted.

15 changes: 0 additions & 15 deletions data/scripts/import_inpn_media/config.py.sample

This file was deleted.

Loading

0 comments on commit 52ec95d

Please sign in to comment.