Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Last active December 14, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhawthorn/5072567 to your computer and use it in GitHub Desktop.
Save jhawthorn/5072567 to your computer and use it in GitHub Desktop.
Set the latest tweet from @Cmdr_Hadfield as desktop background
require 'json'
require 'open-uri'
TIMELINE_URL = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Cmdr_Hadfield&include_entities=true&trim_user=true&exclude_replies=true'
class Tweet < Struct.new(:tweet)
def media
tweet['entities']['media'][0]
end
def image_url
media["media_url"] + ':orig'
end
def text
tweet['text'].gsub(media['url'], '').strip
end
def write_image filename
system %(convert '#{esc image_url}' -fill white -undercolor '#00000080' -gravity South -annotate +0+5 '#{esc text}' '#{esc filename}')
end
private
def esc text
text.gsub("'", "''")
end
end
json = open(TIMELINE_URL).read
data = JSON.parse(json)
tweet = data.detect{|x| x['entities'] && x['entities']['media'] }
tweet = Tweet.new(tweet)
tweet.write_image '/tmp/bg.jpg'
system 'feh --bg-scale /tmp/bg.jpg'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment