💾 Archived View for jsreed5.org › log › 2024 › 202401 › 20240110-saving-gemini-documents-with-ncat.g… captured on 2024-08-31 at 11:40:49. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2024-02-05)
-=-=-=-=-=-=-
---
I access Geminispace using GUI clients: Kristall on desktop Linux and Deedum on Android. Sometimes I come across a file served on someone's capsule that I want to save, or I want to archive a page I enjoy reading. Kristall can save documents to local storage, but Deedum cannot.
I perform many of my daily computing tasks from the command line, including bookmark management. Since I already save links to documents using Bash, I decided to write a Bash alias that could save documents directly using `ncat`. The result follows:
# get document over gemini function gemget() { url="$1" if [[ "$2" ]]; then archiveloc="$2"; else archiveloc="."; fi if [[ "$url" == *"/" ]]; then url="${url}index.gmi" fi host="${url#gemini://}" if [[ "$host" == *"/"* ]]; then host="${host%%/*}" else url="${url}/index.gmi" fi if [[ "$host" == *":"* ]]; then port="{host#*:}" host="${host%:*}" else port="1965" fi file="$(python3 -c "import urllib.parse; print(urllib.parse.quote_plus(\"$url\"))")" echo -e "${url}\r" | ncat --ssl --no-shutdown "$host" "$port" | sed "1d" > "${archiveloc}/${file}" }
The function takes a Gemini URL, port optional, and saves the document at that location to a specified directory. The local filename is the encoded URL from which the file was retrieved.
I did need to use a single Python statement to properly encode the URL. I would like to do this using pure Bash, but I'm not fluent enough to be able to do it myself. In the interim, the alias works well enough for my purposes. The ending `sed` statement removes the status code returned by the server.
At the core of this function is the `ncat` statement at the very end. ncat, as has been noted elsewhere, can actually function as a full-fledged Gemini browser. It can connect using SSL, send requests with query strings, even use client certificates to access user-specific resources:
echo -e "[Gemini URL]\r" | ncat --ssl --ssl-cert [PEM-format client certificate file] --ssl-key [PEM-format client certificate key file] --no-shutdown "[host]" [port]
Using ncat itself as a pipe allows devices to send data to each other over the Gemini protocol directly from the command line, without needing dedicated server software. There are already examples of projects and home setups that use Gemini this way.
Such flexibility and robustness is one of my favorite aspects of Gemini. By combining simple tools in clever ways, Geminispace has grown into a vibrant (if still niche) Internet community. We write logs, we share documents, we build services, we play games, we chat and debate and laugh together. It doesn't take much for people to come together and make something exciting.
---
[Last updated: 2024-01-10]