Skip to content

Instantly share code, notes, and snippets.

@walfie
Created August 26, 2015 01:45
Show Gist options
  • Select an option

  • Save walfie/be39d40bfe3089add737 to your computer and use it in GitHub Desktop.

Select an option

Save walfie/be39d40bfe3089add737 to your computer and use it in GitHub Desktop.
Get random tumblr post, set it as wallpaper
# This is pretty bad code. Don't use it expecting it to work well
require 'net/http'
require 'uri'
require 'nokogiri'
# copied and pasted from:
# http://ruby-doc.org/stdlib-2.2.3/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Following+Redirection
def fetch(uri_str, limit = 2)
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
escaped_uri = URI.escape(uri_str).sub('%23_=_', '')
`echo #{escaped_uri} > /tmp/wall_src.txt` # DONT DO THIS
url = URI.parse(escaped_uri)
req = Net::HTTP::Get.new(url.path)
response = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch(response['location'], limit - 1)
else
response.error!
end
end
url = 'http://uychizu.tumblr.com/random' # Change this to whatever random URL
body = fetch(url).body
html = Nokogiri::HTML(body)
src = html.at_css('.photo-wrapper-inner').at_css('img').attributes['src'].content
puts src
# yeah let's use shell because I don't feel like effort
img_path = '/tmp/wall.jpg'
`curl #{src} > #{img_path}`
# Set that xfce wallpaper. This is very specific to my computer.
`xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitorDVI-0/workspace0/last-image --set #{img_path}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment