Skip to content

Commit 7ebdbd1

Browse files
authored
Merge pull request #138 from jaredkhan/iucn-wikidata
IUCN wikidata
2 parents 03f6d2d + 6e2c6bd commit 7ebdbd1

8 files changed

Lines changed: 71 additions & 40 deletions

File tree

dvc.lock

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ stages:
77
deps:
88
- path: data/Wiki/wd_JSON/latest-all-json-bz2-url.txt
99
hash: md5
10-
md5: e094b0f57c0c14e1016842c2dac5482e
10+
md5: aa1692624401f4a1c759bc9526787fd7
1111
size: 90
1212
outs:
1313
- path: data/filtered/OneZoom_latest-all.json
1414
hash: md5
15-
md5: 65b22a5c761c78d79b30faf871d1e404
16-
size: 1542990225
15+
md5: e6f69def9d6fa2bb4b90060509c079bb
16+
size: 1567956473
1717
extract_wikidata_titles:
1818
cmd: extract_wikidata_titles data/filtered/OneZoom_latest-all.json -o
1919
data/filtered/wikidata_titles.txt
2020
deps:
2121
- path: data/filtered/OneZoom_latest-all.json
2222
hash: md5
23-
md5: 65b22a5c761c78d79b30faf871d1e404
24-
size: 1542990225
23+
md5: e6f69def9d6fa2bb4b90060509c079bb
24+
size: 1567956473
2525
outs:
2626
- path: data/filtered/wikidata_titles.txt
2727
hash: md5
28-
md5: e498b85311c8a84a0d5157a0bbbcb23f
29-
size: 9382189
28+
md5: 8a8d1fc7e30c8c90890a761a4ba22f2d
29+
size: 9540289
3030
download_wikipedia_sql:
3131
cmd: wget --progress=bar:force -O data/Wiki/wp_SQL/enwiki-page.sql.gz "$(cat
3232
data/Wiki/wp_SQL/enwiki-page-sql-gz-url.txt)"
@@ -46,13 +46,13 @@ stages:
4646
deps:
4747
- path: data/filtered/wikidata_titles.txt
4848
hash: md5
49-
md5: e498b85311c8a84a0d5157a0bbbcb23f
50-
size: 9382189
49+
md5: 8a8d1fc7e30c8c90890a761a4ba22f2d
50+
size: 9540289
5151
outs:
5252
- path: data/filtered/pageviews/
5353
hash: md5
54-
md5: f021afa12f9d7c893412a0b2980ab187.dir
55-
size: 104535557
54+
md5: 701c03fc874aa8a4d5578a6a74267bee.dir
55+
size: 128216689
5656
nfiles: 13
5757
filter_wikipedia_sql:
5858
cmd: filter_wikipedia_sql data/Wiki/wp_SQL/enwiki-page.sql.gz
@@ -65,13 +65,13 @@ stages:
6565
size: 2397370114
6666
- path: data/filtered/wikidata_titles.txt
6767
hash: md5
68-
md5: e498b85311c8a84a0d5157a0bbbcb23f
69-
size: 9382189
68+
md5: 8a8d1fc7e30c8c90890a761a4ba22f2d
69+
size: 9540289
7070
outs:
7171
- path: data/filtered/OneZoom_enwiki-latest-page.sql
7272
hash: md5
73-
md5: af577077774060f78a752f22e2f1c6d3
74-
size: 21445833
73+
md5: f7437ba9e2a89ff658749e434a38b39a
74+
size: 21507159
7575
download_opentree:
7676
cmd: download_opentree --version v16.1 --output-dir data/OpenTree
7777
params:
@@ -225,20 +225,20 @@ stages:
225225
size: 417054016
226226
- path: data/filtered/OneZoom_enwiki-latest-page.sql
227227
hash: md5
228-
md5: af577077774060f78a752f22e2f1c6d3
229-
size: 21445833
228+
md5: f7437ba9e2a89ff658749e434a38b39a
229+
size: 21507159
230230
- path: data/filtered/OneZoom_latest-all.json
231231
hash: md5
232-
md5: 65b22a5c761c78d79b30faf871d1e404
233-
size: 1542990225
232+
md5: e6f69def9d6fa2bb4b90060509c079bb
233+
size: 1567956473
234234
- path: data/filtered/OneZoom_provider_ids.csv
235235
hash: md5
236236
md5: f7c9bb8374957c07168bec36d6591347
237237
size: 221682224
238238
- path: data/filtered/pageviews/
239239
hash: md5
240-
md5: f021afa12f9d7c893412a0b2980ab187.dir
241-
size: 104535557
240+
md5: 701c03fc874aa8a4d5578a6a74267bee.dir
241+
size: 128216689
242242
nfiles: 13
243243
params:
244244
params.yaml:
@@ -249,8 +249,8 @@ stages:
249249
outs:
250250
- path: data/output_files/
251251
hash: md5
252-
md5: 81dfa9c3747bbe94c57161cc8e22d5c9.dir
253-
size: 1158499655
252+
md5: 3a18d1302a90a924d080c6a84b2429ad.dir
253+
size: 1158320679
254254
nfiles: 7
255255
make_js_treefiles:
256256
cmd:
@@ -260,16 +260,16 @@ stages:
260260
deps:
261261
- path: data/output_files/
262262
hash: md5
263-
md5: 81dfa9c3747bbe94c57161cc8e22d5c9.dir
264-
size: 1158499655
263+
md5: 3a18d1302a90a924d080c6a84b2429ad.dir
264+
size: 1158320679
265265
nfiles: 7
266266
discover_latest_wikidata_dump_url:
267267
cmd: discover_latest_wikidata_dump_url >
268268
data/Wiki/wd_JSON/latest-all-json-bz2-url.txt
269269
outs:
270270
- path: data/Wiki/wd_JSON/latest-all-json-bz2-url.txt
271271
hash: md5
272-
md5: e094b0f57c0c14e1016842c2dac5482e
272+
md5: aa1692624401f4a1c759bc9526787fd7
273273
size: 90
274274
discover_latest_enwiki_sql_url:
275275
cmd: discover_latest_enwiki_sql_url >

