data_output_agent.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. module Agents
  2. class DataOutputAgent < Agent
  3. cannot_be_scheduled!
  4. description do
  5. <<-MD
  6. The Data Output Agent outputs received events as either RSS or JSON. Use it to output a public or private stream of Huginn data.
  7. This Agent will output data at:
  8. `https://#{ENV['DOMAIN']}#{Rails.application.routes.url_helpers.web_requests_path(agent_id: ':id', user_id: user_id, secret: ':secret', format: :xml)}`
  9. where `:secret` is one of the allowed secrets specified in your options and the extension can be `xml` or `json`.
  10. You can setup multiple secrets so that you can individually authorize external systems to
  11. access your Huginn data.
  12. Options:
  13. * `secrets` - An array of tokens that the requestor must provide for light-weight authentication.
  14. * `expected_receive_period_in_days` - How often you expect data to be received by this Agent from other Agents.
  15. * `template` - A JSON object representing a mapping between item output keys and incoming event values. Use [Liquid](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) to format the values. Values of the `link`, `title`, `description` and `icon` keys will be put into the \\<channel\\> section of RSS output. The `item` key will be repeated for every Event. The `pubDate` key for each item will have the creation time of the Event unless given.
  16. * `events_to_show` - The number of events to output in RSS or JSON. (default: `40`)
  17. * `ttl` - A value for the \\<ttl\\> element in RSS output. (default: `60`)
  18. If you'd like to output RSS tags with attributes, such as `enclosure`, use something like the following in your `template`:
  19. "enclosure": {
  20. "_attributes": {
  21. "url": "{{media_url}}",
  22. "length": "1234456789",
  23. "type": "audio/mpeg"
  24. }
  25. },
  26. "another_tag": {
  27. "_attributes": {
  28. "key": "value",
  29. "another_key": "another_value"
  30. },
  31. "_contents": "tag contents (can be an object for nesting)"
  32. }
  33. # Ordering events in the output
  34. #{description_events_order('events in the output')}
  35. # Liquid Templating
  36. In Liquid templating, the following variable is available:
  37. * `events`: An array of events being output, sorted in the given order, up to `events_to_show` in number. For example, if source events contain a site title in the `site_title` key, you can refer to it in `template.title` by putting `{{events.first.site_title}}`.
  38. MD
  39. end
  40. def default_options
  41. {
  42. "secrets" => ["a-secret-key"],
  43. "expected_receive_period_in_days" => 2,
  44. "template" => {
  45. "title" => "XKCD comics as a feed",
  46. "description" => "This is a feed of recent XKCD comics, generated by Huginn",
  47. "item" => {
  48. "title" => "{{title}}",
  49. "description" => "Secret hovertext: {{hovertext}}",
  50. "link" => "{{url}}"
  51. }
  52. }
  53. }
  54. end
  55. def working?
  56. last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  57. end
  58. def validate_options
  59. if options['secrets'].is_a?(Array) && options['secrets'].length > 0
  60. options['secrets'].each do |secret|
  61. case secret
  62. when %r{[/.]}
  63. errors.add(:base, "secret may not contain a slash or dot")
  64. when String
  65. else
  66. errors.add(:base, "secret must be a string")
  67. end
  68. end
  69. else
  70. errors.add(:base, "Please specify one or more secrets for 'authenticating' incoming feed requests")
  71. end
  72. unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
  73. errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
  74. end
  75. unless options['template'].present? && options['template']['item'].present? && options['template']['item'].is_a?(Hash)
  76. errors.add(:base, "Please provide template and template.item")
  77. end
  78. end
  79. def events_to_show
  80. (interpolated['events_to_show'].presence || 40).to_i
  81. end
  82. def feed_ttl
  83. (interpolated['ttl'].presence || 60).to_i
  84. end
  85. def feed_title
  86. interpolated['template']['title'].presence || "#{name} Event Feed"
  87. end
  88. def feed_link
  89. interpolated['template']['link'].presence || "https://#{ENV['DOMAIN']}"
  90. end
  91. def feed_url(options = {})
  92. feed_link + Rails.application.routes.url_helpers.
  93. web_requests_path(agent_id: id || ':id',
  94. user_id: user_id,
  95. secret: options[:secret],
  96. format: options[:format])
  97. end
  98. def feed_icon
  99. interpolated['template']['icon'].presence || feed_link + '/favicon.ico'
  100. end
  101. def feed_description
  102. interpolated['template']['description'].presence || "A feed of Events received by the '#{name}' Huginn Agent"
  103. end
  104. def receive_web_request(params, method, format)
  105. unless interpolated['secrets'].include?(params['secret'])
  106. if format =~ /json/
  107. return [{ error: "Not Authorized" }, 401]
  108. else
  109. return ["Not Authorized", 401]
  110. end
  111. end
  112. source_events = sort_events(received_events.order(id: :desc).limit(events_to_show).to_a)
  113. interpolation_context.stack do
  114. interpolation_context['events'] = source_events
  115. items = source_events.map do |event|
  116. interpolated = interpolate_options(options['template']['item'], event)
  117. interpolated['guid'] = {'_attributes' => {'isPermaLink' => 'false'},
  118. '_contents' => interpolated['guid'].presence || event.id}
  119. date_string = interpolated['pubDate'].to_s
  120. date =
  121. begin
  122. Time.zone.parse(date_string) # may return nil
  123. rescue => e
  124. error "Error parsing a \"pubDate\" value \"#{date_string}\": #{e.message}"
  125. nil
  126. end || event.created_at
  127. interpolated['pubDate'] = date.rfc2822.to_s
  128. interpolated
  129. end
  130. if format =~ /json/
  131. content = {
  132. 'title' => feed_title,
  133. 'description' => feed_description,
  134. 'pubDate' => Time.now,
  135. 'items' => simplify_item_for_json(items)
  136. }
  137. return [content, 200]
  138. else
  139. content = Utils.unindent(<<-XML)
  140. <?xml version="1.0" encoding="UTF-8" ?>
  141. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  142. <channel>
  143. <atom:link href=#{feed_url(secret: params['secret'], format: :xml).encode(xml: :attr)} rel="self" type="application/rss+xml" />
  144. <atom:icon>#{feed_icon.encode(xml: :text)}</atom:icon>
  145. <title>#{feed_title.encode(xml: :text)}</title>
  146. <description>#{feed_description.encode(xml: :text)}</description>
  147. <link>#{feed_link.encode(xml: :text)}</link>
  148. <lastBuildDate>#{Time.now.rfc2822.to_s.encode(xml: :text)}</lastBuildDate>
  149. <pubDate>#{Time.now.rfc2822.to_s.encode(xml: :text)}</pubDate>
  150. <ttl>#{feed_ttl}</ttl>
  151. XML
  152. content += simplify_item_for_xml(items).to_xml(skip_types: true, root: "items", skip_instruct: true, indent: 1).gsub(/^<\/?items>/, '').strip
  153. content += Utils.unindent(<<-XML)
  154. </channel>
  155. </rss>
  156. XML
  157. return [content, 200, 'text/xml']
  158. end
  159. end
  160. end
  161. private
  162. class XMLNode
  163. def initialize(tag_name, attributes, contents)
  164. @tag_name, @attributes, @contents = tag_name, attributes, contents
  165. end
  166. def to_xml(options)
  167. if @contents.is_a?(Hash)
  168. options[:builder].tag! @tag_name, @attributes do
  169. @contents.each { |key, value| ActiveSupport::XmlMini.to_tag(key, value, options.merge(skip_instruct: true)) }
  170. end
  171. else
  172. options[:builder].tag! @tag_name, @attributes, @contents
  173. end
  174. end
  175. end
  176. def simplify_item_for_xml(item)
  177. if item.is_a?(Hash)
  178. item.each.with_object({}) do |(key, value), memo|
  179. if value.is_a?(Hash)
  180. if value.key?('_attributes') || value.key?('_contents')
  181. memo[key] = XMLNode.new(key, value['_attributes'], simplify_item_for_xml(value['_contents']))
  182. else
  183. memo[key] = simplify_item_for_xml(value)
  184. end
  185. else
  186. memo[key] = value
  187. end
  188. end
  189. elsif item.is_a?(Array)
  190. item.map { |value| simplify_item_for_xml(value) }
  191. else
  192. item
  193. end
  194. end
  195. def simplify_item_for_json(item)
  196. if item.is_a?(Hash)
  197. item.each.with_object({}) do |(key, value), memo|
  198. if value.is_a?(Hash)
  199. if value.key?('_attributes') || value.key?('_contents')
  200. contents = if value['_contents'] && value['_contents'].is_a?(Hash)
  201. simplify_item_for_json(value['_contents'])
  202. elsif value['_contents']
  203. { "contents" => value['_contents'] }
  204. else
  205. {}
  206. end
  207. memo[key] = contents.merge(value['_attributes'] || {})
  208. else
  209. memo[key] = simplify_item_for_json(value)
  210. end
  211. else
  212. memo[key] = value
  213. end
  214. end
  215. elsif item.is_a?(Array)
  216. item.map { |value| simplify_item_for_json(value) }
  217. else
  218. item
  219. end
  220. end
  221. end
  222. end