@@ -4,19 +4,19 @@ var fs = require('fs')
4
4
var path = require ( 'path' )
5
5
var https = require ( 'https' )
6
6
var concat = require ( 'concat-stream' )
7
- var unzip = require ( 'unzip ' )
7
+ var yauzl = require ( 'yauzl ' )
8
8
var dsv = require ( 'd3-dsv' )
9
9
var bail = require ( 'bail' )
10
10
11
- var found
11
+ var found = false
12
12
13
- var SCOPES = {
13
+ var scopes = {
14
14
I : 'individual' ,
15
15
M : 'macrolanguage' ,
16
16
S : 'special'
17
17
}
18
18
19
- var TYPES = {
19
+ var types = {
20
20
A : 'ancient' ,
21
21
C : 'constructed' ,
22
22
E : 'extinct' ,
@@ -25,31 +25,60 @@ var TYPES = {
25
25
S : 'special'
26
26
}
27
27
28
+ // Note:
29
+ // You can find download links here:
30
+ // <https://iso639-3.sil.org/code_tables/download_tables>
31
+ // Just get the complete code tables in UTF-8.
32
+
28
33
https
29
34
. request (
30
- 'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3_Code_Tables_20180123 .zip' ,
35
+ 'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3_Code_Tables_20190408 .zip' ,
31
36
onrequest
32
37
)
33
38
. end ( )
34
39
35
- process . on ( 'exit' , onexit )
36
-
37
- function onexit ( ) {
38
- if ( ! found ) {
39
- throw new Error ( 'Could not find expected file' )
40
- }
40
+ function onrequest ( res ) {
41
+ res
42
+ . pipe ( fs . createWriteStream ( 'archive.zip' ) )
43
+ . on ( 'close' , onclose )
44
+ . on ( 'error' , bail )
41
45
}
42
46
43
- function onrequest ( response ) {
44
- response . pipe ( new unzip . Parse ( ) ) . on ( 'entry' , onentry )
47
+ function onclose ( ) {
48
+ yauzl . open ( 'archive.zip' , { lazyEntries : true } , onopen )
45
49
}
46
50
47
- function onentry ( entry ) {
48
- if ( path . basename ( entry . path ) === 'iso-639-3_20180123.tab' ) {
51
+ function onopen ( err , archive ) {
52
+ bail ( err )
53
+
54
+ read ( )
55
+
56
+ archive . on ( 'entry' , onentry )
57
+ archive . on ( 'end' , onend )
58
+
59
+ function onentry ( entry ) {
60
+ if ( path . basename ( entry . fileName ) !== 'iso-639-3_20190408.tab' ) {
61
+ return read ( )
62
+ }
63
+
49
64
found = true
50
- entry . pipe ( concat ( onconcat ) )
51
- } else {
52
- entry . autodrain ( )
65
+ archive . openReadStream ( entry , onreadstream )
66
+ }
67
+
68
+ function onreadstream ( err , rs ) {
69
+ bail ( err )
70
+ rs . pipe ( concat ( onconcat ) ) . on ( 'error' , bail )
71
+ rs . on ( 'end' , read )
72
+ }
73
+
74
+ function read ( ) {
75
+ archive . readEntry ( )
76
+ }
77
+ }
78
+
79
+ function onend ( ) {
80
+ if ( ! found ) {
81
+ throw new Error ( 'File not found' )
53
82
}
54
83
}
55
84
@@ -59,15 +88,14 @@ function onconcat(body) {
59
88
fs . writeFile ( 'index.json' , JSON . stringify ( data , 0 , 2 ) + '\n' , bail )
60
89
}
61
90
62
- function mapper ( language ) {
91
+ function mapper ( d ) {
63
92
return {
64
- name : language . Ref_Name || null ,
65
- type : TYPES [ language . Language_Type ] ,
66
- scope : SCOPES [ language . Scope ] ,
67
- iso6393 : language [ 'Id' ] , // There’s a `<U+FEFF>`
68
- // in there, I don’t know why, but meh.
69
- iso6392B : language . Part2B || null ,
70
- iso6392T : language . Part2T || null ,
71
- iso6391 : language . Part1 || null
93
+ name : d . Ref_Name || null ,
94
+ type : types [ d . d_Type ] ,
95
+ scope : scopes [ d . Scope ] ,
96
+ iso6393 : d . Id ,
97
+ iso6392B : d . Part2B || null ,
98
+ iso6392T : d . Part2T || null ,
99
+ iso6391 : d . Part1 || null
72
100
}
73
101
}
0 commit comments