generated from MHG-LAB/submit-urls-from-sitemap-to-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (46 loc) · 1.07 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
const fs = require('fs');
var request = require('request');
var { google } = require('googleapis');
var key = require('./service_account.json');
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
['https://www.googleapis.com/auth/indexing'],
null
);
const batch = fs
.readFileSync('urls.txt')
.toString()
.split('\n');
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) => {
console.log(body);
});
});