aboutsummaryrefslogtreecommitdiff
path: root/content/notes/parse-rss-with-lua.md
blob: c28c20ca2ff307ba905c7c87b2014f604b78bf06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
---
title: Parse RSS feeds with Lua
url: parse-rss-with-lua.html
date: 2023-05-23T12:00:00+02:00
type: notes
draft: false
tags: [lua, rss]
---

Example of parsing RSS feeds with Lua. Before running the script install:

- feedparser with `luarocks install feedparser`
- luasocket with `luarocks install luasocket`

```lua
local http = require("socket.http")
local feedparser = require("feedparser")

local feed_url = "https://mitjafelicijan.com/feed.rss"

local response, status, _ = http.request(feed_url)
if status == 200 then
  local parsed = feedparser.parse(response)

  -- Print out feed details.
  print("> Title   ", parsed.feed.title)
  print("> Author  ", parsed.feed.author)
  print("> ID      ", parsed.feed.id)
  print("> Entries ", #parsed.entries)

  for _, item in ipairs(parsed.entries) do
    print("GUID    ", item.guid)
    print("Title   ", item.title)
    print("Link    ", item.link)
    print("Summary ", item.summary)
  end
else
  print("! Request failed. Status:", status)
end
```