(* Downloading data for a given key from gbif.org *) maintainDataGbif[key0_] := Module[{id, file, url, textcondit, r, matchlist, varlist, data, varn, matchn, value, results, synonyms, kingdom, phylum, class, order, family, genus, species, key, rank, scientificName, nameType, synonym, basionym, taxonomicStatus, basionymKey, acceptedKey, accepted}, id = ToString[key0]; dirDataGbif = FileNameJoin[{dirwork, "data", "ids", "gbif"}]; checkdir1[dirDataGbif]; file = FileNameJoin[{dirDataGbif, id <> ".json"}]; If[! FileExistsQ[file], url = urlGbif <> "species/" <> id <> "?limit=999"; textcondit = (! StringFreeQ[#, id]) &; r = urlsave[url, file, textcondit]; If[FileExistsQ[file], Print["Downloading data for the key ", key0, " completed."]]]; keyData[key0, "ifexists"] = False; (* Readind data *) matchlist = {"kingdom", "phylum", "class", "order", "family", "genus", "species", "key", "rank", "scientificName", "nameType", "synonym", "basionym", "taxonomicStatus", "basionymKey", "acceptedKey", "accepted"}; Clear /@ matchlist; varlist = ToExpression /@ matchlist; If[key0 =!= 0, noPrint["Reading data from file ", file]; If[FileExistsQ[file], keyData[key0, "ifexists"] = True; keyData[key0, "link"] = "https://www.gbif.org/species/" <> ToString[key0]; data = Import[file, "JSON"]; If[data === $Failed, Print["Error importing file ", file, " as JSON"], Do[ varn = varlist[[n]]; matchn = matchlist[[n]]; value = matchn /. data; Evaluate[varn] = If[value =!= matchn, value, "Undefined"]; noPrint[matchn, " -> ", varn]; keyData[key0, matchn] = varn, {n, Length[matchlist]}], Print["Error: Reading gbif-data: file ", file, " does not exist."]; ]]; (* Adding possibly missing synonym status *) If[keyData[key0, "synonym"] === "Undefined", If[keyData[key0, "taxonomicStatus"] === "ACCEPTED", keyData[key0, "synonym"] = False]; If[keyData[key0, "taxonomicStatus"] === "SYNONYM", keyData[key0, "synonym"] = True]]; ]; (* Synonyms *) file = FileNameJoin[{dirDataGbif, id <> "-synonyms.json"}]; If[! FileExistsQ[file], url = urlGbif <> "species/" <> id <> "/synonyms?limit=999"; textcondit = (! StringFreeQ[#, "results"]) &; r = urlsave[url, file, textcondit]; If[FileExistsQ[file], Print["Downloading synonyms for the key ", key0, " completed."]]]; If[!FileExistsQ[file], Print["Error: Downloading synonyms for the key ", key0, " failed."]]; (* Reading data *) keyData[key0, "synonyms"] = "Undefined"; If[keyData[key0, "ifexists"], noPrint["Reading data from file ", file]; If[FileExistsQ[file], data = Import[file, "JSON"]; If[data === $Failed, Print["Error importing file ", file, " as JSON"], results = "results" /. data; If[results =!= "results", synonyms = "scientificName" /. results; If[synonyms =!= "synonyms", keyData[key0, "synonyms"] = synonyms; ]; ]]; ]; ]; ];