-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsocrata.ts
More file actions
76 lines (67 loc) · 2.06 KB
/
Copy pathsocrata.ts
File metadata and controls
76 lines (67 loc) · 2.06 KB
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
import { env } from "../../config";
import { BaseHarvester, BaseHarvesterConfig } from "./base";
import { CkanResource, PortalJsCloudDataset } from "@/schemas/portaljs-cloud";
import { Harvester } from ".";
import { listAllDatasets } from "../lib/socrata";
import { type SocrataDataset } from "@/schemas/socrata";
@Harvester
class SocrataHarvester extends BaseHarvester<SocrataDataset> {
constructor(args: BaseHarvesterConfig) {
super(args);
}
async getSourceDatasets() {
return await listAllDatasets({
socrataUrl: this.config.source.url,
socrataAppToken: this.config.source.apiKey,
});
}
mapSourceDatasetToTarget(ds: SocrataDataset): PortalJsCloudDataset {
const owner_org = env.PORTALJS_CLOUD_MAIN_ORG;
const resources: CkanResource[] = [];
// Attachments
for (let att of ds.metadata.attachments ?? []) {
let url = "";
if (att.assetId) {
url = `${this.config.source.url}/api/views/${ds.id}/files/${att.assetId}?filename=${att.filename}`;
} else {
url = `${this.config.source.url}/api/assets/${att.blobId}?download=true`;
}
resources.push({
name: att.name ?? att.filename,
url,
});
}
// Tabular resource
if (ds.viewType === "tabular") {
resources.push({
name: ds.name,
url: `${this.config.source.url}/api/views/${ds.id}/rows.csv`,
});
}
// Extras
const extras = [];
extras.push({
key: "Source URL",
value: `${this.config.source.url}/d/${ds.id}`,
});
extras.push({
key: "Last Harvested At",
value: new Date().toISOString(),
});
if (ds.category) {
extras.push({ key: "Category", value: ds.category });
}
return {
owner_org,
name: `${owner_org}--${ds.id}`,
title: ds.name,
notes: ds.description || "no description",
author: ds.owner?.displayName,
language: "EN", // Socrata datasets don’t usually have a language field
resources,
tags: ds.tags?.map((t) => ({ name: t })),
extras,
};
}
}
export { SocrataHarvester };