forked from davidohlin/instapaper-to-omnivore-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
141 lines (122 loc) Β· 3.52 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { parse } from "csv-parse";
import { createObjectCsvWriter } from "csv-writer"
import "dotenv/config";
import fs from "fs";
import { gql, GraphQLClient } from "graphql-request";
import { finished } from "stream/promises";
interface ImportItem {
url: string,
title: string,
selection: string,
folder: string,
timestamp: string
}
const OMNIVORE_API_URL = "https://api-prod.omnivore.app/api/graphql";
const INSTAPAPER_CSV_EXPORT_PATH = `${__dirname}/instapaper-export.csv`;
const csvWriter = createObjectCsvWriter({
path: `error_${new Date().toJSON().slice(0, 10)}.csv`,
header: [
{ id: 'url', title: 'URL' },
{ id: 'title', title: 'Title' },
{ id: 'selection', title: 'Selection' },
{ id: 'folder', title: 'Folder' },
{ id: 'timestamp', title: 'Timestamp' },
]
});
// https://github.com/omnivore-app/omnivore/blob/main/packages/api/src/schema.ts
const createArticleMutation = gql`
mutation CreateArticleSavingRequest($input: CreateArticleSavingRequestInput!) {
createArticleSavingRequest(input: $input) {
... on CreateArticleSavingRequestSuccess {
articleSavingRequest {
id
status
}
}
... on CreateArticleSavingRequestError {
errorCodes
}
}
}
`;
const createArchiveMutation = gql`
mutation SetLinkArchived($input: ArchiveLinkInput!) {
setLinkArchived(input: $input) {
... on ArchiveLinkSuccess {
linkId
message
}
... on ArchiveLinkError {
message
errorCodes
}
}
}
`;
async function processCsv() {
const records: ImportItem[] = [];
//const records = [];
const parser = fs.createReadStream(INSTAPAPER_CSV_EXPORT_PATH).pipe(parse());
parser.on("readable", () => {
let record;
while ((record = parser.read()) !== null) {
if (record[0] !== "URL") {
records.push({ url: record[0], folder: record[3] });
}
}
});
await finished(parser);
return records;
}
async function main() {
if (!process.env.OMNIVORE_API_KEY) {
throw new Error(
"No API key found. Did you forget to add it to the .env file?"
);
}
const client = new GraphQLClient(OMNIVORE_API_URL, {
headers: {
authorization: process.env.OMNIVORE_API_KEY,
},
});
const entries = await processCsv();
let addedEntriesCount = 0;
let archivedEntriesCount = 0;
let failedEntriesCount = 0;
const failedEntries: ImportItem[] = [];
console.log(`Adding ${entries.length} links to Omnivore..`);
for (const entry of entries) {
try {
const response = await client.request(createArticleMutation, {
input: { url: entry.url },
});
addedEntriesCount++;
var result = response;
if (entry.folder == "Archive") {
await client.request(createArchiveMutation, {
input: { linkId: result.createArticleSavingRequest.articleSavingRequest.id, archived: true },
});
archivedEntriesCount++;
}
} catch (error) {
console.error(`π« Failed to add ${entry.url}`);
failedEntries.push({
url: entry.url,
title: entry.title,
selection: entry.selection,
folder: entry.folder,
timestamp: entry.timestamp
})
failedEntriesCount++;
}
}
console.log(
`Successfully added ${addedEntriesCount} (Archived: ${archivedEntriesCount}) of ${entries.length} links!`
);
if (failedEntriesCount > 0) {
csvWriter
.writeRecords(failedEntries)
.then(() => console.log('The CSV file was written successfully'));
}
}
main();