diff options
| author | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-12 18:35:08 +0200 |
|---|---|---|
| committer | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-12 18:35:08 +0200 |
| commit | 23a56bd50b04211da3cab45f72c3390711b2416b (patch) | |
| tree | ab9a4a0136b4cce06dba7d853e296f682f807dbb /content/notes/parse-rss-with-lua.md | |
| parent | cecb4b48a39a3558979b9c4b50e45bf605a3684e (diff) | |
| download | mitjafelicijan.com-23a56bd50b04211da3cab45f72c3390711b2416b.tar.gz | |
Moved notes and posts into subfolders
Diffstat (limited to 'content/notes/parse-rss-with-lua.md')
| -rw-r--r-- | content/notes/parse-rss-with-lua.md | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/content/notes/parse-rss-with-lua.md b/content/notes/parse-rss-with-lua.md new file mode 100644 index 0000000..c2cdcbc --- /dev/null +++ b/content/notes/parse-rss-with-lua.md | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | --- | ||
| 2 | title: Parse RSS feeds with Lua | ||
| 3 | url: parse-rss-with-lua.html | ||
| 4 | date: 2023-05-23T12:00:00+02:00 | ||
| 5 | type: note | ||
| 6 | draft: false | ||
| 7 | tags: [lua, rss] | ||
| 8 | --- | ||
| 9 | |||
| 10 | Example of parsing RSS feeds with Lua. Before running the script install: | ||
| 11 | |||
| 12 | - feedparser with `luarocks install feedparser` | ||
| 13 | - luasocket with `luarocks install luasocket` | ||
| 14 | |||
| 15 | ```lua | ||
| 16 | local http = require("socket.http") | ||
| 17 | local feedparser = require("feedparser") | ||
| 18 | |||
| 19 | local feed_url = "https://mitjafelicijan.com/feed.rss" | ||
| 20 | |||
| 21 | local response, status, _ = http.request(feed_url) | ||
| 22 | if status == 200 then | ||
| 23 | local parsed = feedparser.parse(response) | ||
| 24 | |||
| 25 | -- Print out feed details. | ||
| 26 | print("> Title ", parsed.feed.title) | ||
| 27 | print("> Author ", parsed.feed.author) | ||
| 28 | print("> ID ", parsed.feed.id) | ||
| 29 | print("> Entries ", #parsed.entries) | ||
| 30 | |||
| 31 | for _, item in ipairs(parsed.entries) do | ||
| 32 | print("GUID ", item.guid) | ||
| 33 | print("Title ", item.title) | ||
| 34 | print("Link ", item.link) | ||
| 35 | print("Summary ", item.summary) | ||
| 36 | end | ||
| 37 | else | ||
| 38 | print("! Request failed. Status:", status) | ||
| 39 | end | ||
| 40 | ``` | ||
| 41 | |||
