💾 Archived View for midnight.pub › posts › 1880 captured on 2024-05-26 at 14:37:03. Gemini links have been rewritten to link to archived content

View Raw

More Information

➡️ Next capture (2024-08-18)

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

Midnight Pub

A Lua "post to midnight.pub" script alternative

~inquiry

The midnight.pub manual (https://midnight.pub/manual) has a "Post using your terminal" section. The script mentioned worked fine for simple cases, but content containing bash special characters was problematic, so I set off to create what I believe to be a better version in Lua, which follows below. The key was discovering curl's syntax for inhaling content from a file into a form variable (-F "variable=<file"), thereby bypassing trying to cram it into a pair of quotes and struggling with how to escape special shell characters.

As always, it's likely not to the reader's standards regarding indentation, line spacing between sections, absence of comments, etc... but it's been working great for me of late, so hopefully it's useful to you too (NOTE: I substituted two space characters for each tab character, because I'm "set ts=2" in vim, but didn't want it looking like the humongous 8 characters per tab character in your browser...)... also, its "help" mentions an "mp-cookie" script shown farther below.

The "midnight" script:

#! /usr/bin/env lua
local function escape(line)
  line = string.gsub(line, '%


, '\\


)
  line = string.gsub(line, '`', '\\`')
  line = string.gsub(line, '"', '\\"')
  return line
end
local host = 'https://midnight.pub'
local function help(error)
  if error then
    print('===')
    print('=== ' .. error)
    print('===\n')
  end
  print([[midnight [-h] [-t "<title>" <file>] [-p <post_id> <file>] [-r <reply_id> <file>]

NOTE: -t implies new post
NOTE: -p is for "reply to post"
NOTE: -r is for "reply to reply"

SETUP: first login via elinks, 'K' to show cookies, copy/paste
session cookie like this:

$ mp-cookie >$HOME/.config/.midnight

then <enter>, <ctrl-d>]])
end
local function session()
  local s = ''
  local handle = io.open(os.getenv('HOME') .. '/.config/.midnight')
  s = handle:read()
  handle:close()
  return s
end
local function execute(command)
  if os.getenv('TEST') then
    print(command)
  else
    os.execute(command)
  end
end
if arg[1] then
  if arg[1] == '-h' then
    help()
  elseif arg[1] == '-t' then
    if arg[3] then
      local command = 'curl -F title="' .. escape(arg[2]) .. '" -F "content=<' .. arg[3] .. '" -b midn="' .. session() .. '" ' .. host .. '/posts/save'
      execute(command)
    else
      help('Missing -t "<title> <file>" argument')
    end
  else
    if arg[3] then
      local command = 'curl -F "reply=<' .. arg[3] .. '" -b midn="' .. session() .. '" ' .. host
      if arg[1] == '-p' then
        command = command .. '/posts/' .. arg[2] .. '/reply'
        execute(command)
      elseif arg[1] == '-r' then
        command = command .. '/replies/' .. arg[2] .. '/save'
        execute(command)
      else
        help('Invalid option/flag')
      end
    else
      help('Missing ' .. arg[1] .. ' <id> <file> argument')
    end
  end
else
  help('One or more flags is required')
end

The mp-cookie script:

#! /usr/bin/env lua
local lines = {}
for line in io.stdin:lines() do
  line = string.gsub(line, '%s%S%s', '')
  line = string.gsub(line, '%s+', '')
  line = string.gsub(line, '|', '')
  table.insert(lines, line)
end
print(table.concat(lines, ''))

Write a reply