-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfunction.js
50 lines (45 loc) · 1.16 KB
/
function.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
var request = require("request");
var { google } = require("googleapis");
var key = require("./service_account.json");
async function indexingApi(urls, callback) {
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
["https://www.googleapis.com/auth/indexing"],
null
);
const batch = urls.split(/\r?\n/g);
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
}
const items = batch.map((line) => {
return {
"Content-Type": "application/http",
"Content-ID": "",
body:
"POST /v3/urlNotifications:publish HTTP/1.1\n" +
"Content-Type: application/json\n\n" +
JSON.stringify({
url: line,
type: "URL_UPDATED",
}),
};
});
const options = {
url: "https://indexing.googleapis.com/batch",
method: "POST",
headers: {
"Content-Type": "multipart/mixed",
},
auth: { bearer: tokens.access_token },
multipart: items,
};
request(options, (err, resp, body) => {
callback(body);
});
});
}
module.exports = { indexingApi };