Created
December 14, 2021 23:10
-
-
Save michal-h21/dddc9337de5e0bfefa0c33655819a7c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env texlua | |
-- get feed from gemini or https, and replace links to gemini with links to HTML proxy | |
-- it is meant for use with Newsboat. | |
-- to use it, add a similar line to your "urls" file: | |
-- "exec: gemrss gemini://marginalia.nu/log/feed.xml" | |
local socket = require("socket") | |
local ports = {gemini = 1965, https = 443, http = 80} | |
local commands = { | |
gemini = "ncat --ssl $host $port > $tmpfile", | |
https = "wget -q -O $tmpfile $url", | |
http = "wget -q -O $tmpfile $url", | |
} | |
local url = arg[1] or "gemini://gemini.susa.net/index.gmi" | |
local proxy = arg[2] or "https://proxy.vulpes.one/gemini/" | |
local parsed_url = socket.url.parse(url) | |
local scheme = parsed_url.scheme | |
local port = ports[scheme] | |
-- we must save result to tempfile | |
local tmpfile = os.tmpname() | |
local options = { | |
url = url, | |
scheme = parsed_url.scheme, | |
host = parsed_url.host, | |
port = port, | |
tmpfile= tmpfile | |
} | |
-- get command to be executed based on the URL scheme | |
local command_str = commands[scheme] | |
-- do string interpolation of options | |
command_str = command_str:gsub("%$(%w+)", options) | |
local command = io.popen(command_str, "w") | |
if scheme == "gemini" then | |
-- we must write URL to the server for Gemini | |
command:write(url .. "\r\n") | |
end | |
command:close() | |
-- read temp file | |
local i = 1 | |
for line in io.lines(tmpfile) do | |
-- ignore first line if it contains Gemini status code | |
if i > 1 or (not line:match("^%d+")) then | |
line = line:gsub("(<link[^>]+>)", function(link) | |
-- replace gemini links with proxy | |
return link:gsub("gemini://", proxy) | |
end) | |
print(line) | |
end | |
i = i + 1 | |
end | |
os.remove(tmpfile) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment