Skip to content

Commit

Permalink
Nodejs dependent functions separated and fetchGeolocation (puppeteer …
Browse files Browse the repository at this point in the history
…-dep) removed.
  • Loading branch information
darsan-in committed Nov 19, 2024
1 parent 0252c2d commit 01f0260
Show file tree
Hide file tree
Showing 3 changed files with 595 additions and 581 deletions.
99 changes: 99 additions & 0 deletions lib/node-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { format } from "@prettier/sync";
import { load } from "cheerio/slim";
import { writeFile } from "node:fs/promises";

/* //it is only good if you have google business page
async function _getGeoCode(address: string): Promise<GeoOptions> {
const url: string = encodeURI(
`https://www.google.com/maps?q=${address}`,
);
//browser instance
const browser = await puppeteer.launch();
//creating page
const page = await browser.newPage();
//navigating
await page.goto(url);
//wait until completed loaded and url updated
await page.waitForNavigation();
const geocodes: string[] = page
.url()
.split("/")[6]
.split(",")
.slice(0, 2);
//close browser instance
await browser.close();
return {
latitude: parseFloat(geocodes?.[0].replace("@", "") ?? "0"),
longitude: parseFloat(geocodes?.[1] ?? "0"),
};
}
//make geocode if previously not generated with map iframe
export async function fetchGeoLocation(
meta: LocalBusinessOptions | RestaurantOptions,
): Promise<LocalBusinessOptions | RestaurantOptions> {
if (!meta.geo) {
console.log(
"Warning: No Map frame was found in HTML\nMaking approximate coordinates..",
);
const completeAddress = [
meta.businessName,
meta.address.streetAddress,
meta.address.addressLocality,
meta.address.addressRegion,
meta.address.postalCode,
meta.address.addressCountry,
].join(",");
const { latitude, longitude } = await _getGeoCode(completeAddress);
meta.geo = { latitude, longitude };
}
return meta;
}
*/
export function createJsonLD(
innerDataObject: Record<string, any>,
): string {
const jsonLD =
innerDataObject ?
`<script type="application/ld+json">${JSON.stringify(
innerDataObject,
)}</script>`
: "";
return jsonLD;
}

export function writeOutput(
source: string,
destinationFile: string,
richResult: string,
pretty: boolean = true,
): Promise<void> {
console.log(source);
/* loading source as dom object */
const $ = load(source);

/* append json ld to source file */
$("head").append(richResult);

/* final output */

const content = pretty ? format($.html(), { parser: "html" }) : $.html();

return new Promise((resolve, reject) => {
writeFile(destinationFile, content)
.then(() => {
resolve();
})
.catch((error) => {
reject(
`Error while writing outputfile: ${destinationFile}\n ${error} `,
);
});
});
}
Loading

0 comments on commit 01f0260

Please sign in to comment.