oz_tree_build/images_and_vernaculars/get_wiki_images.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from PIL import Image
4545

4646
from .._OZglobals import src_flags
47+
from ..user_agent import USER_AGENT_HEADERS
4748
from ..utilities.db_helper import (
4849
connect_to_database,
4950
default_appconfig,
@@ -70,9 +71,6 @@
7071
"img",
7172
)
7273

73-
# See https://meta.wikimedia.org/wiki/User-Agent_policy
74-
wiki_http_headers = {"User-Agent": "OneZoomBot/0.1 (https://www.onezoom.org/; mail@onezoom.org) get-wiki-images/0.1"}
75-
7674

7775
# Copied from OZTree/OZprivate/ServerScripts/Utilities/getEOL_crops.py
7876
def subdir_name(doID):
@@ -95,7 +93,7 @@ def make_http_request_with_retries(url):
9593
retries = 6
9694
delay = 1
9795
for i in range(retries):
98-
r = requests.get(url, headers=wiki_http_headers)
96+
r = requests.get(url, headers=USER_AGENT_HEADERS)
9997
if r.status_code == 200:
10098
return r
10199

@@ -371,7 +369,7 @@ def save_wiki_image(db, leaf_data, image_name, src, src_id, rating, output_dir,
371369

372370
# Download the uncropped image
373371
uncropped_image_path = f"{image_dir}/{src_id}_uncropped.jpg"
374-
response = requests.get(image_url, headers=wiki_http_headers)
372+
response = requests.get(image_url, headers=USER_AGENT_HEADERS)
375373
response.raise_for_status()
376374
with open(uncropped_image_path, "wb") as f:
377375
for chunk in response.iter_content(1024):

oz_tree_build/user_agent.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Shared HTTP User-Agent for outbound requests from tree-build."""
2+
3+
# See https://meta.wikimedia.org/wiki/User-Agent_policy
4+
USER_AGENT = "OneZoom-tree-build/1.0 (https://www.onezoom.org/; mail@onezoom.org)"
5+
USER_AGENT_HEADERS = {"User-Agent": USER_AGENT}

oz_tree_build/utilities/download_and_filter_pageviews.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import tempfile
1717
import urllib.request
1818

19+
from oz_tree_build.user_agent import USER_AGENT_HEADERS
20+
1921
from .file_utils import stream_bz2_lines_from_url
2022
from .filter_pageviews import filter_pageview_lines, write_filtered_pageviews
2123
from .filter_wikidata import load_titles_file
@@ -28,7 +30,7 @@
2830
def _fetch_index(url):
2931
"""Fetch an Apache directory index page and return its HTML."""
3032
print(f"Fetching index from {url}")
31-
req = urllib.request.Request(url, headers={"User-Agent": "OneZoom-tree-build/1.0"})
33+
req = urllib.request.Request(url, headers=USER_AGENT_HEADERS)
3234
with urllib.request.urlopen(req, timeout=30) as resp:
3335
return resp.read().decode("utf-8")
3436

oz_tree_build/utilities/download_and_filter_wikidata.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import tempfile
1414
import urllib.request
1515

16+
from oz_tree_build.user_agent import USER_AGENT_HEADERS
17+
1618
from .file_utils import stream_bz2_lines_from_url
1719
from .filter_wikidata import filter_wikidata
1820

@@ -28,7 +30,14 @@ def discover_latest_wikidata_dump_url(base_url=WIKIDATA_ENTITIES_URL, timeout=30
2830
folder_re = re.compile(r'href="(\d{8})/"')
2931
file_re_template = r'href="(wikidata-{date}-all\.json\.bz2)"'
3032

31-
index_html = urllib.request.urlopen(base_url, timeout=timeout).read().decode()
33+
index_html = (
34+
urllib.request.urlopen(
35+
urllib.request.Request(base_url, headers=USER_AGENT_HEADERS),
36+
timeout=timeout,
37+
)
38+
.read()
39+
.decode()
40+
)
3241

3342
dates = sorted(folder_re.findall(index_html), reverse=True)
3443
if not dates:
@@ -38,7 +47,14 @@ def discover_latest_wikidata_dump_url(base_url=WIKIDATA_ENTITIES_URL, timeout=30
3847
folder_url = f"{base_url}{date}/"
3948
logger.info("Checking %s", folder_url)
4049
try:
41-
folder_html = urllib.request.urlopen(folder_url, timeout=timeout).read().decode()
50+
folder_html = (
51+
urllib.request.urlopen(
52+
urllib.request.Request(folder_url, headers=USER_AGENT_HEADERS),
53+
timeout=timeout,
54+
)
55+
.read()
56+
.decode()
57+
)
4258
except urllib.error.URLError as exc:
4359
logger.warning("Could not fetch %s: %s", folder_url, exc)
4460
continue

oz_tree_build/utilities/file_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import shutil
1010
import subprocess
1111

12+
from oz_tree_build.user_agent import USER_AGENT
13+
1214
__author__ = "David Ebbo"
1315

1416

@@ -40,7 +42,7 @@ def stream_bz2_lines_from_url(url, read_timeout=120):
4042
"-",
4143
"--connect-timeout=30",
4244
f"--read-timeout={read_timeout}",
43-
"--header=User-Agent: OneZoom-tree-build/1.0",
45+
f"--header=User-Agent: {USER_AGENT}",
4446
url,
4547
],
4648
stdout=subprocess.PIPE,

oz_tree_build/utilities/filter_wikidata.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@
4040
"P961": [{"mainsnak": {"datavalue": {"value": KEEP}}}],
4141
"P9157": [{"mainsnak": {"datavalue": {"value": KEEP}}}],
4242
"P3151": [{"mainsnak": {"datavalue": {"value": KEEP}}}],
43-
"P141": [{"references": [{"snaks": {"P627": [{"datavalue": {"value": KEEP}}]}}]}],
43+
"P627": [{"mainsnak": {"datavalue": {"value": KEEP}}}],
44+
"P141": [
45+
{
46+
"mainsnak": {"datavalue": {"value": {"numeric-id": KEEP}}},
47+
"rank": KEEP,
48+
"qualifiers": {"P585": [{"datavalue": {"value": {"time": KEEP}}}]},
49+
"references": [{"snaks": {"P627": [{"datavalue": {"value": KEEP}}]}}],
50+
}
51+
],
4452
"P1420": [{"mainsnak": {"datavalue": {"value": {"numeric-id": KEEP}}}}],
4553
"P18": [
4654
{
@@ -136,9 +144,7 @@ def trim_and_write_json_item(json_item, filtered_wiki_f):
136144
else:
137145
potential_extra_json_items.append(("vernacular", vernaculars_matches, json_item))
138146

139-
logging.info(
140-
"Writing extra lines at the end of the file " f"(subset of {len(potential_extra_json_items)} lines)"
141-
)
147+
logging.info(f"Writing extra lines at the end of the file (subset of {len(potential_extra_json_items)} lines)")
142148

143149
for desc, linked_qids, json_item in potential_extra_json_items:
144150
for qid in linked_qids:

oz_tree_build/wiki_extraction/mwparserfromhell_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import mwparserfromhell
1010
import requests_cache
1111

12+
from oz_tree_build.user_agent import USER_AGENT_HEADERS
13+
1214
session = requests_cache.CachedSession("http_cache")
1315

1416
API_URL = "https://en.wikipedia.org/w/api.php"
1517

1618

1719
def wiki_api_request(params):
18-
headers = {"User-Agent": "My-Bot-Name/1.0"}
20+
headers = USER_AGENT_HEADERS
1921
# Use this line to avoid caching when testing
2022
# headers["Cache-Control"] = "no-cache"
2123
max_retries = 30

0 commit comments

Comments
 (0)