Skip to content

Instantly share code, notes, and snippets.

@marshall-lee
Last active August 29, 2015 14:20
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 marshall-lee/34774bb43a46f496d27a to your computer and use it in GitHub Desktop.
Save marshall-lee/34774bb43a46f496d27a to your computer and use it in GitHub Desktop.
Download GIFs from 9gag
#!/usr/bin/env ruby
require 'open-uri'
url = ARGV[0]
$stderr.puts 'URL not specified' and exit 1 unless url
uri = URI.parse url
$stderr.puts 'This should be 9gag.com URL' and exit 1 if uri.host != '9gag.com'
m = uri.path.match %r{^/gag/(\w+)$}
$stderr.puts 'Malformed URL!' and exit 1 unless m
id = m[1]
body = uri.read
m = body.match %r{data-image="(https?://.+#{id}.+\.gif)"}
gif_url = m[1]
gif_filename = "#{id}.gif"
File.binwrite(gif_filename, URI.parse(gif_url).read)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment