Skip to content

Instantly share code, notes, and snippets.

@klaaz0r
Created December 10, 2023 12:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save klaaz0r/b45d4b57a9358bb35749b8f708046939 to your computer and use it in GitHub Desktop.
Save klaaz0r/b45d4b57a9358bb35749b8f708046939 to your computer and use it in GitHub Desktop.
Google Indexing API for Homestra.com
import getDataSource from '../dataSource';
import { ListingStatus, Property } from '../entities/Property';
import log from '../logger';
import fetch from 'node-fetch';
import { IsNull } from 'typeorm';
//@ts-ignore <- fight me
import { google } from 'googleapis';
// Enable the Indexing API here:
// https://console.cloud.google.com/apis/library/indexing.googleapis.com?project={project_id}
// Make sure to add your "client_email" to the Search Console here:
// https://search.google.com/u/0/search-console/users
// More details: https://developers.google.com/search/apis/indexing-api/v3/prereqs#verify-site
// NOTE: You need to also set the service account to "owner" in the Search Console
// We need an access token to authenticate our requests
async function authorize() {
return new Promise((resolve, reject) => {
// Downloade the JSON Keyfile from Google Cloud Console
const key = require('../../../../key.json');
return new google.auth.JWT(
key.client_email,
undefined,
key.private_key,
['https://www.googleapis.com/auth/indexing'],
undefined
).authorize((err: any, tokens: any /* @dhh was right */) => {
if (err) {
return reject(err);
}
resolve(tokens.access_token);
});
});
}
export default async function googleIndexAPI() {
// Fetch your data
// yes this is TypeORM and yes I will migrate to Prisma
const properties = await (await getDataSource()).getRepository(Property).find({
where: { listingStatus: ListingStatus.published, deletedAt: IsNull() },
order: {
createdAt: 'DESC'
},
take: 200
});
log.info({ size: properties.length }, 'live properties');
const token = await authorize();
for await (const property of properties) {
await fetch('https://indexing.googleapis.com/v3/urlNotifications:publish', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
url: `https://homestra.com/property/${property.slug}`,
type: 'URL_UPDATED'
})
})
.then((response) => response.json())
.then((response) => {
log.info({ response }, 'indexed url');
})
.catch((err) => {
log.error({ err }, 'failed to index url');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment