Last active
August 29, 2015 14:20
-
-
Save marshall-lee/34774bb43a46f496d27a to your computer and use it in GitHub Desktop.
Download GIFs from 9gag
This file contains 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
#!/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