smartsitemap/ts/smartsitemap.classes.smartsitemap.ts

88 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-03-24 23:14:34 +00:00
import { SitemapNews } from './smartsitemap.classes.sitemapnews.js';
2023-07-27 14:16:37 +00:00
import { type IUrlInfo, SitemapWebsite } from './smartsitemap.classes.sitemapwebsite.js';
2022-03-24 23:14:34 +00:00
import * as plugins from './smartsitemap.plugins.js';
import * as interfaces from './interfaces/index.js';
2020-10-25 22:12:38 +00:00
export class SmartSitemap {
constructor() {}
/**
2020-10-26 12:18:33 +00:00
* creates a sitemap for news from feedurl
2020-10-25 22:12:38 +00:00
*/
2020-10-26 12:18:33 +00:00
public async createSitemapNewsFromFeedUrl(feedUrlArg: string): Promise<string> {
2020-10-26 00:52:04 +00:00
const sitemapNewsInstance = new SitemapNews({});
2020-10-28 15:54:39 +00:00
await sitemapNewsInstance.readAndAddFromRssFeedUrl(feedUrlArg);
2020-10-26 12:18:33 +00:00
return sitemapNewsInstance.exportSitemapXml();
}
2020-10-28 17:43:58 +00:00
/**
* creates a sitemap for news from feedxmlstring
*/
public async createSitemapNewsFromAFeedStringArg(feedStringArg: string): Promise<string> {
const sitemapNewsInstance = new SitemapNews({});
await sitemapNewsInstance.readAndAddFromRssFeedString(feedStringArg);
return sitemapNewsInstance.exportSitemapXml();
}
/**
* creates a sitemap for news from an array of articles
*/
2023-07-27 14:16:37 +00:00
public async createSitemapNewsFromArticleArray(
articleArrayArg: plugins.tsclass.content.IArticle[]
): Promise<string> {
2020-10-28 17:43:58 +00:00
const sitemapNewsInstance = new SitemapNews({});
await sitemapNewsInstance.readAndParseArticles(articleArrayArg);
return sitemapNewsInstance.exportSitemapXml();
}
2020-10-26 12:18:33 +00:00
/**
* creates a normal sitemap from a list of urls
*/
2020-10-28 15:14:54 +00:00
public async createSitemapFromYmlString(yamlString: string): Promise<string> {
2023-07-27 14:16:37 +00:00
const yamlObject: interfaces.ISitemapYaml = await plugins.smartyaml.yamlStringToObject(
yamlString
);
2020-10-26 17:19:23 +00:00
const sitemapWebsite = new SitemapWebsite();
2023-07-27 14:16:37 +00:00
for (const urlArg of yamlObject.daily) {
2020-10-26 17:19:23 +00:00
sitemapWebsite.addUrl({
url: urlArg,
timestamp: Date.now() - 10000,
2023-07-27 14:16:37 +00:00
frequency: 'daily',
2020-10-26 17:19:23 +00:00
});
}
2020-10-28 15:14:54 +00:00
return sitemapWebsite.exportSitemapXml();
}
/**
* creates a normal sitemap from a list of urls
*/
public async createSitemapFromUrlInfoArray(urlInfosArg: IUrlInfo[]) {
const sitemapWebsite = new SitemapWebsite();
2023-07-27 14:16:37 +00:00
for (const urlInfo of urlInfosArg) {
2020-10-28 15:14:54 +00:00
sitemapWebsite.addUrl(urlInfo);
}
return sitemapWebsite.exportSitemapXml();
2020-10-25 22:12:38 +00:00
}
2021-01-03 02:08:00 +00:00
/**
* parses a sitemap url
*/
public async parseSitemapUrl(urlArg: string) {
2023-07-27 14:16:37 +00:00
const sitemapXml = await (
await new plugins.webrequest.WebRequest().request(urlArg, {
method: 'GET',
})
).text();
2021-01-03 02:08:00 +00:00
const parsedSitemap = await this.parseSitemap(sitemapXml);
return parsedSitemap;
}
/**
* parses a sitemap
*/
2021-01-03 02:48:45 +00:00
public async parseSitemap(sitemapXmlArg: string): Promise<interfaces.IParsedSiteMap> {
2023-07-27 14:16:37 +00:00
return new plugins.smartxml.SmartXml().parseXmlToObject(sitemapXmlArg);
2021-01-03 02:08:00 +00:00
}
2020-10-26 00:52:04 +00:00
}