Creating a website aggregator with ChatGPT, React, and Node.js šŸš€

A website aggregator is a website that collects data from other websites across the internet and puts the information in one place where visitors can access it.

Author:Nevo David
Nevo David

There are many versions of website aggregators; some are search engines such as Google and Duck Duck go, and some can have more of aĀ Product HuntĀ structure where you can see a picture and a short text.

You will usually scrape the website, take their metatags and h1-6 tags, scan their sitemap.xml, and use some pattern to sort the information.

Today I am going to use a different solution 😈I will take the entire website content, send it to ChatGPT, and ask them to give me the information I need.

It’s kinda crazy to see ChatGPT parses the website content

So lettttsss do it šŸš€

In this article, you’ll learn how to build a website aggregator which scrapes content from a website and determines the website’s title and description using ChatGPT.

What is ChatGPT?

ChatGPTĀ is an AI language model trained byĀ OpenAIĀ to generate text and interact with users in a human-like conversational manner. It is worth mentioning that ChatGPT is free and open to public use.

Users can submit requests and get information or answers to questions from a wide range of topics such as history, science, mathematics, and current events in just a few seconds.

ChatGPT performs other tasks, such as proofreading, paraphrasing, and translation. It can also help with writing, debugging, and explaining code snippets. Its wide range of capabilities is the reason why ChatGPT has been trending.

ChatGPT is not available as an API yet šŸ™ In order to use we will have to scrape our way in 😈

Novu – the first open-source notification infrastructure

Just a quick background about us. Novu is the first open-sourceĀ notification infrastructure. We basically help to manage all the product notifications. It can beĀ In-AppĀ (the bell icon like you have in Facebook – Websockets), Emails, SMSs and so on.

I would be super happy if you could give us a star! And let me also know in the comments ā¤ļø https://github.com/novuhq/novu

Limitation with ChatGPT

As previously mentioned, ChatGPT is not accessible through a public API. Instead, we can use web scraping techniques to access it. This involves automating the process of logging in to the OpenAI website, solving the captcha (you can use 2captcha for this), and sending an API request with the OpenAI cookies. Fortunately, there is a public library that can handle these tasks for us. Keep in mind that this is not a formal API, so you may encounter limitations if you attempt to make a large number of requests. Additionally, it is not suitable for real-time requests. If you want to use it, consider implementing a queue system for background processing.

Project Set up

Here, I’ll guide you through creating the project environment for the web application. We’ll use React.js for the front end and Node.js for the backend server.

Create the project folder for the web application by running the code below:

mkdir website-aggregator
cd website-aggregator
mkdir client server

Setting up the Node.js server

Navigate into the server folder and create aĀ package.jsonĀ file.

cd server & npm init -y

Install Express, Nodemon, and the CORS library.

npm install express cors nodemon

ExpressJSĀ is a fast, minimalist framework that provides several features for building web applications in Node.js,Ā CORSĀ is a Node.js package that allows communication between different domains, andĀ NodemonĀ is a Node.js tool that automatically restarts the server after detecting file changes.

Create anĀ index.jsĀ file – the entry point to the web server.

touch index.js

Set up a Node.js server using ExpressJS. The code snippet below returns a JSON object when you visit theĀ http://localhost:4000/apiĀ in your browser.

//\ud83d\udc47\ud83c\udffbindex.js
const express = require("express");
const cors = require("cors");
const app = express();
const PORT = 4000;

app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());

app.get("/api", (req, res) => {
    res.json({
        message: "Hello world",
    });
});

app.listen(PORT, () => {
    console.log(`Server listening on ${PORT}`);
});

Install the unofficialĀ ChatGPT API libraryĀ andĀ Puppeteer. The ChatGPT API uses Puppeteer as an optional peer dependency to automate bypassing the Cloudflare protections.

npm install chatgpt puppeteer

To use the ChatGPT API within theĀ server/index.jsĀ file, you need to configure the file to use both theĀ requireĀ andĀ importĀ keywords for importing libraries.

Therefore, update theĀ server/package.jsonĀ file to contain the type keyword.

{ "type": "module" }

Add the code snippet below at the top of theĀ server/index.jsĀ file.

import { createRequire } from "module";
const require = createRequire(import.meta.url);
//...other code statements

Once you have completed the last two steps, you can now use ChatGPT within theĀ index.jsĀ file.

Configure Nodemon by adding the start command to the list of scripts in theĀ package.jsonĀ file. The code snippet below starts the server using Nodemon.

//In server/package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  },

Congratulations! You can now start the server by using the command below.

npm start

Setting up the React application

Navigate into the client folder via your terminal and create a new React.js project.

cd client
npx create-react-app ./

Delete the redundant files, such as the logo and the test files from the React app, and update theĀ App.jsĀ file to display ā€œHello Worldā€ as below.

function App() {
    return (
        
            Hello World!</p>
        </div>
    );
}
export default App;

Navigate into theĀ src/index.cssĀ file and copy the code below. It contains all the CSS required for styling this project.

