~ghost08/ratt

3 2

Lua all the things!

Details
Message ID
<CNZ9JBBDPKDO.183CDZ3NRYUMJ@micro-pc>
DKIM signature
missing
Download raw message
Hi guys!

So I've been thingking about integrating the config files even more with
lua. In fact it would be beneficial to write them all in lua!

It could be better declared what every attribude is (a selector or lua
function). More flexibility in writing new confs. Using some lua LSP to
check for errors. etc

So something like this:
https://git.sr.ht/~ghost08/ratt/tree/master/item/confs/youtube-search.yml

Could look like this:

```lua
local gojq = require("gojq")
local ytjson = ""

ratt.add({
	regex = "https://www.youtube.com/results.*",
	selectors = {
		httpsettings = {
			cookie = {},
			header = {
				authority = "www.youtube.com",
				["accept-language"] = "en-US,en;q=0.9",
			},
			useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36"
		},
		feed = {
			title = "#search",
			description = function(sel)
				sel:find("script"):each(function(_, s)
					local index = s:text():find("var ytInitialData")
					if index == nil then
						return
					end
					local data = s:text():sub(index + 20)
					data = data:sub(0, #data - 1)
					local query, err = gojq.parse([[
					  [ .contents|
						..|.videoRenderer? |
						select(. !=null) |
						{
						  title: .title.runs[0].text,
						  channel: .longBylineText.runs[0].text,
						  duration:.lengthText.simpleText,
						  views: .shortViewCountText.simpleText,
						  date: .publishedTimeText.simpleText,
						  videoID: .videoId,
						  thumbs: .thumbnail.thumbnails[0].url
						}
					  ]
					]])
					if err ~= nil then
						error("gojq:" .. err)
					end
					data, err = query:runMap(data)
					if err ~= nil then
						error("gojq runMap:" .. err)
					end
					ytjson = data
				end)
				print("Youtube search results")
			end,
			authorname = "",
			authoremail = "",
			item = {
				container = function(sel)
					local data = ytjson[1]
					print(#data)
				end,
				title = function(sel)
					local data = ytjson[1]
					print(data[index + 1]["title"])
				end,
				link = function(sel)
					local data = ytjson[1]
					print("https://www.youtube.com/watch?v=" .. data[index + 1]["videoID"])
				end,
				linkattr = "href",
				created = "",
				createdformat = "",
				description = "",
				image = function(sel)
					local data = ytjson[1]
					print(data[index + 1]["thumbs"])
				end,
				imageattr = "src"
			},
		},
	}
})
```

PS: also using this mail, to see how much of you guys are using ratt
enough to care.

----------------------------------
https://useplaintext.email/
pgp: https://mgyar.me/vladimir.gpg
Details
Message ID
<20221030154603.1c3b33e2.mckinley@aaathats3as.com>
In-Reply-To
<CNZ9JBBDPKDO.183CDZ3NRYUMJ@micro-pc> (view parent)
DKIM signature
missing
Download raw message
I think this would be a good change. It would make it much more
intuitive to share variables and even code when extracting data.

Plus, I don't like YAML very much and it would be a good excuse to
practice Lua some more. :)
Details
Message ID
<CQ3SWWY112Y5.2MM5ES2SXFX5T@micro-pc>
In-Reply-To
<20221030154603.1c3b33e2.mckinley@aaathats3as.com> (view parent)
DKIM signature
missing
Download raw message
The changes are there, now everything is in lua scripts.

- converted all the configs to lua
- docs and manpages are updated to lua

The only thing is, that the functions can run just sequentially. This
really slows down the creation of items, mainly if the functions also
fetch some data from the web. There is a async library for gopher-lua,
so maybe that could help. https://github.com/CuberL/glua-async

Meanwhile I would like everyone to test the new approach and report any
defects.

Happy hacking :)
Reply to thread Export thread (mbox)