-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress.js
197 lines (166 loc) · 5.54 KB
/
wordpress.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
const axios = require('axios');
const Airtable = require('airtable');
const fs = require('fs');
const path = require('path');
const download = require('image-downloader');
const os = require('os');
const URL = require('url').URL;
const yargs = require('yargs/yargs');
require('dotenv').config();
hideBin = argv => argv.slice(2);
const argv = yargs(hideBin(process.argv)).argv;
// From args:
const wtWebsiteHost = argv.host || 'http://localhost:8888/wikitongues';
const table = argv.table || 'CRM';
const view = argv.view || 'Website Export';
const maxRecords = argv.maxRecords || 1;
const allRecords = argv.all;
const postType = argv.postType || 'team';
const imageFieldAirtable = argv.airtableField || 'profile_picture';
const imageFieldWP = argv.wpField || 'profile_pic_url';
const wpUser = argv.wpUser || process.env.WP_USER || 'wt-admin';
const wpPassword = argv.wpPassword || process.env.WP_PASSWORD || console.error(
'Wordpress Admin password required. Please provide --wp-password or set WP_PASSWORD'
);
const apiKey = argv.apiKey || process.env.APIKEY || console.error(
'Airtable API key required. Please provide --api-key or set APIKEY'
);
const baseId = argv.baseId || process.env.BASE || console.error(
'Airtable Base ID required. Please provide --base-id or set BASE'
);
const dryRun = argv.dryRun;
if (!apiKey || !baseId || !wpPassword) {
process.exit(1);
}
if (dryRun) {
console.info('Dry run: will not download images from Airtable');
}
if (!allRecords) {
console.info(`Processing ${maxRecords} posts(s). To process all posts, add the --all flag.`);
}
// Constants:
const POST_TITLE_FIELD = 'post_title';
let count = 0;
//-----------------------------------------------------------------------------------------
async function handleAirtableRow(record) {
const title = record.get(POST_TITLE_FIELD);
const postTitle = title.replace(/\s+/g, '-');
const imgData = record.get(imageFieldAirtable);
let imageurl;
if (imgData !== undefined) {
imageurl = imgData.substring(imgData.indexOf('(h') + 1, imgData.length - 1);
} else {
return;
}
console.log(`Processing ${postTitle}...`);
try {
new URL(imageurl);
} catch (e) {
console.warn(`Invalid image url ${imageurl}`);
return;
}
try {
const targetPostID = await lookUpTargetPost(postTitle);
if (targetPostID === undefined) {
console.warn(`No post exists for ${postTitle}`);
return;
}
if (dryRun) {
return;
}
const filePath = await downloadImage(imageurl, os.tmpdir());
const imagePostId = await uploadImage(filePath);
await setImageForPost(imagePostId, targetPostID);
count++;
} catch (e) {
console.error(`Image not uploaded for ${postTitle} - an error occured.`);
console.error(e);
return;
}
}
async function handlePage(records) {
try {
for (const record of records) {
await handleAirtableRow(record);
}
} catch (e) {
console.error('error inside eachPage => ', e);
process.exit(1);
}
}
//-----------------------------------------------------------------------------------------
async function downloadImage(url, dest) {
const options = { url, dest };
return (await download.image(options)).filename;
}
//-----------------------------------------------------------------------------------------
async function uploadImage(filePath) {
var config = {
method: 'post',
url: `${wtWebsiteHost}/wp-json/wp/v2/media`,
headers: {
'Content-Type': 'application/json',
"Content-Disposition": 'form-data; filename="image.jpeg"',
"Content-Type": "image/jpeg",
},
auth: {
username: wpUser,
password: wpPassword
},
data: fs.readFileSync(filePath)
};
const res = await axios(config);
const imagePostId = JSON.stringify(res.data.id);
return imagePostId;
}
//-----------------------------------------------------------------------------------------
async function lookUpTargetPost(postTitle) {
const config = {
url: `${wtWebsiteHost}/wp-json/wp/v2/${postType}?slug=${postTitle}`,
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
auth: {
username: wpUser,
password: wpPassword
}
};
let res = await axios(config)
if (!res.data || res.data.length === 0) {
return undefined;
}
const targetPostID = JSON.stringify(res.data[0].id);
return targetPostID;
}
//-----------------------------------------------------------------------------------------
async function setImageForPost(imagePostId, targetPostId) {
const data = {
'acf': {
[imageFieldWP]: imagePostId
}
}
var config = {
method: 'post',
url: `${wtWebsiteHost}/wp-json/wp/v2/${postType}/${targetPostId}`,
headers: {
'Content-Type': 'application/json',
},
auth: {
username: wpUser,
password: wpPassword
},
data
};
await axios(config)
}
const base = new Airtable({ apiKey }).base(baseId);
const selectParams = allRecords ? { view } : { view, maxRecords };
base(table).select(selectParams).eachPage(function page(records, fetchNextPage) {
handlePage(records).then(fetchNextPage);
}).then(error => {
if (error) {
console.error(error);
process.exit(1);
}
console.log(`Done! Processed ${count} posts.`);
});