Skip to content

Instantly share code, notes, and snippets.

@supermomonga
Created October 10, 2013 07:45
Show Gist options
  • Save supermomonga/6914570 to your computer and use it in GitHub Desktop.
Save supermomonga/6914570 to your computer and use it in GitHub Desktop.
# coding: utf-8
require 'mechanize'
require 'nokogiri'
require 'json'
m = Mechanize.new
m.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36'
unless ARGV[0]
puts "You need to give a twitter id."
exit
end
id = ARGV[0]
`mkdir ./img_#{ id }`
# init_url = "https://twitter.com/#{ id }/media"
# m.get init_url
media_url = "https://twitter.com/i/profiles/show/#{ id }/media_timeline?include_available_features=1&include_entities=1&last_note_ts=0"
m.get media_url
json = JSON.parse m.page.body
max_id = 0
loop do
html = Nokogiri::HTML json['items_html']
html.css('span.media-thumbnail').map do |e|
tweet_id = e['data-status-id']
file_url = e['data-resolved-url-large']
file_name = file_url.sub('https://pbs.twimg.com/media/','').sub(':large','')
file_path = "./img_#{ id }/#{ tweet_id }_#{ file_name }"
puts "tweet_id: #{tweet_id}"
if File.exists? file_path
puts "already exists : #{ file_path }"
else
puts "downloading : #{ file_path }"
`wget #{ e['data-resolved-url-large'] } -o - -O #{ file_path }`
puts "done"
sleep 5
end
max_id = tweet_id
puts ""
end
if json['has_more_items']
sleep 2
m.get "https://twitter.com/i/profiles/show/#{ id }/media_timeline?include_available_features=1&include_entities=1&last_note_ts=0&max_id=#{ max_id }&oldest_unread_id=0"
json = JSON.parse m.page.body
else
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment