前言

路由触发

Untitled

async function fetchAndApply(request) {
  if (request.method === "OPTIONS") {
    return handleOptions(request);
  }
  let url = new URL(request.url);
  if (url.pathname === "/" + API_TOKEN + "/bookmarklet") {
    let response = new Response(await add_subscribe(url.searchParams));
    response.headers.set("Content-Type", "text/html;charset=UTF-8");
    return response;
  }
  let response;
  let err_body = pages.replace(
    "Kali-Team",
    "Path error, please check Token parameter."
  );
  response = new Response(err_body);
  response.headers.set("Content-Type", "text/html;charset=UTF-8");
  return response;
}

判断是否已经存在

async function filter_from_database(rss_url) {
  const options = {
    method: "POST",
    headers: {
      "Notion-Version": "2022-02-22",
      "Content-Type": "application/json",
      Authorization: "Bearer " + NOTION_TOKEN,
    },
    body:
      '{"filter":{"or":[{"property":"Link","rich_text":{"equals":"' +
      rss_url +
      '"}}]}}',
  };
  let response = await fetch(
    "<https://api.notion.com/v1/databases/>" + SOURCE_ID + "/query",
    options
  );
  let body = await response.json();
  let results = body.results;
  let title = null;
  if (results.length > 0) {
    let titles = results[0].properties.Title.title;
    if (titles.length > 0) {
      title = titles[0].plain_text;
    } else {
      title = "";
    }
  }
  return title;
}

添加订阅

async function create_page(rss_url) {
  const options = {
    method: "POST",
    headers: {
      "Notion-Version": "2022-02-22",
      "Content-Type": "application/json",
      Authorization: "Bearer " + NOTION_TOKEN,
    },
    body:
      '{"parent":{"database_id":"' +
      SOURCE_ID +
      '"},"properties":{"Link":{"url":"' +
      rss_url +
      '"},"Enabled":{"checkbox":true}}}',
  };
  let response = await fetch("<https://api.notion.com/v1/pages>", options);
  let body = await response.json();
  let properties = body.properties;
  let title = null;
  if (properties.length > 0) {
    let titles = properties.Title.title;
    if (titles.length > 0) {
      title = titles[0].plain_text;
      rss_url = rss_url + title;
    } else {
      rss_url = null;
    }
  }
  return rss_url;
}

部署云函数

Untitled

const NOTION_TOKEN = "secret_xxx";
const SOURCE_ID = "8a49af58-5aa8-4420-8ee0-85b3814e1a0d";
const API_TOKEN = "14fe27c312f2828deb73bb1c7bfd92dc";

Untitled

Untitled

参考

Powered by Kali-Team