@import url("https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@300;400;500;600;700&display=swap");
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: "Space Grotesk", sans-serif;
}
input {
    padding: 10px;
    width: 70%;
    margin: 10px 0;
    outline: none;
}
button {
    width: 200px;
    cursor: pointer;
    padding: 10px 20px;
    outline: none;
    border: none;
    background-color: #6d9886;
}
.home,
.home__form,
.website__item,
.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}
.home__form > h2,
.home__form {
    margin-bottom: 30px;
    text-align: center;
    width: 100%;
}
.home {
    min-height: 100vh;
    padding: 20px;
    width: 100%;
}
.website__container {
    width: 100%;
    min-height: 50vh;
    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    padding: 15px;
}
.website__item {
    width: 80%;
    margin: 10px;
    background-color: #f7f7f7;
    border-radius: 5px;
    padding: 30px;
    box-shadow: 0 2px 8px 0 rgba(99, 99, 99, 0.2);
}
.website__item > img {
    width: 70%;
    border-radius: 5px;
}
.website__item > h3 {
    margin: 10px 0;
}
.website__item > p {
    text-align: center;
    opacity: 0.7;
}
.loading {
    height: 100vh;
    background-color: #f2e7d5;
}

Update theĀ App.jsĀ file to display an input field that allows you to provide the website’s URL.

import React, { useState } from "react";

const App = () => {
    const [url, setURL] = useState("");

    const handleSubmit = (e) => {
        e.preventDefault();
        console.log({ url });
        setURL("");
    };

    return (
        
            
                Website Aggregator</h2>
                Provide the website URL</label>
                 setURL(e.target.value)}
                />
                ADD WEBSITE</button>
            </form>
        </div>
    );
};

export default App;

Congratulations! You’ve successfully created the application’s user interface. In the following sections, I’ll walk you through scraping data from websites using Puppeteer and getting a website’s description and title via ChatGPT.

How to scrape data using Puppeteer in Node.js

PuppeteerĀ is a Node.js library that automates several browser actions such as form submission, crawling single-page applications, UI testing, and in particular, web scraping and generating screenshots of web pages.

Here, I’ll guide you through scraping the website’s content via Puppeteer in Node.js. We’ll send the website url provided by the user to the Node.js server and scrape the website’s content via its URL.

Create an endpoint on the server that accepts the website’s URL from the React app.

app.post("/api/url", (req, res) => {
    const { url } = req.body;

    //\ud83d\udc47\ud83c\udffb The URL from the React app
    console.log(url);
});

Import the Puppeteer library and scrape the website’s content as done below:

//\ud83d\udc47\ud83c\udffb Import Puppeteer at the top
const puppeteer = require("puppeteer");

app.post("/api/url", (req, res) => {
    const { url } = req.body;

    //\ud83d\udc47\ud83c\udffb Puppeteer webscraping function
    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto(url);

        //\ud83d\udc47\ud83c\udffb returns all the website content
        const websiteContent = await page.evaluate(() => {
            return document.documentElement.innerText.trim();
        });

        //\ud83d\udc47\ud83c\udffb returns the website meta image
        const websiteOgImage = await page.evaluate(() => {
            const metas = document.getElementsByTagName("meta");
            for (let i = 0; i < metas.length; i++) {
                if (metas[i].getAttribute("property") === "og:image") {
                    return metas[i].getAttribute("content");
                }
            }
        });

        console.log({ websiteContent, websiteOgImage })
        await browser.close();
    })();
});

Add a function within the React app that sends the URL to theĀ api/url/Ā endpoint.

const handleSubmit = (e) => {
    e.preventDefault();
    setLoading(true);
    setURL("");
    //\ud83d\udc47\ud83c\udffb Calls the function.
    sendURL();
};

async function sendURL() {
    try {
        const request = await fetch("http://localhost:4000/api/url", {
            method: "POST",
            body: JSON.stringify({
                url,
            }),
            headers: {
                Accept: "application/json",
                "Content-Type": "application/json",
            },
        });
        const data = await request.json();
        //\ud83d\udc47\ud83c\udffb toggles the loading state if the request is successful
        if (data.message) {
            setLoading(false);
        }
    } catch (err) {
        console.error(err);
    }
}

From the code snippet above, we added a loading state that describes the state of the API request.

const [loading, setLoading] = useState(false);

Create aĀ LoadingĀ component that is shown to the users when the request is pending.

import React from "react";

const Loading = () => {
    return (
        
            Loading, please wait...</h1>
        </div>
    );
};

export default Loading;

Display theĀ LoadingĀ component whenever the content is yet to be available.

import Loading from "./Loading";
//\ud83d\udc47\ud83c\udffb Add this code within the App.js component

if (loading) {
    return ;
}

Congratulations! You’ve learnt how to scrape content from websites using Puppeteer. In the upcoming section, you’ll learn how to communicate with ChatGPT in Node.js by generating websites’ descriptions and brand names.

How to communicate with ChatGPT in Node.js

ChatGPT is not yet available as a public API. Therefore, to use it, we have to scrape our way in – meaning we’ll perform a full browser automation that logs in to the OpenAI website, solves the captcha, and send an API request with the OpenAI cookies.

