pink/public_gopher/planet.dcgi
2024-04-09 23:26:39 -04:00

78 lines
2.0 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require 'cgi'
PROTOCOLS = {
'.plan' => 'finger',
'public_gemini' => 'gemini',
'public_gopher' => 'gopher',
'public_html' => 'https',
}
search = ENV['X_GOPHER_SEARCH']
params = search ? CGI.parse(search) : {}
filters = {
:protocol => params['protocol'].to_a.empty? ?
PROTOCOLS.values :
params['protocol'],
:size => params['size'].to_a.empty? ?
50 :
Integer(params['size'].first),
}
results = Dir.glob('/home/*/{public_{gemini,gopher,html},.plan}')
.map{|f| {
filepath: f,
user: File.basename(File.dirname(f)),
mtime: File.mtime(f),
protocol: PROTOCOLS[File.basename(f)],
}}
.reject{|h|
if File.world_readable? h[:filepath] then
Dir.empty? h[:filepath] || Dir.glob(h[:filepath]).all?{|f|
f =~ /(\.sample|public_(gemini,gopher))$/
}
else
true
end
}
.filter{|h| filters[:protocol].any?{|p| p == h[:protocol]}}
.sort_by{|h| h[:mtime]}
.reverse
.first(filters[:size])
.map{|h|
user = h[:user]
protocol = h[:protocol]
date = h[:mtime].strftime '%Y-%m-%d'
type = '1'
prefix = '/'
suffix = ''
unless protocol.eql? 'gopher' then
type = 'h'
prefix.prepend "URL:#{protocol}://tilde.pink"
end
unless protocol.eql? 'finger' then
prefix << '~'
suffix << '/'
end
desc = "#{date} #{user} {#{protocol}}"
path = "#{prefix}#{user}#{suffix}"
"[#{type}|#{desc}|#{path}|server|port]"
}
print ''\
"┏━┃┃ ┏━┃┏━ ┏━┛━┏┛ ┏━┃┛┏━ ┃ ┃\n"\
"┏━┛┃ ┏━┃┃ ┃┏━┛ ┃ ┏━┛┃┃ ┃┏┛\n"\
"┛ ━━┛┛ ┛┛ ┛━━┛ ┛ ┛ ┛┛ ┛┛ ┛\n"\
"a tilde.pink aggregator █▉▊▋▌▍▎▏\n\n"\
"showing #{results.length} results from #{filters[:protocol].join ', '}\n\n"\
"[7|change search filters|/~znqv/planet.dcgi|server|port]\n"\
"example: size=100&protocol=gemini&protocol=gopher\n"\
"[1|clear search filters|/~znqv/planet.dcgi|server|port]\n\n"\
"#{results.join "\n"}"