Skip to content

Instantly share code, notes, and snippets.

@jpablobr
Last active December 10, 2015 17:08
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 jpablobr/4465778 to your computer and use it in GitHub Desktop.
Save jpablobr/4465778 to your computer and use it in GitHub Desktop.
This script will parse all @gifs tweets searching for gifs urls and download them.
#!/usr/bin/env ruby
# This script will parse all @gifs tweets searching for gifs urls and
# download them.
# Initial disclaimer!
# This mess is gonna get raw, like sushi. So haters to the left.
# Usage:
# $ git clone https://gist.github.com/4465778.git gifs
# $ cd gifs
# $ ruby ./gifs.rb
# or
# $ curl -L https://gist.github.com/raw/4465778/1ff62de0af65a7eac7af2f665e6ebced8eff2627/gifs.rb | ruby
require 'json'
require 'open-uri'
class Gif
attr_reader :directory, :user
def initialize
@user = get_user_info
@directory = "twitter-gifs"
end
def download!
set_download_directory
if user && save_gifs_urls!
Dir.chdir directory
puts "Current directory: #{Dir.pwd}"
File.open("gifs.txt", 'r') do |gifs|
gifs.each_line do |gif|
download_gif gif
end
end
end
end
private
def save_gifs_urls!
statuses_count = user['statuses_count'].to_i
# https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
number_of_pages = ([3200, statuses_count].min/200).ceil
puts "@gifs has #{statuses_count} status updates."
File.open("#{directory}/gifs.txt", 'w') do |user_tweets|
(1..number_of_pages).each do |page_number|
tweets = open "#{statuses_url}&page=#{page_number}"
puts "Fetching page #{page_number}"
if tweets.status.include? '200'
get_gifs_urls(tweets).each do |gif|
user_tweets.write("#{Net::HTTP.get_response(URI(gif))['location']}\n")
end
else
puts "Failed. Response code: #{tweets.status.to_s}"
end
end
end
end
def download_gif url
gif_filename = url.tr("\n",'').gsub(/^.*\//,'')
if File.exists? gif_filename
puts "#{gif_filename} already downloaded."
else
puts "Downloading: #{gif_filename}"
open(url) do |gif|
File.open(gif_filename,"wb") { |f| f.puts gif.read }
end
end
end
def set_download_directory
if File.exists? directory
puts "Using #{directory} directory for gifs downloads."
else
puts "Creating #{directory} directory for gifs downloads."
Dir.mkdir directory
end
end
def get_user_info
user_json = open "http://api.twitter.com/1/users/show.json?screen_name=gifs"
if user_json.status.include? '200'
JSON.parse user_json.read
else
puts "Error: Response code is: #{user_json.status.to_s}"
false
end
end
def statuses_url
"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=gifs&trim_user=true&count=200&include_retweets=true&include_entities=true"
end
def get_gifs_urls tweets
# http://24.media.tumblr.com/tumblr_lx3cwnqmaW1r154eho1_500.gif
tweets_txt = JSON.parse(tweets.read).map { |twt| twt['text'] }
tweets_txt.inject([]) do |twts, twt|
twts << twt.match(/http:\/\/t\.co\/.*/)[0] if twt.match(/http:\/\/t\.co\/.*/);twts
end
end
end
Gif.new.download!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment