Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Last active August 29, 2015 13:56
Show Gist options
  • Save dmerrick/9261730 to your computer and use it in GitHub Desktop.
Save dmerrick/9261730 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'open-uri'
require 'slack-notifier'
# use the turtle subreddit
turtles = "http://www.reddit.com/r/turtle.json"
json = JSON.load(open(turtles))
# select all imgur links that are not self posts
image_links = json["data"]["children"]
image_links.reject! { |post| post["data"]["is_self"] }
image_links.select! { |post| post["data"]["url"] =~ /imgur/ }
# select a random post
link = image_links.sample["data"]["url"]
# add .jpg to the end if needed
if File.basename(URI.parse(link).path) !~ /\.(gif|jpg|png)/
link += '.jpg'
end
# post to slack
notifier = Slack::Notifier.new "zencoder", ARGV.shift
status = notifier.ping link, channel: ARGV.shift || '#random'
# print the status
puts "#{status}: #{link}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment