|
| 1 | +import { XMLParser } from 'fast-xml-parser'; |
| 2 | +import { FeedDetail, RssObj } from '../types'; |
| 3 | +import { |
| 4 | + FEED_AI_SUMMARY_IN_PROGRESS_MESSAGE, |
| 5 | + ONE_MINUTE, |
| 6 | + TIME_INTERVAL, |
| 7 | +} from '../constant'; |
| 8 | +import { ParserUtil } from './utils/parser-util'; |
| 9 | + |
| 10 | +export interface RawFeed { |
| 11 | + title: string; |
| 12 | + link: string; |
| 13 | + pubDate: string; |
| 14 | + description: string; |
| 15 | +} |
| 16 | + |
| 17 | +export abstract class BaseFeedParser { |
| 18 | + protected readonly xmlParser = new XMLParser({ |
| 19 | + ignoreAttributes: false, |
| 20 | + attributeNamePrefix: '@_', |
| 21 | + parseAttributeValue: true, |
| 22 | + trimValues: true, |
| 23 | + }); |
| 24 | + protected readonly parserUtil: ParserUtil; |
| 25 | + |
| 26 | + constructor(parserUtil: ParserUtil) { |
| 27 | + this.parserUtil = parserUtil; |
| 28 | + } |
| 29 | + |
| 30 | + async parseFeed(rssObj: RssObj, xmlData: string): Promise<FeedDetail[]> { |
| 31 | + // 각 포맷(atom1.0, rss2.0 등...) |
| 32 | + const rawFeeds = this.extractRawFeeds(xmlData); |
| 33 | + const timeMatchedFeeds = this.filterByTime(rawFeeds); |
| 34 | + const detailedFeeds = await this.convertToFeedDetails( |
| 35 | + rssObj, |
| 36 | + timeMatchedFeeds, |
| 37 | + ); |
| 38 | + |
| 39 | + return detailedFeeds; |
| 40 | + } |
| 41 | + |
| 42 | + abstract canParse(xmlData: string): boolean; |
| 43 | + protected abstract extractRawFeeds(xmlData: string): RawFeed[]; |
| 44 | + |
| 45 | + private filterByTime(rawFeeds: RawFeed[]): RawFeed[] { |
| 46 | + const now = new Date().setSeconds(0, 0); |
| 47 | + return rawFeeds.filter((item) => { |
| 48 | + const pubDate = new Date(item.pubDate).setSeconds(0, 0); |
| 49 | + const timeDiff = (now - pubDate) / (ONE_MINUTE * TIME_INTERVAL); |
| 50 | + return timeDiff >= 0 && timeDiff < 1; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + private async convertToFeedDetails( |
| 55 | + rssObj: RssObj, |
| 56 | + rawFeeds: RawFeed[], |
| 57 | + ): Promise<FeedDetail[]> { |
| 58 | + return Promise.all( |
| 59 | + rawFeeds.map(async (feed) => { |
| 60 | + const imageUrl = await this.parserUtil.getThumbnailUrl(feed.link); |
| 61 | + const date = new Date(feed.pubDate); |
| 62 | + const formattedDate = date.toISOString().slice(0, 19).replace('T', ' '); |
| 63 | + |
| 64 | + const content = (feed.description || '') |
| 65 | + .replace(/<[^>]*>/g, '') |
| 66 | + .replace(/ | /g, ' ') |
| 67 | + .replace(/&[^;]+;/g, '') |
| 68 | + .replace(/\s+/g, ' ') |
| 69 | + .trim(); |
| 70 | + |
| 71 | + return { |
| 72 | + id: null, |
| 73 | + blogId: rssObj.id, |
| 74 | + blogName: rssObj.blogName, |
| 75 | + blogPlatform: rssObj.blogPlatform, |
| 76 | + pubDate: formattedDate, |
| 77 | + title: feed.title, |
| 78 | + link: decodeURIComponent(feed.link), |
| 79 | + imageUrl: imageUrl, |
| 80 | + content: content, |
| 81 | + summary: FEED_AI_SUMMARY_IN_PROGRESS_MESSAGE, |
| 82 | + deathCount: 0, |
| 83 | + } as FeedDetail; |
| 84 | + }), |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments