# gem install twitter | |
require 'twitter' | |
# https://apps.twitter.com/ | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = "...." | |
config.consumer_secret = "...." | |
end | |
def collect_with_max_id(collection=[], max_id=nil, &block) | |
response = yield(max_id) | |
collection += response | |
response.empty? ? collection.flatten : collect_with_max_id(collection, response.last.id - 1, &block) | |
end | |
def client.get_all_tweets(user) | |
collect_with_max_id do |max_id| | |
options = {count: 200, include_rts: true} | |
options[:max_id] = max_id unless max_id.nil? | |
user_timeline(user, options) | |
end | |
end | |
all_tweets = client.get_all_tweets("ihower") | |
stop_at = "2017-09" | |
File.open("tweets.md", 'w') do |f| | |
current_ym = "#{all_tweets[0].created_at.year}-#{all_tweets[0].created_at.month}" | |
f << "<h2>#{all_tweets[0].created_at.year}/#{all_tweets[0].created_at.month}</h2>\n" | |
f << "<ul>\n" | |
all_tweets.each do |tweet| | |
this_ym = "#{tweet.created_at.year}-#{tweet.created_at.month}" | |
if this_ym == stop_at | |
break | |
end | |
if current_ym != this_ym | |
f << "</ul>\n" | |
f << "<h2>#{tweet.created_at.year}/#{tweet.created_at.month}</h2>\n" | |
f << "<ul>\n" | |
end | |
current_ym = this_ym | |
f << "<li><a href=\"#{tweet.url}\">#{tweet.created_at.getlocal.to_s[0,16]}</a> #{tweet.full_text}</li>\n" | |
end | |
f << "</ul>\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment