Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
70 changes: 49 additions & 21 deletions src/components/Widgets/RssFeed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</template>

<script>
import * as Parser from 'rss-parser';
import WidgetMixin from '@/mixins/WidgetMixin';
import { widgetApiEndpoints } from '@/utils/defaults';

Expand All @@ -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;
Expand All @@ -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: {
Expand All @@ -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;
},
},
Expand Down
23 changes: 22 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down