Skip to content

Instantly share code, notes, and snippets.

@titomus
Created September 11, 2023 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save titomus/007f5e2489df205a0d76f9b436bf2984 to your computer and use it in GitHub Desktop.
Save titomus/007f5e2489df205a0d76f9b436bf2984 to your computer and use it in GitHub Desktop.
Auto Indexing Urls via Google API's
const fs = require('fs');
var request = require('request');
var { google } = require('googleapis');
var key = require('./cle.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);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment