summaryrefslogtreecommitdiff
path: root/pictures.rb
blob: e21cb9baf6437dd294ac45d1c91eeae3d6f7b410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env ruby
require 'json'
require_relative 'lib'

PICTURE_URL = 'https://media.pdp8.info/pictures'
PICTURE_PATH = '/srv/media/pictures'

html = File.read(File.join(SNIPPETS, 'head.html'))
html += nav 'pictures'

posts = Dir[File.join('/srv', 'social', 'outbox', 'create', '*.json')].collect { |f| JSON.load_file(f) }.select do |a|
  a['to'].include?('https://www.w3.org/ns/activitystreams#Public') and a['object']['attachment']
end.collect { |a| a['object'] }.select { |o| o['attachment'].size == 1 and o['attachment'][0]['mediaType'].match(/image/) }

posts.sort_by { |o| o['published'] }.reverse.each do |object|
  att = object['attachment'][0]
  url = att['url']
  basename = File.basename(url, File.extname(url))
  next if basename == 'cover'

  www_path = File.join(PICTURE_PATH, 'www', basename + '.webp')
  www_url = File.join(PICTURE_URL, 'www', basename + '.webp')
  alt = att['name'] ? att['name'].gsub("'", ''').gsub('"', '"') : ''
  src_url = File.join(PICTURE_URL, basename + File.extname(url))
  w, h = `/etc/profiles/per-user/ch/bin/identify -format "%w %h" #{www_path}`.chomp.split(' ')
  html += "<div class='post'>"
  html += "<a href='#{src_url}'><img loading='lazy' width='#{w}' height='#{h}' "
  html += "alt='#{alt}' src='#{www_url}'></a>"
  html += '</div>'
end

html += File.read(File.join(SNIPPETS, 'tail.html'))
print_html 'pictures', html