Class: TwitterBackup::Tweet

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/twitter_backup/tweet.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.download(args = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/twitter_backup/tweet.rb', line 53

def download(args={})
  args = {
    :from    => nil,
    :to    => nil
  }.merge(args)
  args[:from], args[:to] = [args[:from], args[:to]].sort.reverse unless [args[:from], args[:to]].include? nil

  needed_tweets = []

  if args[:from].blank?
    say ".... user_timeline API request" if TBConfig.passed_opts.verbose?
    received_tweets = Twitter.user_timeline(:count => 200)
  else
    say ".... user_timeline API request" if TBConfig.passed_opts.verbose?
    received_tweets = Twitter.user_timeline(:count => 200, :max_id => args[:from])
  end

  if args[:to].blank?
    id_of_the_earliest_received_tweet = nil
    until id_of_the_earliest_received_tweet == received_tweets.last.id
      id_of_the_earliest_received_tweet = received_tweets.last.id
      say ".... user_timeline API request" if TBConfig.passed_opts.verbose?
      received_tweets.concat(
          Twitter.user_timeline(:count => 200, :max_id => received_tweets.last.id)
      )
    end
    received_tweets
  else args[:to]
    id_of_the_earliest_needed_tweet = args[:to]
    until received_tweets.map(&:id).include? id_of_the_earliest_needed_tweet
      say ".... user_timeline API request" if TBConfig.passed_opts.verbose?
      received_tweets.concat(
          Twitter.user_timeline(:count => 200, :max_id => received_tweets.last.id)
      )
    end
    index_of_earliest_needed_tweet = received_tweets.rindex{|tweet| tweet.id == id_of_the_earliest_needed_tweet}
    received_tweets[0..index_of_earliest_needed_tweet]
  end

end

.dump_to_backup_fileObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/twitter_backup/tweet.rb', line 98

def dump_to_backup_file
  if TBConfig.passed_opts.verbose?
    say ".... Saving tweets to: #{TBConfig.options[:backup_file]}"
  end
  tweets = slim.order("created_at DESC").map{ |tweet| {
                                    :id => tweet.status_id,
                                    :text => tweet.status,
                                    :created_at => tweet.created_at,
                                    :link => tweet.public_link } }
  File.open( TBConfig.options[:backup_file], "w" ) { |f| YAML::dump( tweets, f ) }
end

.earliestObject



49
50
51
# File 'lib/twitter_backup/tweet.rb', line 49

def earliest
  self.slim.order("created_at DESC").last
end

.latestObject



46
47
48
# File 'lib/twitter_backup/tweet.rb', line 46

def latest
  self.slim.order("created_at DESC").first
end

.synced?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/twitter_backup/tweet.rb', line 94

def synced?
  self.count == TBConfig.user.statuses_count
end

.update_tweets(args = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/twitter_backup/tweet.rb', line 10

def update_tweets(args={})
  args = {
    :from    => nil,
    :to    => latest.try(:status_id)
  }.merge(args)
  if args[:from] == :earliest
    args[:from] = earliest.try(:status_id)
    args[:to] = nil if args[:to] == latest.try(:status_id)
  end
  if !synced? || TBConfig.passed_opts.force?
    say ".... Updating tweets from #{args[:from] || 'the youngest one'} to #{args[:to] || 'the oldest one'}" if TBConfig.passed_opts.verbose?
    download(:from => args[:from], :to => args[:to]).each do |tweet|
      if absent_tweet = find_by_status_id(tweet.id)
        next
      else
        say %[<%= color(".", YELLOW) %> ] if TBConfig.passed_opts.verbose?
        tweet_text = if tweet.retweet?
          "RT @#{tweet.retweeted_status.user.screen_name}: #{tweet.retweeted_status.text}"
        else
          tweet.text
        end
        create(
          :status_id => tweet.id,
          :status => tweet_text,
          :created_at => tweet.created_at,
          :raw => tweet
        )
      end
    end
    say "!"
    say ".... Updating succeeded" if TBConfig.passed_opts.verbose?
  else
    say ".... Seems like there is nothing we can update" if TBConfig.passed_opts.verbose?
  end
end

Instance Method Details



111
112
113
# File 'lib/twitter_backup/tweet.rb', line 111

def public_link
  "https://twitter.com/#{TBConfig.user.screen_name}/status/#{self.status_id}"
end