Skip to content

Commit

Permalink
Instant Feed Add
Browse files Browse the repository at this point in the history
  • Loading branch information
derme302 committed May 10, 2024
1 parent 04b93d7 commit 2c0d33a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/app/feed/feed.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</ion-header>

<!-- Pull to refresh -->
<ion-refresher slot="fixed" (ionRefresh)="feedService.parseUrls($event)">
<ion-refresher slot="fixed" (ionRefresh)="feedService.syncEntriesWithUpstream($event)">
<ion-refresher-content></ion-refresher-content>
</ion-refresher>

Expand Down
75 changes: 14 additions & 61 deletions src/app/services/feed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> = [];
const feedList = this.sourcesService.getSources();
let feedResult;

// Update the cache for any feeds that need updating
for (const feed of feedList) {
const tempFeedData: Array<any> = [];
const nextPollDate = feed.lastRetrieved + (feed.pollingFrequency * 1000); // Milliseconds

if (Date.now() < nextPollDate) {
Expand All @@ -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();
}
Expand All @@ -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<any>): Array<any> {
return array.sort((a: any, b: any) => {
return new Date(b.isoDate).valueOf() - new Date(a.isoDate).valueOf();
});
}
}
58 changes: 58 additions & 0 deletions src/app/services/sources.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export class SourcesService {
tags: []
};

this.updateLocalCache(feedInfo, feedData);

this.addSource(feedInfo);
}

Expand Down Expand Up @@ -172,6 +174,28 @@ export class SourcesService {
return feed;
}

public updateLocalCache(feed: any, feedData: any): any {
const tempFeedData: Array<any> = [];

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,
Expand All @@ -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;
}
}

}

0 comments on commit 2c0d33a

Please sign in to comment.