Last active
June 23, 2016 09:18
-
-
Save lambda2/4225a1650a864f25710f78f242721f39 to your computer and use it in GitHub Desktop.
Redirect all bonjour* tumblr posts to a slack channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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