Created
September 11, 2023 08:16
-
-
Save titomus/007f5e2489df205a0d76f9b436bf2984 to your computer and use it in GitHub Desktop.
Auto Indexing Urls via Google API's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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