forked from omnivore-app/readwise-reader-csv-to-omnivore-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
100 lines (93 loc) · 2.76 KB
/
index.ts
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
import * as fs from "fs";
import * as csvParser from "csv-parser";
interface InputRow {
Title: string;
URL: string;
Document_tags: string;
Saved_date: string;
Reading_progress: number;
Location: string;
Seen: boolean;
}
interface OutputRow {
url: string;
state: string;
labels: string;
saved_at: string;
published_at: string;
}
function escapeLabels(labelString: string): string {
if (!labelString || labelString.length < 1) {
return "";
}
// Remove single quotes from the input string
const cleanedString = labelString.replace(/'/g, "");
// Split the cleaned string into individual label names
const labels = cleanedString
.slice(1, -1)
.split(",")
.map((label) => label.trim());
// Escape and format labels
return "[" + labels.map((label) => `""${label}""`).join(", ") + "]";
}
async function convertCSV(inputRows: InputRow[]): Promise<OutputRow[]> {
const outputRows: OutputRow[] = inputRows.map((row) => {
const state =
row.Location.toLowerCase() === "archive" ? "ARCHIVED" : "SUCCEEDED";
const labels = escapeLabels(row.Document_tags);
const saved_at = row.Saved_date
? new Date(row.Saved_date).getTime().toString()
: "";
const published_at = ""; // Assuming we don't have this information in the input
return {
url: row.URL,
state: state,
labels: labels,
saved_at: saved_at,
published_at: published_at,
};
});
return outputRows;
}
async function processCSV(filePath: string): Promise<OutputRow[]> {
return new Promise<OutputRow[]>((resolve, reject) => {
const rows: InputRow[] = [];
fs.createReadStream(filePath)
.pipe(csvParser())
.on("data", (row: any) => {
rows.push({
Title: row.Title,
URL: row.URL,
Document_tags: row["Document tags"],
Saved_date: row["Saved date"],
Reading_progress: parseFloat(row["Reading progress"]),
Location: row.Location,
Seen: row.Seen === "True",
});
})
.on("end", () => {
resolve(convertCSV(rows));
})
.on("error", (err) => {
reject(err);
});
});
}
function buildOmnivoreCSV(outputRows: OutputRow[]) {
fs.writeFile("omnivore_file.csv", outputRows.map(row => `"${row.url}","${row.state}","${row.labels}","${row.saved_at}","${row.published_at}"` ).join("\n"), (err) => {
if (err) {
console.error('Error writing Omnivore CSV file', err);
} else {
console.log('Omnivore CSV file has been created.');
}
});
}
// Main
if (process.argv.length > 1) {
const filePath = process.argv[2];
processCSV(filePath)
.then((outputRows) => buildOmnivoreCSV(outputRows))
.catch((err) => console.error(err));
} else {
console.error("Please provide a CSV file path.");
}