-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.rb
81 lines (67 loc) · 2.1 KB
/
generator.rb
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require './strip_heredoc'
class Generator
def initialize(actor_url)
@actor_url = actor_url
end
def call
actor = get(actor_url)
outbox_url = actor[:outbox]
outbox = get(outbox_url)
first_page = outbox[:first]
items = get_items(first_page)
# TODO: More items?
JSON.pretty_generate \
version: 'https://jsonfeed.org/version/1',
title: actor[:name],
description: actor[:summary],
home_page_url: actor[:url],
feed_url: "http://mastodon-feed-converter.johnholdun.com/feed.json?source=#{CGI.escape(actor_url)}",
icon: actor.dig(:icon, :url),
items: items,
author: {
name: actor[:name],
avatar: actor.dig(:icon, :url)
}
end
def self.call(*args)
new(*args).call
end
private
attr_reader :actor_url
# TODO: Cache each response, add etag/if-modified support
def get(url)
response = open(url, 'Accept' => 'application/activity+json').read
JSON.parse(response, symbolize_names: true)
end
def get_items(url)
get(url)[:orderedItems].map do |item|
object = item[:object]
object = get(object) if object.is_a?(String)
json_entry =
{
id: item[:id],
content_html: object[:content],
url: object[:id],
date_published: item[:published]
}
(object[:attachment] || []).each do |attachment|
json_entry[:content_html] +=
if %w(image/jpeg image/jpg image/png image/gif image/webp).include?(attachment[:mediaType])
%Q(<p><img alt=#{attachment[:name].to_s.inspect} src="#{attachment[:url]}"></p>)
else
%Q(<p>Unexpected attachment: <code>#{JSON.pretty_generate(attachment)}</code></p>)
end
end
if object[:summary]
json_entry[:title] = object[:summary]
end
if item[:type] == 'Announce'
json_entry[:content_html] = strip_heredoc(<<-SHARE).strip
<p><a href="#{object[:attributedTo]}">#{object[:attributedTo]}</a>:</p>
<blockquote>#{json_entry[:content_html]}</blockquote>
SHARE
end
json_entry
end
end
end