Fortunately, a public library that does this isĀ availableĀ and has been installed as part of the project requirement.

Import theĀ ChatGPT API libraryĀ and create a function that sends a request to ChatGPT.

import { ChatGPTAPIBrowser } from "chatgpt";

async function chatgptFunction(content) {
    // use puppeteer to bypass cloudflare (headful because of captchas)
    const api = new ChatGPTAPIBrowser({
        email: "",
        password: "",
    });
    await api.initSession();
    //\ud83d\udc47\ud83c\udffb Extracts the brand name from the website content
    const getBrandName = await api.sendMessage(
        `I have a raw text of a website, what is the brand name in a single word? ${content}`
    );
    //\ud83d\udc47\ud83c\udffb Extracts the brand description from the website content
    const getBrandDescription = await api.sendMessage(
        `I have a raw text of a website, can you extract the description of the website from the raw text. I need only the description and nothing else. ${content}`
    );
    //\ud83d\udc47\ud83c\udffb Returns the response from ChatGPT
    return {
        brandName: getBrandName.response,
        brandDescription: getBrandDescription.response,
    };
}

Chat GPT is super intelligent, and it will answer any question we will ask it. So basically, we will send it to write us the brand name and the description based on the complete website HTML.The brand name can usually be found on the ā€œog:site_name,ā€ but to show you how cool it is, we will let ChatGPT extract it. As for the description, it’s pretty crazy. It will tell us what the site is about and summarize everything!

Next,Update theĀ api/urlĀ route to as done below:

//\ud83d\udc47\ud83c\udffb holds all the ChatGPT result
const database = [];
//\ud83d\udc47\ud83c\udffb generates a random string as ID
const generateID = () => Math.random().toString(36).substring(2, 10);

app.post("/api/url", (req, res) => {
    const { url } = req.body;

    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto(url);
        const websiteContent = await page.evaluate(() => {
            return document.documentElement.innerText.trim();
        });
        const websiteOgImage = await page.evaluate(() => {
            const metas = document.getElementsByTagName("meta");
            for (let i = 0; i < metas.length; i++) {
                if (metas[i].getAttribute("property") === "og:image") {
                    return metas[i].getAttribute("content");
                }
            }
        });
        //\ud83d\udc47\ud83c\udffb accepts the website content as a parameter
        let result = await chatgptFunction(websiteContent);
        //\ud83d\udc47\ud83c\udffb adds the brand image and ID to the result
        result.brandImage = websiteOgImage;
        result.id = generateID();
    //\ud83d\udc47\ud83c\udffb adds the result to the array
        database.push(result);
    //\ud83d\udc47\ud83c\udffb returns the results
        return res.json({
            message: "Request successful!",
            database,
        });

        await browser.close();
    })();
});

To display the response within the React application, create a state that holds the server’s response.

const [websiteContent, setWebsiteContent] = useState([]);

async function sendURL() {
    try {
        const request = await fetch("http://localhost:4000/api/url", {
            method: "POST",
            body: JSON.stringify({
                url,
            }),
            headers: {
                Accept: "application/json",
                "Content-Type": "application/json",
            },
        });
        const data = await request.json();
        if (data.message) {
            setLoading(false);
            //\ud83d\udc47\ud83c\udffb update the state with the server response
            setWebsiteContent(data.database);
        }
    } catch (err) {
        console.error(err);
    }
}

Lastly, update theĀ App.jsĀ layout to display the server’s response to the user.

const App = () => {
    //...other code statements
//\ud83d\udc47\ud83c\udffb remove the quotation marks around the description
const trimDescription = (content) =>
        content.match(/(?:"[^"]*"|^[^"]*$)/)[0].replace(/"/g, "");

    return (
        
            
                Website Aggregator</h2>
                Provide the website URL</label>
                 setURL(e.target.value)}
                />
                ADD WEBSITE</button>
            </form>
            
                {websiteContent.map((item) => (
                    
                        
                        {item?.brandName}</h3>
                        {trimDescription(item?.brandDescription)}</p>
                    </div>
                ))}
            </main>
        </div>
    );
};

Congratulations!šŸŽ‰ You’ve completed the project for this tutorial.

Here is a sample of the result gotten from the application:

Conclusion

So far, we have covered,

  • what ChatGPT is,
  • how to scrape website content using Puppeteer, and
  • how to communicate with ChatGPT in a Node.js application

This tutorial walks you through an example of an application you can build using Puppeteer and ChatGPT. ChatGPT can be seen as the ultimate personal assistant, very useful in various fields to enable us to work smarter and better.

The source code for this tutorial is available here:

https://github.com/novuhq/blog/tree/main/website-aggregator-with-chatgpt-react

Thank you for reading!

Help me out!

If you feel like this article helped you understand WebSockets better! I would be super happy if you could give us a star! And let me also know in the comments ā¤ļø https://github.com/novuhq/novu

Read More

You’re five minutes away from your first Novu-backed notification

Create a free account, send your first notification, all before your coffee gets cold... no credit card required.