Class: Ebooks::Archiver
- Inherits:
-
Object
- Object
- Ebooks::Archiver
- Defined in:
- lib/twitter_ebooks/archiver.rb
Instance Method Summary collapse
- #fetch_tweets ⇒ Object
-
#initialize(username, outpath) ⇒ Archiver
constructor
A new instance of Archiver.
-
#read_corpus ⇒ Object
Read exiting corpus into memory.
-
#tweets_since(since_id) ⇒ Object
Retrieve all available tweets for a given user since the last tweet id.
Constructor Details
#initialize(username, outpath) ⇒ Archiver
Returns a new instance of Archiver.
8 9 10 11 12 |
# File 'lib/twitter_ebooks/archiver.rb', line 8 def initialize(username, outpath) @username = username @outpath = outpath @client = Twitter::Client.new end |
Instance Method Details
#fetch_tweets ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/twitter_ebooks/archiver.rb', line 58 def fetch_tweets lines, since_id = read_corpus if since_id.nil? puts "Retrieving tweets from @#{@username}" else puts "Retrieving tweets from @#{@username} since #{since_id}" end tweets = tweets_since(since_id) if tweets.length == 0 puts "No new tweets" return end new_lines = tweets.map { |tweet| tweet.text.gsub("\n", " ") } new_since_id = tweets[0].id.to_s lines = ["# " + new_since_id] + new_lines + lines corpus = File.open(@outpath, 'w') corpus.write(lines.join("\n")) corpus.close end |
#read_corpus ⇒ Object
Read exiting corpus into memory. Return list of tweet lines and the last tweet id.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/twitter_ebooks/archiver.rb', line 16 def read_corpus lines = [] since_id = nil if File.exists?(@outpath) lines = File.read(@outpath).split("\n") if lines[0].start_with?('#') since_id = lines[0].split('# ').last end end [lines, since_id] end |
#tweets_since(since_id) ⇒ Object
Retrieve all available tweets for a given user since the last tweet id
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/twitter_ebooks/archiver.rb', line 31 def tweets_since(since_id) page = 1 retries = 0 tweets = [] max_id = nil opts = { count: 200, include_rts: false, trim_user: true } opts[:since_id] = since_id unless since_id.nil? loop do opts[:max_id] = max_id unless max_id.nil? new = @client.user_timeline(@username, opts) break if new.length <= 1 puts "Received #{new.length} tweets" tweets += new max_id = new.last.id break end tweets end |