Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for existing entries to prevent duplicates #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import getNewFeedItems from './feed';
import { addFeedItemToNotion, deleteOldUnreadItemsFromNotion } from './notion';
import {
getExistingPages,
addFeedItemToNotion,
deleteOldUnreadItemsFromNotion,
} from './notion';
import htmlToNotionBlocks from './parser';

async function index() {
const feedItems = await getNewFeedItems();
const existingPages = await getExistingPages(feedItems);

for (let i = 0; i < feedItems.length; i++) {
const item = feedItems[i];

const existingEntries = existingPages.find(
(page) => page.properties.Link.url === item.link
);

const isNewEntry = existingEntries === undefined;

const notionItem = {
title: item.title,
link: item.link,
content: htmlToNotionBlocks(item.content),
};

await addFeedItemToNotion(notionItem);
if (isNewEntry) {
await addFeedItemToNotion(notionItem);
}
}

await deleteOldUnreadItemsFromNotion();
Expand Down
18 changes: 18 additions & 0 deletions src/notion.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ const {

const logLevel = CI ? LogLevel.INFO : LogLevel.DEBUG;

export async function getExistingPages(items) {
const notion = new Client({
auth: NOTION_API_TOKEN,
logLevel,
});
const response = await notion.databases.query({
database_id: NOTION_READER_DATABASE_ID,
or: items.map((item) => ({
property: 'Link',
text: {
equals: item.link,
},
})),
});

return response.results;
}

export async function getFeedUrlsFromNotion() {
const notion = new Client({
auth: NOTION_API_TOKEN,
Expand Down