💾 Archived View for tilde.club › ~oldernow › 2023-11-30-10-57-16.gmi captured on 2023-12-28 at 17:17:08. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Thursday somehow became Codeday

I've been enjoying - when not suffering at the hands of my own stupidity - messing with scripting to create versions of Gemini pages showing dead links. It's far from perfect, but mostly does what I need it to do. Much thanks to @skyjake for the insights!

This script that I call test-gemini-links (the name is inadequate, but you know how script evolution can take code past initial naming...) does the medium lifting. It depends on 'gemget' and 'curl':

#! /usr/bin/env lua
if arg[1] then
  local connect_timeout = 3
  local errfile = '/tmp/errmsg'
  local handle = nil
  if os.getenv('FILE') then
    handle = io.open(arg[1], 'r')
  else
    handle = io.popen('gemget ' .. arg[1])
  end
  for line in handle:lines() do
    local url,title = string.match(line, '^=>%s*(%S*)%s*(.*)


)
    if url then
      local exit_okay, exit_termination_type, exit_status
      if string.match(url, '^gemini') then
        exit_okay, exit_termination_type, exit_status = os.execute('gemget --connect-timeout ' .. connect_timeout .. ' ' .. url .. ' 1>/dev/null 2>' .. errfile)
      elseif string.match(url, '^http') or string.match(url, '^gopher') then
        exit_okay, exit_termination_type, exit_status = os.execute('curl --connect-timeout ' .. connect_timeout .. ' ' .. url .. ' 1>/dev/null 2>' .. errfile)
      else
        url = arg[1] .. '/' .. url
        exit_okay, exit_termination_type, exit_status = os.execute('gemget --connect-timeout ' .. connect_timeout .. ' ' .. url .. ' 1>/dev/null 2>' .. errfile)
      end
      line = '=> ' .. url .. ' ' .. title
      if exit_status == 1 then
        local err = io.open(errfile, 'r')
        local msg = err:read('*a')
        err:close()
        print('```')
        print('DEAD ' .. line)
        io.stdout:write(msg)
        print('```')
      else
        print(line)
      end
    else
      print(line)
    end
  end
  handle:close()
else
  print('=== missing URL argument')
end

I call command line front-end script "gemopen", which runs the previous script against links it finds in the page referenced by the input url, puts the results in a file (morphs the input url to a filesystem-friendly name), then calls Lagrange to open the file. An optional second argument leads to deleting the file after waiting a bit for Lagrange to finish loading it:

#! /usr/bin/env lua
if arg[1] then
  local file = string.gsub(arg[1], '^.-://', '')
  file = 'gemopen-' .. string.gsub(file, '[/~]', '@') .. '.gmi'
  os.execute('test-gemini-links ' .. arg[1] .. ' 2>&1 | tee ' .. file)
  os.execute('lagrange ' .. file)
  if arg[2] then
    os.execute('sleep 2') -- give lagrange a chance to start
    os.execute('rm ' .. file)
  end
else
  print('=== missing URL argument')
end