smartsitemap/ts/smartsitemap.classes.sitemapnews.ts

72 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-10-25 22:12:38 +00:00
import * as plugins from './smartsitemap.plugins';
2020-10-28 17:43:58 +00:00
import * as interfaces from './interfaces';
2020-10-25 22:12:38 +00:00
export class SitemapNews {
2020-10-28 17:43:58 +00:00
public rssItems: interfaces.IRssItem[] = [];
2020-10-26 00:52:04 +00:00
constructor(optionsArg: {}) {}
2020-10-28 15:54:39 +00:00
public async readAndAddFromRssFeedString(feedStringArg: string) {
const smartfeedInstance = new plugins.smartfeed.Smartfeed();
const parsedFeed = await smartfeedInstance.parseFeedFromString(feedStringArg);
2020-10-28 17:43:58 +00:00
this.rssItems = this.rssItems.concat(parsedFeed.items);
2020-10-28 15:54:39 +00:00
}
public async readAndAddFromRssFeedUrl(urlArg: string) {
2020-10-26 00:52:04 +00:00
const smartfeedInstance = new plugins.smartfeed.Smartfeed();
const parsedFeed = await smartfeedInstance.parseFeedFromUrl(urlArg);
2020-10-28 17:43:58 +00:00
this.rssItems = this.rssItems.concat(parsedFeed.items);
}
public async readAndParseArticles(articleArrayArg: plugins.tsclass.content.IArticle[]) {
const rssItemArray = articleArrayArg.map((articleArg): interfaces.IRssItem => {
return {
title: articleArg.title,
content: articleArg.content,
isoDate: new Date(/* TODO: put article timestamp here */).toISOString()
};
});
this.rssItems = this.rssItems.concat(rssItemArray);
2020-10-26 00:52:04 +00:00
}
public exportSitemapXml() {
const urls: {
loc: string;
'news:news': {
'news:publication': {
'news:name': string;
'news:language': string;
};
2020-10-28 17:43:58 +00:00
'news:publication_date': string;
'news:keywords': string;
'news:title': string;
2020-10-26 00:52:04 +00:00
};
}[] = [];
2020-10-28 17:43:58 +00:00
for (const itemArg of this.rssItems) {
2020-10-26 00:52:04 +00:00
urls.push({
loc: itemArg.link,
2020-10-28 17:43:58 +00:00
'news:news': {
'news:publication': {
'news:language': 'en',
'news:name': 'some name',
2020-10-26 00:52:04 +00:00
},
2020-10-28 17:43:58 +00:00
'news:keywords': '',
'news:publication_date': itemArg.isoDate,
'news:title': itemArg.title,
},
});
2020-10-26 00:52:04 +00:00
}
const sitemapObject: any = {
urlset: {
'@_xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9',
'@_xmlns:news': 'http://www.google.com/schemas/sitemap-news/0.9',
url: urls,
},
};
const smartxmlInstance = new plugins.smartxml.SmartXml();
const sitemapString = smartxmlInstance.createXmlFromObject(sitemapObject);
return sitemapString;
2020-10-25 22:12:38 +00:00
}
}