okfnfr / CITES-biodiversity

CITES project
6 stars 0 forks source link

API : Extraction des données des espèces Wikidata vers une app web #7

Open adaniela opened 5 years ago

adaniela commented 5 years ago

1er test: espèces et statut juridique CITES Species+ ID.

adaniela commented 5 years ago

Query Species, ID Cites : 19 994 results

Columns: taxonRank idCITES nomSC

https://query.wikidata.org/#SELECT%20distinct%20%20%3FtaxonRank%20%3FidCITES%20%20%3FnomSc%0AWHERE%20%7B%0A%20%3Fitem%20wdt%3AP2040%20%3FidCITES%20.%0A%20%3Fitem%20wdt%3AP225%20%3FnomSc%20.%0A%20%3Fitem%20wdt%3AP105%20%3FtaxonRank%0A%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP18%20%3Fimage%20.%20%7D%0A%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22fr%22%20%7D%0A%7D%0A

API Species, ID Cites:

https://query.wikidata.org/sparql?format=json&query=SELECT%20distinct%20%20%3FtaxonRank%20%3FidCITES%20%20%3FnomSc%0AWHERE%20%7B%0A%20%3Fitem%20wdt%3AP2040%20%3FidCITES%20.%0A%20%3Fitem%20wdt%3AP225%20%3FnomSc%20.%0A%20%3Fitem%20wdt%3AP105%20%3FtaxonRank%0A%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP18%20%3Fimage%20.%20%7D%0A%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22fr%22%20%7D%0A%7D%0A

Query Species, ID Cites and images: 31 306 results

Columns: idCITES item itemLabel taxonRank nomScientific taxonCommonName taxonCommonName_lang Image

https://query.wikidata.org/#%23%20Afficher%20les%20esp%C3%A8ces%20ayant%20un%20ID%20CITES%20mais%20aucune%20aucune%20image%20%28v2%29%0ASELECT%20%3FidCITES%20%3Fitem%20%3FitemLabel%20%3FtaxonRank%20%3FnomScientific%20%3FtaxonCommonName%20%3FtaxonCommonName_lang%20%3Fimage%0AWHERE%20%7B%0A%20%3Fitem%20wdt%3AP2040%20%3FidCITES%20.%0A%20%3Fitem%20wdt%3AP225%20%3FnomScientific%20.%0A%20%20%0A%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP105%20%3FtaxonRank%20.%20%7D%0A%20OPTIONAL%20%7B%20%0A%20%20%20%3Fitem%20wdt%3AP1843%20%3FtaxonCommonName%20.%20%0A%20%20%20FILTER%20%28lang%28%3FtaxonCommonName%29%20%3D%20%22en%22%20%7C%7C%20lang%28%3FtaxonCommonName%29%20%3D%20%22fr%22%29%20.%20%20%23%20restreindre%20par%20langue%0A%20%20%20BIND%28lang%28%3FtaxonCommonName%29%20AS%20%3FtaxonCommonName_lang%29%20%20%23%20extraire%20le%20code%20langue%20%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP18%20%3Fimage%20.%20%7D%0A%20%20%0A%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20%7D%0A%7D%20%0AORDER%20BY%20ASC%20%28%3FidCITES%29

API Species, ID Cites and images:

https://query.wikidata.org/sparql?format=json&query=%23%20Afficher%20les%20esp%C3%A8ces%20ayant%20un%20ID%20CITES%20mais%20aucune%20aucune%20image%20(v2)%0ASELECT%20%3FidCITES%20%3Fitem%20%3FitemLabel%20%3FtaxonRank%20%3FnomScientific%20%3FtaxonCommonName%20%3FtaxonCommonName_lang%20%3Fimage%0AWHERE%20%7B%0A%20%3Fitem%20wdt%3AP2040%20%3FidCITES%20.%0A%20%3Fitem%20wdt%3AP225%20%3FnomScientific%20.%0A%20%20%0A%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP105%20%3FtaxonRank%20.%20%7D%0A%20OPTIONAL%20%7B%20%0A%20%20%20%3Fitem%20wdt%3AP1843%20%3FtaxonCommonName%20.%20%0A%20%20%20FILTER%20(lang(%3FtaxonCommonName)%20%3D%20%22en%22%20%7C%7C%20lang(%3FtaxonCommonName)%20%3D%20%22fr%22)%20.%20%20%23%20restreindre%20par%20langue%0A%20%20%20BIND(lang(%3FtaxonCommonName)%20AS%20%3FtaxonCommonName_lang)%20%20%23%20extraire%20le%20code%20langue%20%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP18%20%3Fimage%20.%20%7D%0A%20%20%0A%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20%7D%0A%7D%20%0AORDER%20BY%20ASC%20(%3FidCITES)

cecilaki commented 4 years ago

@adaniela a essayé d’utiliser l’api dans une web app rails sans succès pour le moment. Ci-dessous l'erreur que j'ai eu: "rails aborted! OpenURI::HTTPError: 403 Please identify your user agent, see https://meta.wikimedia.org/wiki/User-Agent_policy"

adaniela commented 4 years ago

Entête pour la requête GET : User-Agent: EndangeredSpeciesIdentifier/0.1 (contact@okfnfr; http://cites.okfn.fr/en/home/)

itly314 commented 4 years ago

Example of how to query the Wikidata API from Python using the requests library

See docs on the requests library here

import requests

# Wikidata SPARQL endpoint
url = 'https://query.wikidata.org/sparql'

# build the SPARQL query string
sparql_string = '\
SELECT distinct ?taxonRank ?idCITES  ?nomSc \
WHERE { \
  ?item wdt:P2040 ?idCITES . \
  ?item wdt:P225 ?nomSc . \
  ?item wdt:P105 ?taxonRank \
  OPTIONAL { ?item wdt:P18 ?image . } \
  SERVICE wikibase:label { bd:serviceParam wikibase:language "fr" } \
  } \
LIMIT 20  \
'

# set custom header required by Wikidata service ; pass them in as a dict
# (optional? - the GET call seems to work from Python without this)
header ={'User-Agent':'EndangeredSpeciesIdentifier/0.1 (contact@okfnfr; http://cites.okfn.fr/en/home/)'}

# specify url parameters ; pass in as a dict
params = {'format':'json','query':sparql_string}

# make the HTTP GET call
r = requests.get(url, params=params, headers=header)

# examine the json returned
print( r.json() )

# display the results without the query metadata
print( r.json()['results']['bindings'] )

# display only the first result
print( r.json()['results']['bindings'][0] )