Skip to content

Instantly share code, notes, and snippets.

@Ishotihadus
Created January 7, 2018 14:53
Show Gist options
  • Save Ishotihadus/33f2ff63803d0e6b23a7f0199b6cf3e5 to your computer and use it in GitHub Desktop.
Save Ishotihadus/33f2ff63803d0e6b23a7f0199b6cf3e5 to your computer and use it in GitHub Desktop.
特定の絵師が Twitter にのせた画像を全部ダウンロードするやつ
require 'twitter'
require 'net/http'
TWITTER_CK = ''
TWITTER_CS = ''
TWITTER_AT = ''
TWITTER_AS = ''
USER = 'tktiau'
Dir.mkdir(USER) unless Dir.exists?(USER)
client = Twitter::REST::Client.new do |config|
config.consumer_key = TWITTER_CK
config.consumer_secret = TWITTER_CS
config.access_token = TWITTER_AT
config.access_token_secret = TWITTER_AS
end
last_id = nil
loop do
list = last_id ? client.user_timeline(USER, :count => 200, :max_id => last_id - 1) : client.user_timeline(USER, :count => 200)
break if list.count == 0
last_id = list.last.id
list.select{|e| e.media?}.each do |status|
status.media.select{|e| e.type == 'photo'}.each do |media|
filename = USER + '/' + status.id.to_s + '-' + media.media_uri_https.to_s.match(/[^\/]*$/)[0]
unless File.exists?(filename)
puts media.media_uri_https
File.binwrite(filename, Net::HTTP.get_response(URI.parse(media.media_uri_https.to_s + ':orig')).body)
File::utime(status.created_at, status.created_at, filename)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment