1
0

tumblr_publish_agent.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. require "tumblr_client"
  2. module Agents
  3. class TumblrPublishAgent < Agent
  4. include TumblrConcern
  5. cannot_be_scheduled!
  6. description <<-MD
  7. #{'## Include `tumblr_client` and `omniauth-tumblr` in your Gemfile to use this Agent!' if dependencies_missing?}
  8. The TumblrPublishAgent publishes Tumblr posts from the events it receives.
  9. To be able to use this Agent you need to authenticate with Tumblr in the [Services](/services) section first.
  10. **Required fields:**
  11. `blog_name` Your Tumblr URL (e.g. "mustardhamsters.tumblr.com")
  12. `post_type` One of [text, photo, quote, link, chat, audio, video]
  13. -------------
  14. You may leave any of the following optional fields blank. Including a field not allowed for the specified `post_type` will cause a failure.
  15. **Any post type**
  16. * `state` published, draft, queue, private
  17. * `tags` Comma-separated tags for this post
  18. * `tweet` off, text for tweet
  19. * `date` GMT date and time of the post as a string
  20. * `format` html, markdown
  21. * `slug` short text summary at end of the post URL
  22. **Text** `title` `body`
  23. **Photo** `caption` `link` `source`
  24. **Quote** `quote` `source`
  25. **Link** `title` `url` `description`
  26. **Chat** `title` `conversation`
  27. **Audio** `caption` `external_url`
  28. **Video** `caption` `embed`
  29. -------------
  30. [Full information on field options](https://www.tumblr.com/docs/en/api/v2#posting)
  31. Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
  32. MD
  33. gem_dependency_check { defined?(Tumblr) }
  34. def validate_options
  35. errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  36. end
  37. def working?
  38. event_created_within?(interpolated['expected_update_period_in_days']) && most_recent_event && most_recent_event.payload['success'] == true && !recent_error_logs?
  39. end
  40. def default_options
  41. {
  42. 'expected_update_period_in_days' => "10",
  43. 'blog_name' => "{{blog_name}}",
  44. 'post_type' => "{{post_type}}",
  45. 'options' => {
  46. 'state' => "{{state}}",
  47. 'tags' => "{{tags}}",
  48. 'tweet' => "{{tweet}}",
  49. 'date' => "{{date}}",
  50. 'format' => "{{format}}",
  51. 'slug' => "{{slug}}",
  52. 'title' => "{{title}}",
  53. 'body' => "{{body}}",
  54. 'caption' => "{{caption}}",
  55. 'link' => "{{link}}",
  56. 'source' => "{{source}}",
  57. 'quote' => "{{quote}}",
  58. 'url' => "{{url}}",
  59. 'description' => "{{description}}",
  60. 'conversation' => "{{conversation}}",
  61. 'external_url' => "{{external_url}}",
  62. 'embed' => "{{embed}}",
  63. },
  64. }
  65. end
  66. def receive(incoming_events)
  67. # if there are too many, dump a bunch to avoid getting rate limited
  68. if incoming_events.count > 20
  69. incoming_events = incoming_events.first(20)
  70. end
  71. incoming_events.each do |event|
  72. blog_name = interpolated(event)['blog_name']
  73. post_type = interpolated(event)['post_type']
  74. options = interpolated(event)['options']
  75. begin
  76. post = publish_post(blog_name, post_type, options)
  77. create_event :payload => {
  78. 'success' => true,
  79. 'published_post' => "["+blog_name+"] "+post_type,
  80. 'post_id' => post["id"],
  81. 'agent_id' => event.agent_id,
  82. 'event_id' => event.id
  83. }
  84. end
  85. end
  86. end
  87. def publish_post(blog_name, post_type, options)
  88. options_obj = {
  89. :state => options['state'],
  90. :tags => options['tags'],
  91. :tweet => options['tweet'],
  92. :date => options['date'],
  93. :format => options['format'],
  94. :slug => options['slug'],
  95. }
  96. case post_type
  97. when "text"
  98. options_obj[:title] = options['title']
  99. options_obj[:body] = options['body']
  100. tumblr.text(blog_name, options_obj)
  101. when "photo"
  102. options_obj[:caption] = options['caption']
  103. options_obj[:link] = options['link']
  104. options_obj[:source] = options['source']
  105. tumblr.photo(blog_name, options_obj)
  106. when "quote"
  107. options_obj[:quote] = options['quote']
  108. options_obj[:source] = options['source']
  109. tumblr.quote(blog_name, options_obj)
  110. when "link"
  111. options_obj[:title] = options['title']
  112. options_obj[:url] = options['url']
  113. options_obj[:description] = options['description']
  114. tumblr.link(blog_name, options_obj)
  115. when "chat"
  116. options_obj[:title] = options['title']
  117. options_obj[:conversation] = options['conversation']
  118. tumblr.chat(blog_name, options_obj)
  119. when "audio"
  120. options_obj[:caption] = options['caption']
  121. options_obj[:external_url] = options['external_url']
  122. tumblr.audio(blog_name, options_obj)
  123. when "video"
  124. options_obj[:caption] = options['caption']
  125. options_obj[:embed] = options['embed']
  126. tumblr.video(blog_name, options_obj)
  127. end
  128. end
  129. end
  130. end