From 2c0d33afc350738ffaeb6e5108a820804158f662 Mon Sep 17 00:00:00 2001 From: Derme Date: Fri, 10 May 2024 21:16:45 +1000 Subject: [PATCH] Instant Feed Add --- src/app/feed/feed.page.html | 2 +- src/app/services/feed.service.ts | 75 ++++++----------------------- src/app/services/sources.service.ts | 58 ++++++++++++++++++++++ 3 files changed, 73 insertions(+), 62 deletions(-) diff --git a/src/app/feed/feed.page.html b/src/app/feed/feed.page.html index 9990b43..4372932 100644 --- a/src/app/feed/feed.page.html +++ b/src/app/feed/feed.page.html @@ -68,7 +68,7 @@ - + diff --git a/src/app/services/feed.service.ts b/src/app/services/feed.service.ts index 62776ec..2bc68a2 100644 --- a/src/app/services/feed.service.ts +++ b/src/app/services/feed.service.ts @@ -22,28 +22,26 @@ export class FeedService { private initFeedData(status: boolean) { if (status === true) { console.log('[FeedService] Storage onReady status changed to true'); - this.loadEntriesList(); + this.loadEntries(); } else { console.log('[FeedService] Storage onReady status changed to false'); } } - private async loadEntriesList() { + private async loadEntries() { const storage_feed = await this.storageService.getObjectFromStorage(STORAGE_FEED_DATA); if (storage_feed !== null) this.entries = storage_feed; } - public async parseUrls(event: any) { + public async syncEntriesWithUpstream(event: any) { const tempFeedMasterData: Array = []; const feedList = this.sourcesService.getSources(); - let feedResult; // Update the cache for any feeds that need updating for (const feed of feedList) { - const tempFeedData: Array = []; const nextPollDate = feed.lastRetrieved + (feed.pollingFrequency * 1000); // Milliseconds if (Date.now() < nextPollDate) { @@ -57,38 +55,20 @@ export class FeedService { } try { - feedResult = await this.sourcesService.downloadAndParseFeed(feed.url); + const feedDataDownloaded = await this.sourcesService.downloadAndParseFeed(feed.url); + const feedResult = this.sourcesService.updateLocalCache(feed, feedDataDownloaded); + for (const item of feedResult) { + tempFeedMasterData.push(item); + } } catch (error) { console.error('[FeedService] ' + error); continue; - } - - for (const item of feedResult.items) { - item.source = feed.title; - item.contentStripped = item.contentSnippet.substring(0, 120); - item.imgLink = this.getItemMedia(item); - item.feedUrl = feed.url; - item.bookmark = false; - tempFeedData.push(item); - tempFeedMasterData.push(item); - } - - this.storageService.set(feed.url, JSON.stringify(tempFeedData)); - - const source = this.sourcesService.getSource(feed.url); - source.healthy = true; - source.lastRetrieved = Date.now(); - this.sourcesService.setSource(feed.url, source); + } } - // Sort the feed by date - tempFeedMasterData.sort((a: any, b: any) => { - return new Date(b.isoDate).valueOf() - new Date(a.isoDate).valueOf(); - }); - // Update cache and values - this.entries = tempFeedMasterData; + this.entries = this.sortByDate(tempFeedMasterData); this.storageService.set(STORAGE_FEED_DATA, JSON.stringify(this.entries)); event.target.complete(); } @@ -101,36 +81,9 @@ export class FeedService { console.warn(`[FeedService] ${item.url} not found for bookmark status change`); } - private getItemMedia(item: any): string | null { - let mediaLinkURL = item.mediaContent; - - if (mediaLinkURL !== undefined) { - mediaLinkURL = mediaLinkURL[0].$.url; - } - - if (mediaLinkURL === undefined) - mediaLinkURL = this.findMediaInHTML(item.content); - - if (mediaLinkURL === undefined || mediaLinkURL === null) { - console.warn('[FeedService] No media link found for article'); - return null; - } - else { - return mediaLinkURL; - } - } - - private findMediaInHTML(htmlString: string): string | null { - - const parser = new DOMParser(); - const doc = parser.parseFromString(htmlString, 'text/html'); - const mediaLink = doc.querySelector('img'); - - if (mediaLink) { - const mediaLinkURL = mediaLink.getAttribute('src'); - return mediaLinkURL; - } else { - return null; - } + private sortByDate(array: Array): Array { + return array.sort((a: any, b: any) => { + return new Date(b.isoDate).valueOf() - new Date(a.isoDate).valueOf(); + }); } } diff --git a/src/app/services/sources.service.ts b/src/app/services/sources.service.ts index 86c4a33..aa27b3c 100644 --- a/src/app/services/sources.service.ts +++ b/src/app/services/sources.service.ts @@ -60,6 +60,8 @@ export class SourcesService { tags: [] }; + this.updateLocalCache(feedInfo, feedData); + this.addSource(feedInfo); } @@ -172,6 +174,28 @@ export class SourcesService { return feed; } + public updateLocalCache(feed: any, feedData: any): any { + const tempFeedData: Array = []; + + for (const item of feedData.items) { + item.source = feed.title; + item.contentStripped = item.contentSnippet.substring(0, 120); + item.imgLink = this.getItemMedia(item); + item.feedUrl = feed.url; + item.bookmark = false; + tempFeedData.push(item); + } + + this.storageService.set(feed.url, JSON.stringify(tempFeedData)); + + const source = this.getSource(feed.url); + source.healthy = true; + source.lastRetrieved = Date.now(); + this.setSource(feed.url, source); + + return tempFeedData; + } + async presentErrorToast(message: string) { const toast = await this.toastController.create({ message: message, @@ -183,4 +207,38 @@ export class SourcesService { await toast.present(); } + + private getItemMedia(item: any): string | null { + let mediaLinkURL = item.mediaContent; + + if (mediaLinkURL !== undefined) { + mediaLinkURL = mediaLinkURL[0].$.url; + } + + if (mediaLinkURL === undefined) + mediaLinkURL = this.findMediaInHTML(item.content); + + if (mediaLinkURL === undefined || mediaLinkURL === null) { + console.warn('[FeedService] No media link found for article'); + return null; + } + else { + return mediaLinkURL; + } + } + + private findMediaInHTML(htmlString: string): string | null { + + const parser = new DOMParser(); + const doc = parser.parseFromString(htmlString, 'text/html'); + const mediaLink = doc.querySelector('img'); + + if (mediaLink) { + const mediaLinkURL = mediaLink.getAttribute('src'); + return mediaLinkURL; + } else { + return null; + } + } + }