-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
171 lines (148 loc) · 3.81 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { gql, GraphQLClient } from "graphql-request";
import { json2csv } from "json-2-csv";
const outputFilename = "articles_from_omnivore.csv";
function getArgs(argv) {
const result = {};
argv.splice(0, 2);
argv.forEach((arg, index) => {
if (arg.substring(0, 2) === "--") {
const key = arg.substring(2, arg.length);
const value =
argv[index + 1].substring(0, 2) === "--" ? true : argv[index + 1];
result[key] = value === "false" ? false : value;
}
});
return result;
}
const {
"search-query": searchQuery = "",
"full-data": fullData = true,
"add-tag": addTag = "",
"save-intermediate-files": saveIntermediateFiles = false,
} = getArgs(Bun.argv);
console.log("Using the following options: ", {
searchQuery,
fullData,
addTag,
saveIntermediateFiles,
});
const key = Bun.env.API_KEY;
const client = new GraphQLClient("https://api-prod.omnivore.app/api/graphql", {
headers: {
authorization: key,
},
});
const getArticles = gql`
query Search($after: String, $first: Int, $query: String) {
search(after: $after, first: $first, query: $query) {
... on SearchSuccess {
edges {
node {
createdAt
title
url
description
labels {
name
}
slug
id
pageType
isArchived
author
quote
annotation
color
subscription
highlights {
quote
}
wordsCount
folder
}
}
pageInfo {
hasNextPage
endCursor
totalCount
}
}
... on SearchError {
errorCodes
}
}
}
`;
async function exportArticles() {
let currentCount = 0;
let hasNextPage = true;
const results = [];
console.log("Getting articles from the API!");
while (hasNextPage) {
const response = await client.request(getArticles, {
query: searchQuery,
after: currentCount.toString(),
});
const items = response.search.edges.map((e) => {
const { node } = e;
return {
...node,
title: node.title.replace(/\n|\r/g, ""),
description: node.description
? node.description.replace(/\n|\r/g, "")
: "",
labels: [...node.labels.map((l) => l.name), addTag].join(", "),
highlights: node.highlights.map((h) => h.quote),
};
});
results.push(...items);
currentCount = response.search.pageInfo.endCursor;
hasNextPage = response.search.pageInfo.hasNextPage;
console.log(
`Received...${currentCount} out of ${response.search.pageInfo.totalCount} articles`,
);
}
if (saveIntermediateFiles) {
console.log("Saving exported articles in JSON...");
Bun.write("exported_articles.json", JSON.stringify(results));
}
return results;
}
function preProcessArticles(articles) {
const result = articles.map((a) => {
const processed = {
url: a.url,
title: a.title,
created: a.createdAt,
tags: a.labels,
note: a.description,
};
return processed;
});
if (saveIntermediateFiles) {
console.log("Saving partial data in JSON...");
Bun.write("processed_articles.json", JSON.stringify(result));
}
return result;
}
function convertToCSV(articles) {
const csv = json2csv(articles, {
delimiter: {
field: ",",
wrap: '"',
eol: "\n",
},
});
console.log("Converting to CSV...");
Bun.write(outputFilename, csv);
return csv;
}
const exportedArticles = await exportArticles();
if (fullData) {
convertToCSV(exportedArticles);
} else {
const processedArticles = preProcessArticles(exportedArticles);
convertToCSV(processedArticles);
}
console.log("Done!");
console.log("Output file: ", outputFilename);