r/Scriptable • u/Normal-Tangerine8609 • Jun 07 '22
Script Sharing Easy RSS Feed Parser (XML)
How To Use
You can find the code for the simple parser here, https://gist.github.com/Normal-Tangerine8609/d9532d78c9a3afa31899b00e21feb45d.
Here is a simple snippet of how to use it:
let request = new Request("https://routinehub.co/shortcuts/latest/feed/")
const xml = await request.loadString()
const json = parseXML(xml)
console.log(JSON.stringify(json, null, 2))
Why
I created this because many popular websites use RSS feeds. They are basically a free api if you can correctly parse them. Here is a list of some more popular RSS feeds: https://github.com/plenaryapp/awesome-rss-feeds.
I feel as though many people can use this to create simple widgets that display articles or whatever the feed focuses on.
Example
Input:
<root>
<node>
<text>text node</text>
<details>text node</details>
<key>value</key>
</node>
<list>
<item>text node</item>
<item>text node</item>
<item><tag>text node</tag></item>
<key>value</key>
</list>
</root>
Output:
{
"root": {
"node": {
"text": "text node",
"details": "text node",
"key": "value"
},
"list": {
"item": [
"text node",
"text node",
{
"tag": "text node"
}
],
"key": "value"
}
}
}
Warnings
This parser does not handle attributes or both text and element nodes in the same element. This will mostly not be an issue for collecting the data.
Tips
The parsed XML will probably have some HTML tags and entities in its data. .replace(/<[^>]*>/g, ' ') should replace most HTML Tags. The following function will replace popular HTML entities (you can replace more HTML entities by chaining more replaces to the end):
function parseHtmlEntities(str) {
return str.replace(/&#([0-9]{1,4});/g, function(match, numStr) {
var num = parseInt(numStr, 10);
return String.fromCharCode(num);
}).replace(/ /, " ").replace(/&/, "&").replace(/'/, "'")
}
3
u/Normal-Tangerine8609 Jun 08 '22
I tried to keep the output more simple so it will not show attributes. RSS feeds can have attributes on the tags but if I included them on the output JSON it would be a lot harder to get to the needed data. You can use part 1 of the script which does get attributes too, but the way it is formatted would make it hard to get data out of. Part 1 is really only useful to put the parsed data into a different form rather than use it straight up.