temp-codi / back-end

recommends clothes to wear based on current temperature + event + mbti type
MIT License
1 stars 1 forks source link

notion api #2

Open pol-dev-shinroo opened 1 year ago

pol-dev-shinroo commented 1 year ago

With the data retrieved by web-scraping (https://openweathermap.org/weather-conditions), we now want to insert the data into our notion database

pol-dev-shinroo commented 1 year ago

Retrieving database

const { Client } = require("@notionhq/client");

const notion = new Client({ auth: process.env.NOTION_ACCESS_TOKEN });

const getNotionDB = async () => {
  const dbId = process.env.NOTION_DB_ID;
  const response = await notion.databases.retrieve({ database_id: dbId });
  console.log(response);
};

module.exports = { getNotionDB };
pol-dev-shinroo commented 1 year ago

posting to notion database

const testNotionDB = async () => {
  const dbId = process.env.NOTION_DB_ID;
  // post request
  // req.body
  const scrapeData = await getScrapeData();
  console.log(scrapeData);

  for (let i = 0; i < scrapeData.length; i++) {
    const { id, main, desc } = scrapeData[i];
    const response = await notion.pages.create(
      notionParam({ dbId, id, main, desc })
    );
  }
};

body parameter

const notionParam = ({ dbId, id, main, desc }) => {
  return {
    parent: {
      database_id: dbId,
    },
    properties: {
      id: {
        title: [
          {
            text: {
              content: id,
            },
          },
        ],
      },
      main: {
        rich_text: [
          {
            text: {
              content: main,
            },
          },
        ],
      },
      desc: {
        rich_text: [
          {
            text: {
              content: desc,
            },
          },
        ],
      },
    },
  };
};

module.exports = { notionParam };
pol-dev-shinroo commented 1 year ago

Image