Add in rss parsing library and option to use it instead of the API

This commit is contained in:
hockeymikey 2023-04-29 10:31:11 -05:00
parent 8ca21edd22
commit 7cb6db93f4
4 changed files with 74 additions and 23 deletions

View File

@ -219,6 +219,7 @@ Display news and updates from any RSS-enabled service.
**`limit`** | `number` | _Optional_ | The number of posts to return. If you haven't specified an API key, this will be limited to 10
**`orderBy`** | `string` | _Optional_ | How results should be sorted. Can be either `pubDate`, `author` or `title`. Defaults to `pubDate`
**`orderDirection`** | `string` | _Optional_ | Order direction of feed items to return. Can be either `asc` or `desc`. Defaults to `desc`
**`parseLocally`** | `boolean` | _Optional_ | If true parse the rss feed locally instead of using the rss2json API.
#### Example

View File

@ -43,7 +43,8 @@
"vue-select": "^3.20.2",
"vue-swatches": "^2.1.1",
"vue-toasted": "^1.1.28",
"vuex": "^3.6.2"
"vuex": "^3.6.2",
"rss-parser": "3.13.0"
},
"devDependencies": {
"@architect/sandbox": "^4.5.2",

View File

@ -31,6 +31,7 @@
</template>
<script>
import * as Parser from 'rss-parser';
import WidgetMixin from '@/mixins/WidgetMixin';
import { widgetApiEndpoints } from '@/utils/defaults';
@ -47,11 +48,14 @@ export default {
/* The URL to users atom-format RSS feed */
rssUrl() {
if (!this.options.rssUrl) this.error('Missing feed URL');
return encodeURIComponent(this.options.rssUrl || '');
return this.options.rssUrl || '';
},
apiKey() {
return this.options.apiKey;
},
parseLocally() {
return this.options.parseLocally;
},
limit() {
const usersChoice = this.options.limit;
if (usersChoice) return usersChoice;
@ -73,8 +77,12 @@ export default {
const limit = this.limit && this.apiKey ? `&count=${this.limit}` : '';
const orderBy = this.orderBy && this.apiKey ? `&order_by=${this.orderBy}` : '';
const direction = this.orderDirection ? `&order_dir=${this.orderDirection}` : '';
return `${widgetApiEndpoints.rssToJson}?rss_url=${this.rssUrl}`
+ `${apiKey}${limit}${orderBy}${direction}`;
if (this.parseLocally) {
return this.rssUrl;
} else {
return `${widgetApiEndpoints.rssToJson}?rss_url=${encodeURIComponent(this.rssUrl)}`
+ `${apiKey}${limit}${orderBy}${direction}`;
}
},
},
filters: {
@ -88,31 +96,51 @@ export default {
},
},
methods: {
/* Make GET request to Rss2Json */
/* Make GET request to whatever endpoint we are using */
fetchData() {
this.makeRequest(this.endpoint).then(this.processData);
},
/* Assign data variables to the returned data */
processData(data) {
const { feed, items } = data;
this.meta = {
title: feed.title,
link: feed.link,
author: feed.author,
description: feed.description,
image: feed.image,
};
async processData(data) {
if (this.parseLocally) {
const parser = new Parser();
const { link, title, items, author, description, image } = await parser.parseString(data);
this.meta = {
title,
link,
author,
description,
image,
};
this.processItems(items);
} else {
const { feed, items } = data;
this.meta = {
title: feed.title,
link: feed.link,
author: feed.author,
description: feed.description,
image: feed.image,
};
this.processItems(items);
}
},
processItems(items) {
const posts = [];
items.forEach((post) => {
let { length } = items;
if (this.limit) {
length = this.limit;
}
for (let i = 0; length > i; i += 1) {
posts.push({
title: post.title,
description: post.description,
image: post.thumbnail,
author: post.author,
date: post.pubDate,
link: post.link,
title: items[i].title,
description: items[i].description,
image: items[i].thumbnail,
author: items[i].author,
date: items[i].pubDate,
link: items[i].link,
});
});
}
this.posts = posts;
},
},

View File

@ -4093,7 +4093,7 @@ enquirer@^2.3.5:
dependencies:
ansi-colors "^4.1.1"
entities@^2.0.0:
entities@^2.0.0, entities@^2.0.3:
version "2.2.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==
@ -8595,6 +8595,14 @@ router@~1.3.6:
setprototypeof "1.2.0"
utils-merge "1.0.1"
rss-parser@3.13.0:
version "3.13.0"
resolved "https://registry.yarnpkg.com/rss-parser/-/rss-parser-3.13.0.tgz#f1f83b0a85166b8310ec531da6fbaa53ff0f50f0"
integrity sha512-7jWUBV5yGN3rqMMj7CZufl/291QAhvrrGpDNE4k/02ZchL0npisiYYqULF71jCEKoIiHvK/Q2e6IkDwPziT7+w==
dependencies:
entities "^2.0.3"
xml2js "^0.5.0"
rsup-progress@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/rsup-progress/-/rsup-progress-3.0.0.tgz#e5eab5c1e75794cc288d567aa765b50faaf0cc89"
@ -10562,6 +10570,19 @@ xml2js@0.4.19:
sax ">=0.6.0"
xmlbuilder "~9.0.1"
xml2js@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.5.0.tgz#d9440631fbb2ed800203fad106f2724f62c493b7"
integrity sha512-drPFnkQJik/O+uPKpqSgr22mpuFHqKdbS835iAQrUC73L2F5WkboIRd63ai/2Yg6I1jzifPFKH2NTK+cfglkIA==
dependencies:
sax ">=0.6.0"
xmlbuilder "~11.0.0"
xmlbuilder@~11.0.0:
version "11.0.1"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==
xmlbuilder@~9.0.1:
version "9.0.7"
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d"