forked from marekfoltanski/seo-zabijaka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress.js
More file actions
49 lines (46 loc) · 1.35 KB
/
wordpress.js
File metadata and controls
49 lines (46 loc) · 1.35 KB
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
import fetch from "node-fetch";
import fs from "fs";
import { keys } from "./keys.js";
import { fileName } from "./functions.js";
async function uploadImage(path, title) {
const name = fileName(title);
const createdImage = await fetch(`${keys.WP_ENDPOINT}media`, {
method: "POST",
headers: {
"Content-Disposition": `attachment; filename="${name}.jpg"`,
"Content-Type": "image/jpeg",
Authorization: `Basic ${Buffer.from(
`${keys.WP_USER}:${keys.WP_PASS}`,
"utf-8"
).toString("base64")}`,
},
body: fs.readFileSync(path, (err, data) => {
if (err) {
console.log(err);
}
}),
}).then((response) => response.json());
return createdImage.id;
}
async function createPost(title, content, imageId, categories, tags) {
const createdPost = await fetch(`${keys.WP_ENDPOINT}posts`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${Buffer.from(
`${keys.WP_USER}:${keys.WP_PASS}`,
"utf-8"
).toString("base64")}`,
},
body: JSON.stringify({
title: title,
content: content,
featured_media: imageId ? imageId : null,
status: "publish",
categories: categories,
tags: tags,
}),
}).then((response) => response.json());
return createdPost;
}
export { uploadImage, createPost };