@tricoteuses/senat 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/lib/config.d.ts +1 -0
  2. package/lib/config.js +14 -45
  3. package/lib/databases.js +86 -143
  4. package/lib/datasets.js +78 -83
  5. package/lib/index.d.ts +7 -4
  6. package/lib/index.js +42 -419
  7. package/lib/loaders.js +149 -654
  8. package/lib/model/ameli.js +83 -21
  9. package/lib/model/debats.js +0 -1
  10. package/lib/model/dosleg.d.ts +1 -1
  11. package/lib/model/dosleg.js +179 -73
  12. package/lib/model/index.d.ts +3 -3
  13. package/lib/model/index.js +12 -46
  14. package/lib/model/questions.js +68 -39
  15. package/lib/model/sens.js +383 -113
  16. package/lib/model/texte.js +220 -290
  17. package/lib/model/util.js +9 -26
  18. package/lib/raw_types/ameli.js +5 -6
  19. package/lib/raw_types/debats.js +5 -6
  20. package/lib/raw_types/dosleg.js +5 -6
  21. package/lib/raw_types/questions.js +5 -6
  22. package/lib/raw_types/sens.js +5 -6
  23. package/lib/raw_types_schemats/ameli.js +1 -43
  24. package/lib/raw_types_schemats/debats.js +1 -22
  25. package/lib/raw_types_schemats/dosleg.js +1 -96
  26. package/lib/raw_types_schemats/questions.js +1 -22
  27. package/lib/raw_types_schemats/sens.js +1 -112
  28. package/lib/scripts/convert_data.js +181 -631
  29. package/lib/scripts/datautil.js +17 -60
  30. package/lib/scripts/parse_textes.js +46 -129
  31. package/lib/scripts/retrieve_documents.js +247 -513
  32. package/lib/scripts/retrieve_open_data.js +211 -368
  33. package/lib/scripts/retrieve_senateurs_photos.js +144 -239
  34. package/lib/scripts/shared/cli_helpers.js +30 -30
  35. package/lib/scripts/shared/util.js +28 -94
  36. package/lib/strings.js +20 -45
  37. package/lib/types/ameli.d.ts +1 -1
  38. package/lib/types/ameli.js +14 -25
  39. package/lib/types/debats.d.ts +1 -1
  40. package/lib/types/debats.js +3 -21
  41. package/lib/types/dosleg.d.ts +1 -1
  42. package/lib/types/dosleg.js +152 -119
  43. package/lib/types/questions.d.ts +1 -1
  44. package/lib/types/questions.js +1 -13
  45. package/lib/types/sens.d.ts +1 -1
  46. package/lib/types/sens.js +1 -13
  47. package/lib/types/sessions.js +44 -49
  48. package/lib/types/texte.js +17 -22
  49. package/lib/validators/config.js +47 -111
  50. package/lib/validators/senat.js +1 -5
  51. package/package.json +16 -38
  52. package/lib/aggregates.d.ts +0 -52
  53. package/lib/aggregates.mjs +0 -930
  54. package/lib/aggregates.ts +0 -833
  55. package/lib/config.mjs +0 -16
  56. package/lib/config.ts +0 -26
  57. package/lib/data/legislatures.json +0 -38
  58. package/lib/databases.mjs +0 -57
  59. package/lib/databases.ts +0 -71
  60. package/lib/datasets.mjs +0 -78
  61. package/lib/datasets.ts +0 -118
  62. package/lib/fields.d.ts +0 -10
  63. package/lib/fields.mjs +0 -68
  64. package/lib/fields.ts +0 -29
  65. package/lib/index.mjs +0 -4
  66. package/lib/index.ts +0 -42
  67. package/lib/inserters.d.ts +0 -98
  68. package/lib/inserters.mjs +0 -500
  69. package/lib/inserters.ts +0 -521
  70. package/lib/loaders.mjs +0 -158
  71. package/lib/loaders.ts +0 -271
  72. package/lib/model/ameli.mjs +0 -84
  73. package/lib/model/ameli.ts +0 -100
  74. package/lib/model/debats.mjs +0 -1
  75. package/lib/model/debats.ts +0 -0
  76. package/lib/model/dosleg.mjs +0 -196
  77. package/lib/model/dosleg.ts +0 -240
  78. package/lib/model/index.mjs +0 -4
  79. package/lib/model/index.ts +0 -14
  80. package/lib/model/questions.mjs +0 -71
  81. package/lib/model/questions.ts +0 -93
  82. package/lib/model/sens.mjs +0 -415
  83. package/lib/model/sens.ts +0 -516
  84. package/lib/model/texte.mjs +0 -208
  85. package/lib/model/texte.ts +0 -229
  86. package/lib/model/util.mjs +0 -19
  87. package/lib/model/util.ts +0 -32
  88. package/lib/raw_types/ameli.mjs +0 -5
  89. package/lib/raw_types/ameli.ts +0 -951
  90. package/lib/raw_types/debats.mjs +0 -5
  91. package/lib/raw_types/debats.ts +0 -222
  92. package/lib/raw_types/dosleg.mjs +0 -5
  93. package/lib/raw_types/dosleg.ts +0 -3625
  94. package/lib/raw_types/questions.mjs +0 -5
  95. package/lib/raw_types/questions.ts +0 -427
  96. package/lib/raw_types/sens.mjs +0 -5
  97. package/lib/raw_types/sens.ts +0 -4499
  98. package/lib/raw_types_kysely/ameli.d.ts +0 -6
  99. package/lib/raw_types_kysely/ameli.mjs +0 -7
  100. package/lib/raw_types_kysely/ameli.ts +0 -6
  101. package/lib/raw_types_kysely/debats.d.ts +0 -6
  102. package/lib/raw_types_kysely/debats.mjs +0 -7
  103. package/lib/raw_types_kysely/debats.ts +0 -6
  104. package/lib/raw_types_kysely/dosleg.d.ts +0 -6
  105. package/lib/raw_types_kysely/dosleg.mjs +0 -7
  106. package/lib/raw_types_kysely/dosleg.ts +0 -6
  107. package/lib/raw_types_kysely/questions.d.ts +0 -6
  108. package/lib/raw_types_kysely/questions.mjs +0 -7
  109. package/lib/raw_types_kysely/questions.ts +0 -6
  110. package/lib/raw_types_kysely/sens.d.ts +0 -6
  111. package/lib/raw_types_kysely/sens.mjs +0 -7
  112. package/lib/raw_types_kysely/sens.ts +0 -6
  113. package/lib/raw_types_kysely/texte.d.ts +0 -45
  114. package/lib/raw_types_kysely/texte.mjs +0 -7
  115. package/lib/raw_types_kysely/texte.ts +0 -53
  116. package/lib/raw_types_schemats/ameli.mjs +0 -2
  117. package/lib/raw_types_schemats/ameli.ts +0 -601
  118. package/lib/raw_types_schemats/debats.mjs +0 -2
  119. package/lib/raw_types_schemats/debats.ts +0 -145
  120. package/lib/raw_types_schemats/dosleg.mjs +0 -2
  121. package/lib/raw_types_schemats/dosleg.ts +0 -2195
  122. package/lib/raw_types_schemats/questions.mjs +0 -2
  123. package/lib/raw_types_schemats/questions.ts +0 -251
  124. package/lib/raw_types_schemats/sens.mjs +0 -2
  125. package/lib/raw_types_schemats/sens.ts +0 -2907
  126. package/lib/scripts/convert_data.mjs +0 -181
  127. package/lib/scripts/convert_data.ts +0 -243
  128. package/lib/scripts/datautil.mjs +0 -16
  129. package/lib/scripts/datautil.ts +0 -19
  130. package/lib/scripts/images/transparent_150x192.jpg +0 -0
  131. package/lib/scripts/images/transparent_155x225.jpg +0 -0
  132. package/lib/scripts/parse_textes.mjs +0 -46
  133. package/lib/scripts/parse_textes.ts +0 -65
  134. package/lib/scripts/retrieve_documents.mjs +0 -249
  135. package/lib/scripts/retrieve_documents.ts +0 -298
  136. package/lib/scripts/retrieve_open_data.mjs +0 -217
  137. package/lib/scripts/retrieve_open_data.ts +0 -274
  138. package/lib/scripts/retrieve_senateurs_photos.mjs +0 -147
  139. package/lib/scripts/retrieve_senateurs_photos.ts +0 -177
  140. package/lib/scripts/retrieve_textes.d.ts +0 -1
  141. package/lib/scripts/retrieve_textes.mjs +0 -328
  142. package/lib/scripts/retrieve_textes.ts +0 -143
  143. package/lib/scripts/shared/cli_helpers.ts +0 -36
  144. package/lib/scripts/shared/util.ts +0 -33
  145. package/lib/src/aggregates.d.ts +0 -52
  146. package/lib/src/aggregates.mjs +0 -726
  147. package/lib/src/config.d.ts +0 -2
  148. package/lib/src/config.mjs +0 -16
  149. package/lib/src/databases.d.ts +0 -18
  150. package/lib/src/databases.mjs +0 -55
  151. package/lib/src/datasets.d.ts +0 -28
  152. package/lib/src/datasets.mjs +0 -78
  153. package/lib/src/fields.d.ts +0 -10
  154. package/lib/src/fields.mjs +0 -22
  155. package/lib/src/index.d.ts +0 -8
  156. package/lib/src/index.mjs +0 -7
  157. package/lib/src/inserters.d.ts +0 -98
  158. package/lib/src/inserters.mjs +0 -360
  159. package/lib/src/loaders.d.ts +0 -36
  160. package/lib/src/loaders.mjs +0 -107
  161. package/lib/src/model/ameli.d.ts +0 -4
  162. package/lib/src/model/ameli.js +0 -57
  163. package/lib/src/model/debats.d.ts +0 -4
  164. package/lib/src/model/debats.js +0 -43
  165. package/lib/src/model/dosleg.d.ts +0 -197
  166. package/lib/src/model/dosleg.js +0 -169
  167. package/lib/src/model/index.d.ts +0 -4
  168. package/lib/src/model/index.js +0 -4
  169. package/lib/src/model/questions.d.ts +0 -89
  170. package/lib/src/model/questions.js +0 -76
  171. package/lib/src/model/sens.d.ts +0 -390
  172. package/lib/src/model/sens.js +0 -339
  173. package/lib/src/model/texte.d.ts +0 -7
  174. package/lib/src/model/texte.js +0 -183
  175. package/lib/src/raw_types_kysely/ameli.d.ts +0 -915
  176. package/lib/src/raw_types_kysely/ameli.js +0 -5
  177. package/lib/src/raw_types_kysely/debats.d.ts +0 -207
  178. package/lib/src/raw_types_kysely/debats.js +0 -5
  179. package/lib/src/raw_types_kysely/dosleg.d.ts +0 -3532
  180. package/lib/src/raw_types_kysely/dosleg.js +0 -5
  181. package/lib/src/raw_types_kysely/questions.d.ts +0 -414
  182. package/lib/src/raw_types_kysely/questions.js +0 -5
  183. package/lib/src/raw_types_kysely/sens.d.ts +0 -4394
  184. package/lib/src/raw_types_kysely/sens.js +0 -5
  185. package/lib/src/raw_types_schemats/ameli.d.ts +0 -541
  186. package/lib/src/raw_types_schemats/ameli.js +0 -2
  187. package/lib/src/raw_types_schemats/debats.d.ts +0 -127
  188. package/lib/src/raw_types_schemats/debats.js +0 -2
  189. package/lib/src/raw_types_schemats/dosleg.d.ts +0 -2027
  190. package/lib/src/raw_types_schemats/dosleg.js +0 -2
  191. package/lib/src/raw_types_schemats/questions.d.ts +0 -231
  192. package/lib/src/raw_types_schemats/questions.js +0 -2
  193. package/lib/src/raw_types_schemats/sens.d.ts +0 -2709
  194. package/lib/src/raw_types_schemats/sens.js +0 -2
  195. package/lib/src/scripts/convert_data.d.ts +0 -1
  196. package/lib/src/scripts/convert_data.js +0 -95
  197. package/lib/src/scripts/datautil.d.ts +0 -5
  198. package/lib/src/scripts/datautil.js +0 -16
  199. package/lib/src/scripts/parse_textes.d.ts +0 -1
  200. package/lib/src/scripts/parse_textes.js +0 -47
  201. package/lib/src/scripts/retrieve_documents.d.ts +0 -1
  202. package/lib/src/scripts/retrieve_documents.js +0 -258
  203. package/lib/src/scripts/retrieve_open_data.d.ts +0 -1
  204. package/lib/src/scripts/retrieve_open_data.js +0 -214
  205. package/lib/src/scripts/retrieve_senateurs_photos.d.ts +0 -1
  206. package/lib/src/scripts/retrieve_senateurs_photos.js +0 -147
  207. package/lib/src/scripts/shared/cli_helpers.d.ts +0 -44
  208. package/lib/src/scripts/shared/cli_helpers.js +0 -32
  209. package/lib/src/scripts/shared/util.d.ts +0 -3
  210. package/lib/src/scripts/shared/util.js +0 -28
  211. package/lib/src/strings.d.ts +0 -1
  212. package/lib/src/strings.mjs +0 -18
  213. package/lib/src/types/ameli.d.ts +0 -10
  214. package/lib/src/types/ameli.js +0 -13
  215. package/lib/src/types/debats.d.ts +0 -4
  216. package/lib/src/types/debats.js +0 -2
  217. package/lib/src/types/dosleg.d.ts +0 -98
  218. package/lib/src/types/dosleg.js +0 -151
  219. package/lib/src/types/questions.d.ts +0 -2
  220. package/lib/src/types/questions.js +0 -1
  221. package/lib/src/types/sens.d.ts +0 -10
  222. package/lib/src/types/sens.js +0 -1
  223. package/lib/src/types/sessions.d.ts +0 -42
  224. package/lib/src/types/sessions.js +0 -43
  225. package/lib/src/types/texte.d.ts +0 -61
  226. package/lib/src/types/texte.js +0 -16
  227. package/lib/src/validators/config.d.ts +0 -1
  228. package/lib/src/validators/config.js +0 -54
  229. package/lib/src/validators/senat.d.ts +0 -0
  230. package/lib/src/validators/senat.js +0 -24
  231. package/lib/strings.mjs +0 -18
  232. package/lib/strings.ts +0 -26
  233. package/lib/types/ameli.mjs +0 -13
  234. package/lib/types/ameli.ts +0 -21
  235. package/lib/types/debats.mjs +0 -2
  236. package/lib/types/debats.ts +0 -6
  237. package/lib/types/dosleg.mjs +0 -151
  238. package/lib/types/dosleg.ts +0 -284
  239. package/lib/types/questions.mjs +0 -1
  240. package/lib/types/questions.ts +0 -3
  241. package/lib/types/sens.mjs +0 -1
  242. package/lib/types/sens.ts +0 -12
  243. package/lib/types/sessions.mjs +0 -43
  244. package/lib/types/sessions.ts +0 -42
  245. package/lib/types/texte.mjs +0 -16
  246. package/lib/types/texte.ts +0 -76
  247. package/lib/typings/windows-1252.d.js +0 -2
  248. package/lib/typings/windows-1252.d.mjs +0 -2
  249. package/lib/typings/windows-1252.d.ts +0 -11
  250. package/lib/validators/config.mjs +0 -54
  251. package/lib/validators/config.ts +0 -79
  252. package/lib/validators/senat.mjs +0 -24
  253. package/lib/validators/senat.ts +0 -26
