-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (184 loc) · 4.8 KB
/
index.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const axios = require("axios");
const cheerio = require("cheerio");
const path = require("path");
const fs = require("fs");
const FormData = require("form-data");
const shortid = require("shortid");
const apiBase = "https://docsapi.helpscout.net/v1";
const {
intercomSite: intercomBase,
helpscoutKey: apiKey,
collectionId,
siteId,
} = require("./config");
const api = axios.create({
baseURL: apiBase,
auth: {
username: apiKey,
password: "fakepass",
},
});
async function downloadImage(article, url) {
const filepath = path.resolve(
__dirname,
"images",
`${article}-${shortid.generate()}-${
url.split("/")[url.split("/").length - 1]
}`
);
const writer = fs.createWriteStream(filepath);
const response = await axios({
url,
method: "GET",
responseType: "stream",
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", () => resolve(filepath));
writer.on("error", () => reject());
});
}
const uploadCollection = async (collection, categoryId) => {
try {
// load the collection page
const page = await axios.get(collection);
const $ = cheerio.load(page.data);
// find the articles
const articles = [];
$("a.paper__article-preview").each((index, element) => {
articles.push(element);
});
for (const article of articles) {
const href = article.attribs.href;
console.log(`(loading) ${href}`);
const contentPage = await axios.get(`${intercomBase}${href}`);
const $c = cheerio.load(contentPage.data);
const title = $c(".article .article__meta h1").text().trim();
const description = $c(".article .article__meta .article__desc")
.text()
.trim();
let content = $c(".article article").html();
console.log(`(loaded) ${href} [${title}]`);
// create the article in HelpScout
const created = await api({
url: "/articles",
method: "post",
data: {
collectionId,
status: "published",
slug: href
.replace("/en/articles/", "")
.split("-")
.slice(1)
.join("-"),
name: title,
text: content,
categories: [categoryId],
},
});
const articleId = created.headers.location.replace(
`${apiBase}/articles/`,
""
);
console.log(`(added) ${href} [${title}] [${articleId}]`);
// extract files from the intercom article
const assets = [];
$c(".article article img").each((index, element) => {
if (element.attribs.src.includes("downloads.intercomcdn.com"))
assets.push(element.attribs.src);
});
let i = 0;
for (const asset of assets) {
i++;
console.log(
`(downloading image ${i}/${assets.length}) ${href} [${asset}]`
);
const file = await downloadImage(articleId, asset);
const formData = new FormData();
formData.append("file", fs.createReadStream(file));
formData.append("articleId", articleId);
formData.append("assetType", "image");
formData.append("key", apiKey);
const upload = await api({
url: "/assets/article",
data: formData,
method: "post",
headers: {
...formData.getHeaders(),
},
});
content = content.replace(
new RegExp(asset, "g"),
upload.data.filelink
);
console.log(
`(completed image ${i}/${assets.length}) ${href} [${asset}]`
);
}
await api({
url: `/articles/${articleId}`,
method: "put",
data: {
text: content,
},
});
const articleObj = await api({
url: `/articles/${articleId}`,
method: "get",
});
await api({
url: `/redirects`,
method: "post",
data: {
siteId,
urlMapping: href,
redirect: articleObj.data.article.publicUrl,
},
});
console.log(`(finished) ${href} [${title}] [${articleId}]`);
}
} catch (e) {
console.error(e);
}
};
const run = async () => {
try {
// load the collection page
const page = await axios.get(`${intercomBase}/en/`);
const $ = cheerio.load(page.data);
const collections = [];
$(".g__space a").each((index, element) => {
if (element.attribs.href.includes("/en/collections"))
collections.push(`${intercomBase}/${element.attribs.href}`);
});
for (const index in collections) {
const collectionUrl = collections[index];
console.log(`(starting collection) ${collectionUrl}`);
const category = await api({
url: "/categories",
method: "post",
data: {
collectionId: collectionId,
name: $(
`.g__space a[href="${collectionUrl.replace(
`${intercomBase}/`,
""
)}"] h2`
).text(),
visibility: "public",
order: Number(index) + 1,
defaultSort: "name",
},
});
const categoryId = category.headers.location.replace(
`${apiBase}/categories/`,
""
);
await uploadCollection(collectionUrl, categoryId);
console.log(`(completed collection) ${collectionUrl}`);
}
} catch (e) {
console.error(e, e.data);
}
};
run();