import axios from "axios";
import { execSync } from "node:child_process";
import puppeteer from "puppeteer";
let installed = false;
// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }: any) => {
// Why not try the Appwrite SDK?
//
// const client = new Client()
// .setEndpoint('https://cloud.appwrite.io/v1')
// .setProject(Bun.env["APPWRITE_FUNCTION_PROJECT_ID"])
// .setKey(Bun.env["APPWRITE_API_KEY"]);
// Init!
log("Function Invocation Started");
// You can log messages to the console
log("Hello, Logs!");
// If something goes wrong, log an error
error("Hello, Errors!");
// The `req` object contains the request data
if (req.method === "POST") {
try {
// Assuming the body is already parsed as JSON
const { url } = JSON.parse(req.body);
// Validate the URL
if (!url || typeof url !== "string") {
return res.json(
{ error: "Invalid or missing url ", data: url },
400
);
}
log(`Fetching HTML content for: ${url}`);
// Use Axios to fetch the HTML content
const response = await axios.get(url, {
// Axios config: Receive response as a string
responseType: "text",
});
if (response.data) {
// Return the HTML content
return res.send(response.data);
} else {
if (!installed) {
try {
log("Installing Chromium...");
execSync(
"apk update && apk add chromium nss freetype harfbuzz ca-certificates ttf-freefont",
{ stdio: "inherit" }
);
log("Chromium installed successfully...");
installed = true;
} catch (installError) {
log(`Error installing```