@@ -1,217 +0,0 @@
1
- import assert from "assert";
2
- import { execSync } from "child_process";
3
- import commandLineArgs from "command-line-args";
4
- import fs from "fs-extra";
5
- // import fetch from "node-fetch"
6
- import path from "path";
7
- // import stream from "stream"
8
- import StreamZip from "node-stream-zip";
9
- import readline from "readline";
10
- // import util from "util"
11
- import windows1252 from "windows-1252";
12
- import config from "../config";
13
- import { datasets, getChosenFromEnabledDatasets } from "../datasets";
14
- import { commonOptions } from "./shared/cli_helpers";
15
- const badWindows1252CharacterRegex = /[\u0080-\u009f]/g;
16
- const optionsDefinitions = [
17
- ...commonOptions,
18
- {
19
- alias: "a",
20
- help: "all options: fetch, unzip, repair-encoding, import",
21
- name: "all",
22
- type: Boolean,
23
- },
24
- {
25
- alias: "c",
26
- help: "create TypeScript interfaces from databases schemas into src/raw_types_* directories",
27
- name: "schema",
28
- type: Boolean,
29
- },
30
- {
31
- alias: "e",
32
- help: "repair Windows CP 1252 encoding of SQL dumps",
33
- name: "repairEncoding",
34
- type: Boolean,
35
- },
36
- {
37
- alias: "f",
38
- help: "fetch datasets instead of retrieving them from files",
39
- name: "fetch",
40
- type: Boolean,
41
- },
42
- {
43
- alias: "i",
44
- help: "import SQL dumps into a freshly (re-)created database",
45
- name: "import",
46
- type: Boolean,
47
- },
48
- {
49
- alias: "S",
50
- help: "sudo psql commands with given user",
51
- name: "sudo",
52
- type: String,
53
- },
54
- {
55
- alias: "z",
56
- help: "unzip SQL files",
57
- name: "unzip",
58
- type: Boolean,
59
- },
60
- ];
61
- const options = commandLineArgs(optionsDefinitions);
62
- // const pipeline = util.promisify(stream.pipeline)
63
- async function retrieveDataset(dataDir, dataset) {
64
- const zipFilename = dataset.url.substring(dataset.url.lastIndexOf("/") + 1);
65
- const zipFilePath = path.join(dataDir, zipFilename);
66
- if (options.all || options.fetch) {
67
- // Fetch & save ZIP file.
68
- if (!options.silent) {
69
- console.log(`Loading ${dataset.title}: ${zipFilename}…`);
70
- }
71
- // Fetch fails with OpenSSL error: dh key too small.
72
- // (so does "curl").
73
- // const response = await fetch(dataset.url)
74
- // if (!response.ok) {
75
- // console.error(response.status, response.statusText)
76
- // console.error(await response.text())
77
- // throw new Error(`Fetch failed: ${dataset.url}`)
78
- // }
79
- // await pipeline(response.body!, fs.createWriteStream(zipFilePath))
80
- fs.removeSync(zipFilePath);
81
- execSync(`wget --quiet ${dataset.url}`, {
82
- cwd: dataDir,
83
- env: process.env,
84
- encoding: "utf-8",
85
- // stdio: ["ignore", "ignore", "pipe"],
86
- });
87
- }
88
- const sqlFilename = `${dataset.database}.sql`;
89
- const sqlFilePath = path.join(dataDir, sqlFilename);
90
- if (options.all || options.unzip) {
91
- if (!options.silent) {
92
- console.log(`Unzipping ${dataset.title}: ${zipFilename}…`);
93
- }
94
- fs.removeSync(sqlFilePath);
95
- const zip = new StreamZip({
96
- file: zipFilePath,
97
- storeEntries: true,
98
- });
99
- await new Promise((resolve, reject) => {
100
- zip.on("ready", () => {
101
- zip.extract(null, dataDir, (err, _count) => {
102
- zip.close();
103
- if (err) {
104
- reject(err);
105
- }
106
- else {
107
- resolve(null);
108
- }
109
- });
110
- });
111
- });
112
- if (dataset.repairZip !== undefined) {
113
- if (!options.silent) {
114
- console.log(`Repairing Zip path ${dataset.title}: ${sqlFilename}…`);
115
- }
116
- dataset.repairZip(dataset, dataDir);
117
- }
118
- }
119
- if ((options.all || options.repairEncoding) && dataset.repairEncoding) {
120
- if (!options.silent) {
121
- console.log(`Repairing Windows CP1252 encoding of ${dataset.title}: ${sqlFilename}…`);
122
- }
123
- const repairedSqlFilePath = sqlFilePath + ".repaired";
124
- const repairedSqlWriter = fs.createWriteStream(repairedSqlFilePath, {
125
- encoding: "utf8",
126
- });
127
- const lineReader = readline.createInterface({
128
- input: fs.createReadStream(sqlFilePath, { encoding: "utf8" }),
129
- crlfDelay: Infinity,
130
- });
131
- for await (const line of lineReader) {
132
- repairedSqlWriter.write(line.replace(badWindows1252CharacterRegex, (match) => windows1252.decode(match, { mode: "fatal" })) + "\n");
133
- }
134
- repairedSqlWriter.end();
135
- await fs.move(repairedSqlFilePath, sqlFilePath, { overwrite: true });
136
- }
137
- if (options.all || options.import || options.schema) {
138
- if (!options.silent) {
139
- console.log(`Importing ${dataset.title}: ${sqlFilename}…`);
140
- }
141
- execSync(`${options.sudo ? `sudo -u ${options.sudo} ` : ""}psql --quiet -d ${dataset.database} -f ${sqlFilename}`, {
142
- cwd: dataDir,
143
- env: process.env,
144
- encoding: "utf-8",
145
- stdio: ["pipe", "ignore", "ignore"],
146
- });
147
- }
148
- if (options.schema) {
149
- let definitionsDir = path.resolve("src", "raw_types_schemats");
150
- assert(fs.statSync(definitionsDir).isDirectory());
151
- if (!options.silent) {
152
- console.log(`Creating TypeScript definitions from schema of database ${dataset.database}…`);
153
- }
154
- const dbConnectionString = `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@${process.env.PGHOST}:${process.env.PGPORT}/${dataset.database}`;
155
- let definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`);
156
- execSync(`npx schemats generate -c ${dbConnectionString} -s ${dataset.schema} -o ${definitionFilePath}`, {
157
- // cwd: dataDir,
158
- env: process.env,
159
- encoding: "utf-8",
160
- // stdio: ["ignore", "ignore", "pipe"],
161
- });
162
- const definition = fs.readFileSync(definitionFilePath, { encoding: "utf8" });
163
- const definitionRepaired = definition
164
- .replace(/\r\n/g, "\n")
165
- .replace(/AUTO-GENERATED FILE @ \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, "AUTO-GENERATED FILE");
166
- fs.writeFileSync(definitionFilePath, definitionRepaired);
167
- definitionsDir = path.resolve("src", "raw_types");
168
- definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`);
169
- execSync(`kysely-codegen --url ${dbConnectionString} --default-schema=${dataset.schema} --out-file=${definitionFilePath}`, {
170
- // cwd: dataDir,
171
- env: process.env,
172
- encoding: "utf-8",
173
- // stdio: ["ignore", "ignore", "pipe"],
174
- });
175
- }
176
- }
177
- async function retrieveOpenData() {
178
- const dataDir = options.dataDir;
179
- assert(dataDir, "Missing argument: data directory");
180
- process.env = {
181
- ...process.env,
182
- PGHOST: process.env.PGHOST || config.db.host,
183
- PGPORT: process.env.PGPORT || config.db.port,
184
- PGUSER: process.env.PGUSER || config.db.user,
185
- PGPASSWORD: process.env.PGPASSWORD || config.db.password,
186
- };
187
- assert(process.env.PGHOST &&
188
- process.env.PGPORT &&
189
- process.env.PGUSER &&
190
- process.env.PGPASSWORD, "Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file");
191
- console.time("data extraction time");
192
- for (const [, dataset] of Object.entries(datasets)) {
193
- execSync(`${options.sudo ? `sudo -u ${options.sudo} ` : ""}psql --quiet -c "DROP DATABASE IF EXISTS ${dataset.database}"`, {
194
- cwd: dataDir,
195
- env: process.env,
196
- encoding: "utf-8",
197
- });
198
- execSync(`${options.sudo ? `sudo -u ${options.sudo} ` : ""}psql --quiet -c "CREATE DATABASE ${dataset.database} WITH OWNER opendata"`, {
199
- cwd: dataDir,
200
- env: process.env,
201
- encoding: "utf-8",
202
- });
203
- }
204
- const choosenDatasets = getChosenFromEnabledDatasets(options.categories);
205
- for (const dataset of choosenDatasets) {
206
- await retrieveDataset(dataDir, dataset);
207
- }
208
- if (!options.silent) {
209
- console.timeEnd("data extraction time");
210
- }
211
- }
212
- retrieveOpenData()
213
- .then(() => process.exit(0))
214
- .catch((error) => {
215
- console.log(error);
216
- process.exit(1);
217
- });
@@ -1,274 +0,0 @@
1
- import assert from "assert"
2
- import { execSync } from "child_process"
3
- import commandLineArgs from "command-line-args"
4
- import fs from "fs-extra"
5
- // import fetch from "node-fetch"
6
- import path from "path"
7
- // import stream from "stream"
8
- import StreamZip from "node-stream-zip"
9
- import readline from "readline"
10
- // import util from "util"
11
- import windows1252 from "windows-1252"
12
-
13
- import config from "../config"
14
- import { Dataset, datasets, getChosenFromEnabledDatasets } from "../datasets"
15
- import { commonOptions } from "./shared/cli_helpers"
16
-
17
- const badWindows1252CharacterRegex = /[\u0080-\u009f]/g
18
- const optionsDefinitions = [
19
- ...commonOptions,
20
- {
21
- alias: "a",
22
- help: "all options: fetch, unzip, repair-encoding, import",
23
- name: "all",
24
- type: Boolean,
25
- },
26
- {
27
- alias: "c",
28
- help: "create TypeScript interfaces from databases schemas into src/raw_types_* directories",
29
- name: "schema",
30
- type: Boolean,
31
- },
32
- {
33
- alias: "e",
34
- help: "repair Windows CP 1252 encoding of SQL dumps",
35
- name: "repairEncoding",
36
- type: Boolean,
37
- },
38
- {
39
- alias: "f",
40
- help: "fetch datasets instead of retrieving them from files",
41
- name: "fetch",
42
- type: Boolean,
43
- },
44
- {
45
- alias: "i",
46
- help: "import SQL dumps into a freshly (re-)created database",
47
- name: "import",
48
- type: Boolean,
49
- },
50
- {
51
- alias: "S",
52
- help: "sudo psql commands with given user",
53
- name: "sudo",
54
- type: String,
55
- },
56
- {
57
- alias: "z",
58
- help: "unzip SQL files",
59
- name: "unzip",
60
- type: Boolean,
61
- },
62
- ]
63
- const options = commandLineArgs(optionsDefinitions)
64
-
65
- // const pipeline = util.promisify(stream.pipeline)
66
-
67
- async function retrieveDataset(
68
- dataDir: string,
69
- dataset: Dataset,
70
- ): Promise<void> {
71
- const zipFilename = dataset.url.substring(dataset.url.lastIndexOf("/") + 1)
72
- const zipFilePath = path.join(dataDir, zipFilename)
73
- if (options.all || options.fetch) {
74
- // Fetch & save ZIP file.
75
- if (!options.silent) {
76
- console.log(`Loading ${dataset.title}: ${zipFilename}…`)
77
- }
78
- // Fetch fails with OpenSSL error: dh key too small.
79
- // (so does "curl").
80
- // const response = await fetch(dataset.url)
81
- // if (!response.ok) {
82
- // console.error(response.status, response.statusText)
83
- // console.error(await response.text())
84
- // throw new Error(`Fetch failed: ${dataset.url}`)
85
- // }
86
- // await pipeline(response.body!, fs.createWriteStream(zipFilePath))
87
- fs.removeSync(zipFilePath)
88
- execSync(`wget --quiet ${dataset.url}`, {
89
- cwd: dataDir,
90
- env: process.env,
91
- encoding: "utf-8",
92
- // stdio: ["ignore", "ignore", "pipe"],
93
- })
94
- }
95
-
96
- const sqlFilename = `${dataset.database}.sql`
97
- const sqlFilePath = path.join(dataDir, sqlFilename)
98
- if (options.all || options.unzip) {
99
- if (!options.silent) {
100
- console.log(`Unzipping ${dataset.title}: ${zipFilename}…`)
101
- }
102
- fs.removeSync(sqlFilePath)
103
- const zip = new StreamZip({
104
- file: zipFilePath,
105
- storeEntries: true,
106
- })
107
- await new Promise((resolve, reject) => {
108
- zip.on("ready", () => {
109
- zip.extract(null, dataDir, (err?: any, _count?: number) => {
110
- zip.close()
111
- if (err) {
112
- reject(err)
113
- } else {
114
- resolve(null)
115
- }
116
- })
117
- })
118
- })
119
- if (dataset.repairZip !== undefined) {
120
- if (!options.silent) {
121
- console.log(`Repairing Zip path ${dataset.title}: ${sqlFilename}…`)
122
- }
123
- dataset.repairZip(dataset, dataDir)
124
- }
125
- }
126
-
127
- if ((options.all || options.repairEncoding) && dataset.repairEncoding) {
128
- if (!options.silent) {
129
- console.log(
130
- `Repairing Windows CP1252 encoding of ${dataset.title}: ${sqlFilename}…`,
131
- )
132
- }
133
- const repairedSqlFilePath = sqlFilePath + ".repaired"
134
- const repairedSqlWriter = fs.createWriteStream(repairedSqlFilePath, {
135
- encoding: "utf8",
136
- })
137
- const lineReader = readline.createInterface({
138
- input: fs.createReadStream(sqlFilePath, { encoding: "utf8" }),
139
- crlfDelay: Infinity,
140
- })
141
- for await (const line of lineReader) {
142
- repairedSqlWriter.write(
143
- line.replace(badWindows1252CharacterRegex, (match) =>
144
- windows1252.decode(match, { mode: "fatal" }),
145
- ) + "\n",
146
- )
147
- }
148
- repairedSqlWriter.end()
149
- await fs.move(repairedSqlFilePath, sqlFilePath, { overwrite: true })
150
- }
151
-
152
- if (options.all || options.import || options.schema) {
153
- if (!options.silent) {
154
- console.log(`Importing ${dataset.title}: ${sqlFilename}…`)
155
- }
156
- execSync(
157
- `${options.sudo ? `sudo -u ${options.sudo} ` : ""}psql --quiet -d ${
158
- dataset.database
159
- } -f ${sqlFilename}`,
160
- {
161
- cwd: dataDir,
162
- env: process.env,
163
- encoding: "utf-8",
164
- stdio: ["pipe", "ignore", "ignore"],
165
- },
166
- )
167
- }
168
-
169
- if (options.schema) {
170
- let definitionsDir = path.resolve("src", "raw_types_schemats")
171
- assert(fs.statSync(definitionsDir).isDirectory())
172
- if (!options.silent) {
173
- console.log(
174
- `Creating TypeScript definitions from schema of database ${dataset.database}…`,
175
- )
176
- }
177
- const dbConnectionString = `postgres://${process.env.PGUSER}:${process.env.PGPASSWORD}@${process.env.PGHOST}:${process.env.PGPORT}/${dataset.database}`
178
- let definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`)
179
- execSync(
180
- `npx schemats generate -c ${dbConnectionString} -s ${dataset.schema} -o ${definitionFilePath}`,
181
- {
182
- // cwd: dataDir,
183
- env: process.env,
184
- encoding: "utf-8",
185
- // stdio: ["ignore", "ignore", "pipe"],
186
- },
187
- )
188
- const definition = fs.readFileSync(definitionFilePath, { encoding: "utf8" })
189
- const definitionRepaired = definition
190
- .replace(/\r\n/g, "\n")
191
- .replace(
192
- /AUTO-GENERATED FILE @ \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/,
193
- "AUTO-GENERATED FILE",
194
- )
195
- fs.writeFileSync(definitionFilePath, definitionRepaired)
196
-
197
- definitionsDir = path.resolve("src", "raw_types")
198
- definitionFilePath = path.join(definitionsDir, `${dataset.database}.ts`)
199
- execSync(
200
- `kysely-codegen --url ${dbConnectionString} --default-schema=${dataset.schema} --out-file=${definitionFilePath}`,
201
- {
202
- // cwd: dataDir,
203
- env: process.env,
204
- encoding: "utf-8",
205
- // stdio: ["ignore", "ignore", "pipe"],
206
- },
207
- )
208
- }
209
- }
210
-
211
- async function retrieveOpenData(): Promise<void> {
212
- const dataDir = options.dataDir
213
- assert(dataDir, "Missing argument: data directory")
214
-
215
- process.env = {
216
- ...process.env,
217
- PGHOST: process.env.PGHOST || config.db.host,
218
- PGPORT: process.env.PGPORT || config.db.port,
219
- PGUSER: process.env.PGUSER || config.db.user,
220
- PGPASSWORD: process.env.PGPASSWORD || config.db.password,
221
- }
222
- assert(
223
- process.env.PGHOST &&
224
- process.env.PGPORT &&
225
- process.env.PGUSER &&
226
- process.env.PGPASSWORD,
227
- "Missing database configuration: environment variables PGHOST, PGPORT, PGUSER and PGPASSWORD or TRICOTEUSES_SENAT_DB_* in .env file",
228
- )
229
-
230
- console.time("data extraction time")
231
-
232
- for (const [, dataset] of Object.entries(datasets)) {
233
- execSync(
234
- `${
235
- options.sudo ? `sudo -u ${options.sudo} ` : ""
236
- }psql --quiet -c "DROP DATABASE IF EXISTS ${dataset.database}"`,
237
- {
238
- cwd: dataDir,
239
- env: process.env,
240
- encoding: "utf-8",
241
- },
242
- )
243
- execSync(
244
- `${
245
- options.sudo ? `sudo -u ${options.sudo} ` : ""
246
- }psql --quiet -c "CREATE DATABASE ${
247
- dataset.database
248
- } WITH OWNER opendata"`,
249
- {
250
- cwd: dataDir,
251
- env: process.env,
252
- encoding: "utf-8",
253
- },
254
- )
255
- }
256
-
257
- const choosenDatasets: Dataset[] = getChosenFromEnabledDatasets(
258
- options.categories,
259
- )
260
- for (const dataset of choosenDatasets) {
261
- await retrieveDataset(dataDir, dataset)
262
- }
263
-
264
- if (!options.silent) {
265
- console.timeEnd("data extraction time")
266
- }
267
- }
268
-
269
- retrieveOpenData()
270
- .then(() => process.exit(0))
271
- .catch((error) => {
272
- console.log(error)
273
- process.exit(1)
274
- })
@@ -1,147 +0,0 @@
1
- import assert from "assert";
2
- import { execSync } from "child_process";
3
- import commandLineArgs from "command-line-args";
4
- import fs from "fs-extra";
5
- // import fetch from "node-fetch"
6
- import path from "path";
7
- // import stream from "stream"
8
- // import util from "util"
9
- import { findActif as findActifSenateurs } from "../model/sens";
10
- import { slugify } from "../strings";
11
- import { commonOptions } from "./shared/cli_helpers";
12
- const optionsDefinitions = [
13
- ...commonOptions,
14
- {
15
- alias: "f",
16
- help: "fetch sénateurs' pictures instead of retrieving them from files",
17
- name: "fetch",
18
- type: Boolean,
19
- },
20
- ];
21
- const options = commandLineArgs(optionsDefinitions);
22
- // const pipeline = util.promisify(stream.pipeline)
23
- async function retrievePhotosSenateurs() {
24
- const dataDir = options.dataDir;
25
- assert(dataDir, "Missing argument: data directory");
26
- const photosDir = path.join(dataDir, "photos_senateurs");
27
- const missingPhotoFilePath = path.resolve(__dirname, "images", "transparent_155x225.jpg");
28
- const sens = await Array.fromAsync(findActifSenateurs());
29
- // Download photos.
30
- fs.ensureDirSync(photosDir);
31
- if (options.fetch) {
32
- for (const sen of sens) {
33
- const photoStem = `${slugify(sen.sennomuse, "_")}_${slugify(sen.senprenomuse, "_")}${slugify(sen.senmat, "_")}`;
34
- const photoFilename = photoStem + ".jpg";
35
- const photoFilePath = path.join(photosDir, photoFilename);
36
- const photoTempFilename = photoStem + "_temp.jpg";
37
- const photoTempFilePath = path.join(photosDir, photoTempFilename);
38
- const urlPhoto = `https://www.senat.fr/senimg/${photoFilename}`;
39
- if (!options.silent) {
40
- console.log(`Loading photo ${urlPhoto} for ${sen.senprenomuse} ${sen.sennomuse}…`);
41
- }
42
- // Fetch fails with OpenSSL error: dh key too small.
43
- // (so does "curl").
44
- // for (let retries = 0; retries < 3; retries++) {
45
- // const response = await fetch(urlPhoto)
46
- // if (response.ok) {
47
- // await pipeline(response.body, fs.createWriteStream(photoTempFilePath))
48
- // fs.renameSync(photoTempFilePath, photoFilePath)
49
- // break
50
- // }
51
- // if (retries >= 2) {
52
- // console.warn(`Fetch failed: ${urlPhoto} (${sen.senprenomuse} ${sen.sennomuse})`)
53
- // console.warn(response.status, response.statusText)
54
- // console.warn(await response.text())
55
- // if (fs.existsSync(photoFilePath)) {
56
- // console.warn(" => Reusing existing image")
57
- // } else {
58
- // console.warn(" => Using blank image")
59
- // fs.copyFileSync(missingPhotoFilePath, photoFilePath)
60
- // }
61
- // break
62
- // }
63
- // }
64
- try {
65
- execSync(`wget --quiet -O ${photoTempFilename} ${urlPhoto}`, {
66
- cwd: photosDir,
67
- env: process.env,
68
- encoding: "utf-8",
69
- // stdio: ["ignore", "ignore", "pipe"],
70
- });
71
- fs.renameSync(photoTempFilePath, photoFilePath);
72
- }
73
- catch (error) {
74
- if (typeof error === "object" && error && "status" in error && error.status === 8) {
75
- console.error(`Unable to load photo for ${sen.senprenomuse} ${sen.sennomuse}`);
76
- continue;
77
- }
78
- throw error;
79
- }
80
- }
81
- }
82
- // Resize photos to 155x225, because some haven't exactly this size.
83
- for (const sen of sens) {
84
- const photoStem = `${slugify(sen.sennomuse, "_")}_${slugify(sen.senprenomuse, "_")}${slugify(sen.senmat, "_")}`;
85
- const photoFilename = photoStem + ".jpg";
86
- const photoFilePath = path.join(photosDir, photoFilename);
87
- if (fs.existsSync(photoFilePath)) {
88
- if (!options.silent) {
89
- console.log(`Resizing photo ${photoStem} for ${sen.senprenomuse} ${sen.sennomuse}…`);
90
- }
91
- execSync(`gm convert -resize 155x225! ${photoStem}.jpg ${photoStem}_155x225.jpg`, {
92
- cwd: photosDir,
93
- });
94
- }
95
- else {
96
- if (!options.silent) {
97
- console.warn(`Missing photo for ${sen.senprenomuse} ${sen.sennomuse}: using blank image`);
98
- }
99
- fs.copyFileSync(missingPhotoFilePath, path.join(photosDir, `${photoStem}_155x225.jpg`));
100
- }
101
- }
102
- // Create a mosaic of photos.
103
- if (!options.silent) {
104
- console.log("Creating mosaic of photos…");
105
- }
106
- const photoBySenmat = {};
107
- const rowsFilenames = [];
108
- for (let senIndex = 0, rowIndex = 0; senIndex < sens.length; senIndex += 25, rowIndex++) {
109
- const row = sens.slice(senIndex, senIndex + 25);
110
- const photosFilenames = [];
111
- for (const [columnIndex, sen] of row.entries()) {
112
- const photoStem = `${slugify(sen.sennomuse, "_")}_${slugify(sen.senprenomuse, "_")}${slugify(sen.senmat, "_")}`;
113
- const photoFilename = `${photoStem}_155x225.jpg`;
114
- photosFilenames.push(photoFilename);
115
- photoBySenmat[sen.senmat] = {
116
- chemin: `photos_senateurs/${photoFilename}`,
117
- cheminMosaique: "photos_senateurs/senateurs.jpg",
118
- hauteur: 225,
119
- largeur: 155,
120
- xMosaique: columnIndex * 155,
121
- yMosaique: rowIndex * 225,
122
- };
123
- }
124
- const rowFilename = `row-${rowIndex}.jpg`;
125
- execSync(`gm convert ${photosFilenames.join(" ")} +append ${rowFilename}`, {
126
- cwd: photosDir,
127
- });
128
- rowsFilenames.push(rowFilename);
129
- }
130
- execSync(`gm convert ${rowsFilenames.join(" ")} -append senateurs.jpg`, {
131
- cwd: photosDir,
132
- });
133
- for (const rowFilename of rowsFilenames) {
134
- fs.unlinkSync(path.join(photosDir, rowFilename));
135
- }
136
- if (!options.silent) {
137
- console.log("Creating JSON file containing informations on all pictures…");
138
- }
139
- const jsonFilePath = path.join(photosDir, "senateurs.json");
140
- fs.writeFileSync(jsonFilePath, JSON.stringify(photoBySenmat, null, 2));
141
- }
142
- retrievePhotosSenateurs()
143
- .then(() => process.exit(0))
144
- .catch((error) => {
145
- console.log(error);
146
- process.exit(1);
147
- });