Skip to content

Commit

Permalink
Merge pull request #103 from 3-shake/nwiizo-patch-posts
Browse files Browse the repository at this point in the history
Update posts.ts
  • Loading branch information
nwiizo authored Nov 11, 2024
2 parents 674ecfc + 5e37b4c commit 2545a7d
Showing 1 changed file with 79 additions and 42 deletions.
121 changes: 79 additions & 42 deletions src/builder/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,116 @@ export default {};
type FeedItem = {
title: string;
link: string;
contentSnippet?: string;
isoDate?: string;
contentSnippet?: string | undefined;
isoDate?: string | undefined;
dateMiliSeconds: number;
};

const parser = new Parser();
let allPostItems: PostItem[] = [];

async function fetchFeedItems(url: string) {
const feed = await parser.parseURL(url);
if (!feed?.items?.length) return [];
async function fetchFeedItems(url: string): Promise<FeedItem[]> {
try {
console.log(`Fetching feed from: ${url}`);
const feed = await parser.parseURL(url);

if (!feed?.items?.length) {
console.warn(`No items found in feed: ${url}`);
return [];
}

// return item which has title and link
return feed.items
.map(({ title, contentSnippet, link, isoDate }) => {
return {
title,
contentSnippet: contentSnippet?.replace(/\n|\u2028/g, ''),
link,
isoDate,
dateMiliSeconds: isoDate ? new Date(isoDate).getTime() : 0,
};
})
.filter(({ title, link }) => title && link) as FeedItem[];
return feed.items
.map(({ title, contentSnippet, link, isoDate }) => {
if (!title || !link) {
console.warn(`Skipping item with missing title or link in feed: ${url}`);
return null;
}

const item: FeedItem = {
title,
link,
contentSnippet: contentSnippet?.replace(/\n|\u2028/g, ''),
isoDate,
dateMiliSeconds: isoDate ? new Date(isoDate).getTime() : 0,
};
return item;
})
.filter((item): item is FeedItem => item !== null);
} catch (error) {
console.error(`Error fetching feed from ${url}:`, error instanceof Error ? error.message : 'Unknown error');
return [];
}
}

async function getFeedItemsFromSources(sources: undefined | string[]) {
if (!sources?.length) return [];
let feedItems: FeedItem[] = [];
async function getFeedItemsFromSources(sources: undefined | string[]): Promise<FeedItem[]> {
if (!sources?.length) {
console.warn('No sources provided');
return [];
}

const feedItems: FeedItem[] = [];
for (const url of sources) {
const items = await fetchFeedItems(url);
if (items) feedItems = [...feedItems, ...items];
feedItems.push(...items);
}
return feedItems;
}

async function getMemberFeedItems(member: Member): Promise<PostItem[]> {
const { id, sources, name, includeUrlRegex, excludeUrlRegex } = member;

console.log(`Processing feeds for member: ${name}`);
const feedItems = await getFeedItemsFromSources(sources);
if (!feedItems) return [];

let postItems = feedItems.map((item) => {
return {
...item,
authorName: name,
authorId: id,
};
});
// remove items which not matches includeUrlRegex
let postItems = feedItems.map((item) => ({
...item,
authorName: name,
authorId: id,
}));

if (includeUrlRegex) {
const regex = new RegExp(includeUrlRegex);
postItems = postItems.filter((item) => {
return item.link.match(new RegExp(includeUrlRegex));
const matches = item.link.match(regex);
if (!matches) {
console.debug(`Filtered out item not matching includeUrlRegex: ${item.link}`);
}
return matches;
});
}
// remove items which matches excludeUrlRegex

if (excludeUrlRegex) {
const regex = new RegExp(excludeUrlRegex);
postItems = postItems.filter((item) => {
return !item.link.match(new RegExp(excludeUrlRegex));
const matches = item.link.match(regex);
if (matches) {
console.debug(`Filtered out item matching excludeUrlRegex: ${item.link}`);
}
return !matches;
});
}

return postItems;
}

(async function () {
for (const member of members) {
const items = await getMemberFeedItems(member);
if (items) allPostItems = [...allPostItems, ...items];
}
allPostItems.sort((a, b) => b.dateMiliSeconds - a.dateMiliSeconds);
fs.ensureDirSync(".contents");
try {
console.log('Starting feed processing...');

for (const member of members) {
const items = await getMemberFeedItems(member);
allPostItems.push(...items);
}

// Unicodeの行区切り文字 (line separator) を示すLS (U+2028)、段落区切り文字 (paragraph separator) を示すPS (U+2029)を削除する必要がある。
const json = JSON.stringify(allPostItems).replaceAll(/[\u2028\u2029]/g, "");
fs.writeFileSync(".contents/posts.json", json);
allPostItems.sort((a, b) => b.dateMiliSeconds - a.dateMiliSeconds);

fs.ensureDirSync(".contents");
const json = JSON.stringify(allPostItems).replaceAll(/[\u2028\u2029]/g, "");
fs.writeFileSync(".contents/posts.json", json);

console.log(`Successfully processed ${allPostItems.length} posts from ${members.length} members`);
} catch (error) {
console.error('Fatal error during feed processing:', error instanceof Error ? error.message : 'Unknown error');
process.exit(1);
}
})();

0 comments on commit 2545a7d

Please sign in to comment.