Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

Act as: MERN Developer Technology stack: Node.js, Express.js, MongoDB, Mongoose, ReactJS…

  1. User

    Act as: MERN Developer

    Technology stack: Node.js, Express.js, MongoDB, Mongoose, ReactJS

    Functionality: scrape google trends and populate graph in ui

    Task: Make an API that scrape google trends data and populate graph in UI. Generate only code with file names and folder structure. Separate the files by "###$$$###".

  2. Assistant

    Folder Structure:

    • server
      • routes
        • googleTrends.js
      • controllers
        • googleTrendsController.js
      • models
        • googleTrendsModel.js
      • app.js
    • client
      • src
        • components
          • Graph.js
        • App.js
        • index.js

    ###$$$###

    googleTrends.js:

    const express = require('express');
    const router = express.Router();
    const googleTrendsController = require('../controllers/googleTrendsController');
    
    router.get('/', googleTrendsController.getGoogleTrendsData);
    
    module.exports = router;
    

    ###$$$###

    googleTrendsController.js:

    const googleTrendsModel = require('../models/googleTrendsModel');
    
    exports.getGoogleTrendsData = async (req, res) => {
      try {
        const data = await googleTrendsModel.scrape();
        res.status(200).json({ data });
      } catch(err) {
        res.status(500).json({ err });
      }
    }
    

    ###$$$###

    googleTrendsModel.js:

    const puppeteer = require('puppeteer');
    
    exports.scrape = async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.goto('https://trends.google.com/trends/trendingsearches/daily?geo=US');
      const data = await page.evaluate(() => {
        const items = document.querySelectorAll('.title>a');
        const dataArr = [];
        items.forEach(item => {
          dataArr.push(item.textContent);
        });
        return dataArr;
      });
      await browser.close();
      return data;
    }
    

    ###$$$###

    app.js:

    const express = require('express');
    const app = express();
    const cors = require('cors');
    const googleTrendsRoutes = require('./routes/googleTrends');
    
    app.use(cors());
    app.use(express.json());
    app.use('/google-trends', googleTrendsRoutes);
    
    const PORT = process.env.PORT || 5000;
    app.listen(PORT, () => {
      console.log(`Server listening on port ${PORT}`);
    });
    

    ###$$$###

    Graph.js:

    import React, { useState, useEffect } from 'react';
    import axios from 'axios';
    import { Line } from 'react-chartjs-2';
    
    const Graph = () => {
      const [data, setData] = useState([]);
    
      useEffect(() => {
        const getData = async () => {
          try {
            const res = await axios.get('http://localhost:5000/google-trends');
            setData(res.data.data);
          } catch(err) {
            console.log(err);
          }
        }
        getData();
      }, []);
    
      const graphData = {
        labels: data,
        datasets: [
          {
            label: 'Google Trends',
            backgroundColor: 'rgba(75,192,192,0.4)',
            borderColor: 'rgba(75,192,192,1)',
            borderWidth: 1,
            hoverBackgroundColor: 'rgba(75,192,192,0.8)',
            hoverBorderColor: 'rgba(75,192,192,1)',
            data: [1, 2, 3, 4, 5, 6, 7]
          }
        ]
      }
    
      return (
        <Line data={graphData} />
      );
    }
    
    export default Graph;
    

    ###$$$###

    App.js:

    import React from 'react';
    import Graph from './components/Graph';
    
    const App = () => {
      return (
        <div>
          <h1>Google Trends</h1>
          <Graph />
        </div>
      );
    }
    
    export default App;
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.