Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lambda2
Last active June 23, 2016 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lambda2/4225a1650a864f25710f78f242721f39 to your computer and use it in GitHub Desktop.
Save lambda2/4225a1650a864f25710f78f242721f39 to your computer and use it in GitHub Desktop.
Redirect all bonjour* tumblr posts to a slack channel
require "tumblr_client"
require 'slack-ruby-client'
POST_DELAY = 30.minutes
SLACK_CHANNEL = '#nsfw'
# bonjourmademoiselle.fr dites.bonjourmadame.fr bonjourlesfesses.tumblr.com
TUMBLR_SOURCE = 'bonjourmademoiselle.fr'
PAGE_LENGTH = 20
the_end = false
offset = 0
Slack.configure do |config|
config.token = ENV['SLACK_API_TOKEN']
end
# Authenticate via API Key
client = Tumblr::Client.new :consumer_key => ENV['TUMBLR_API_TOKEN']
# On s'auth sur slack
slack_client = Slack::Web::Client.new
slack_client.auth_test
# On fetch tous les posts
while !the_end do
posts = client.posts TUMBLR_SOURCE, offset: offset
images = posts["posts"].map{|p| p["photos"].map{|e| [p["summary"], e["original_size"]["url"]]}.to_h }.flatten
offset += PAGE_LENGTH
the_end = true if posts["posts"].count < PAGE_LENGTH
# Et on balance une image toutes les {POST_DELAY} 👌
images.flatten.map{|e| [e.keys.first, e.values]}.to_h.each do |caption, urls|
slack_client.chat_postMessage(channel: SLACK_CHANNEL, text: "#{caption}: #{urls.join(', ')}", as_user: true)
sleep(POST_DELAY)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment