-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
executable file
·186 lines (152 loc) · 6.03 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
import argparse
import csv
import io
import json
import logging
import os
from urllib.request import Request, urlopen
import sqlite3
import openpyxl
import sqlite_utils
from bs4 import BeautifulSoup
def fetch(url):
return urlopen(
Request(
url,
headers={
"User-Agent": "NINA-importer/1.0",
},
)
) # has artsdatabanken blocked Python-urllib/* user-agent?
def parse_csv(fp, encoding="utf-8", lineterminator="\r\n", **kwargs):
decoded = fp.read().decode(encoding)
return csv.DictReader(decoded.split(lineterminator), **kwargs)
def parse_excel(fp, sheet):
buffer = io.BytesIO(fp.read())
workbook = openpyxl.load_workbook(buffer)
values = workbook[sheet].values
header = next(values)
return (dict(zip(header, row)) for row in values)
def parse_json(fp):
return json.load(fp)
def autocast(obj):
for row in obj:
for key, value in row.items():
logging.debug("Evaluate value: %s" % value)
if not isinstance(value, str):
logging.debug("Keeping type %s" % type(value))
continue
for function in (int, float):
try:
row[key] = function(value)
logging.debug("Casted to %s" % type(row[key]))
break
except ValueError:
pass
else:
logging.debug("Fallback to string")
yield row
def recipe_fab2023():
url = "https://artsdatabanken.no/lister/fremmedartslista/2023?Export=true"
logging.info("Fetching " + url)
return autocast(parse_excel(fetch(url), "Vurderinger"))
def recipe_fab2018():
url = "https://artsdatabanken.no/Fab2018/api/export/csv"
logging.info("Fetching " + url)
return autocast(parse_csv(fetch(url), encoding="utf-16le", delimiter=";"))
def recipe_rodlista():
url = "https://artsdatabanken.no/lister/rodlisteforarter/2021?Export=true"
logging.info("Fetching " + url)
return autocast(parse_excel(fetch(url), "Vurderinger"))
def recipe_ninkode(version):
url = f"https://nin-kode-api.artsdatabanken.no/{version}/koder/allekoder"
logging.info("Fetching " + url)
for row in autocast(parse_json(fetch(url))):
yield {
"KodeId": row["Kode"]["Id"],
"KodeDefinisjon": row["Kode"]["Definisjon"],
"OverordnetKodeId": row.get("Overordnet", {}).get("Kode", {}).get("Id"),
"Navn": row["Navn"],
"Kategori": row["Kategori"],
}
def recipe_taxongroups():
url = "https://artskart.artsdatabanken.no/publicapi/api/lookup?context=taxongroup"
logging.info("Fetching " + url)
return autocast(parse_json(fetch(url)))
def recipe_specie_by_group(group):
url = f"https://artskart.artsdatabanken.no/publicapi/api/taxon?term=&taxonGroups={group}&take=-1"
logging.info("Fetching " + url)
for row in parse_json(fetch(url)):
simple_row = {}
for key, value in row.items():
if isinstance(value, dict):
pass
elif isinstance(value, list):
pass
else:
simple_row[key] = value
yield simple_row
def recipe_species():
for taxongroup in recipe_taxongroups():
yield from recipe_specie_by_group(taxongroup["Key"])
def recipe_artsvariabler():
url = "https://www.artsdatabanken.no/Pages/221282/Kodeliste_artsvariabler"
soup = BeautifulSoup(fetch(url), features="html.parser")
rows = soup.find("table").findAll("tr")
header = [el.text for el in rows[0].findAll("td")]
artsgruppe = None
for row in rows[1:]:
artsgruppe_new, kode, navn = [el.text for el in row.findAll("td")]
if artsgruppe_new.strip():
artsgruppe = artsgruppe_new
yield dict(zip(header, [artsgruppe, kode, navn]))
def recipe_nin(url):
url = url.replace("//nin.", "//data.") + "/metadata.json"
for row in parse_json(fetch(url))["barn"]:
yield {
"kode": row["kode"],
"kartkode": row["kartkode"],
"tittel_nb": row["tittel"]["nb"],
"tittel_la": row["tittel"].get("la"),
}
def main(database_path, recreate):
connection = sqlite3.connect(database_path)
db = sqlite_utils.Database(connection, recreate=recreate)
db["species"].insert_all(recipe_species(), pk="Id")
db["fab-2023"].insert_all(recipe_fab2023(), pk="Id for vurderingen")
db["fab-2018"].insert_all(recipe_fab2018(), pk="Id")
db["rødlista-2021"].insert_all(recipe_rodlista(), pk="Id")
db["ninkode-1_0"].insert_all(recipe_ninkode(version="v1"), pk="KodeId")
db["ninkode-2_3"].insert_all(recipe_ninkode(version="v2.3"), pk="KodeId")
db["artsvariabler"].insert_all(recipe_artsvariabler(), pk="Kode")
db["nin_bs_1ar"].insert_all(
recipe_nin(
"https://nin.artsdatabanken.no/Natur_i_Norge/Natursystem/Beskrivelsessystem/Artssammensetning/Relativ_del-artsgruppesammensetning"
),
pk="kode",
)
# db["taxongroups"].insert_all(recipe_taxongroups(), pk="Key")
# Add FTS Support
connection.execute('CREATE VIRTUAL TABLE IF NOT EXISTS species_fts USING FTS5(ValidScientificName, tokenize="trigram", content="species", content_rowid="Id");')
connection.execute('INSERT INTO species_fts (rowid, ValidScientificName) SELECT Id, ValidScientificName FROM species;')
connection.commit()
if __name__ == "__main__":
logging.basicConfig(level=os.getenv("LOGGING_LEVEL", "WARNING"))
parser = argparse.ArgumentParser(
description="Fetch common data from artsdatabanken.no",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--database_path",
help="Path of the database to create or update",
default="common.sqlite",
)
parser.add_argument(
"--recreate",
help="Recreate the database",
action=argparse.BooleanOptionalAction,
default=False,
)
args = parser.parse_args()
main(args.database_path, args.recreate)