This repository was archived by the owner on Mar 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitter.rb
More file actions
341 lines (282 loc) · 8.07 KB
/
twitter.rb
File metadata and controls
341 lines (282 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
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
45
46
47
48
49
50
51
52
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
require "rubygems"
require "bundler/setup"
require 'yaml'
require 'twitter'
require 'json'
require 'active_support'
require "tzinfo"
require "active_support/time_with_zone"
require "active_support/duration"
require "active_support/time"
module OnlineMemory
module Twitter
def self.erase_old_tweets(client, expiration=0, storage=nil)
eraser = OldTweetsEraser.new
eraser.client = client
eraser.storage = storage
return eraser.erase_older_than(expiration)
end
def self.backup_new_tweets(client, storage)
raise "TODO"
end
class OldTweetsEraser
attr_reader :client
attr_reader :storage
attr_writer :timeline
attr_accessor :ignore_missing
attr_accessor :wait_if_overloaded
def initialize
@ignore_missing = true
@wait_if_overloaded = false
end
def storage=(val)
if val.respond_to? :store or val.nil?
@storage = val
else
@storage = Storage.new( val )
end
end
def timeline
@timeline ||= Timeline.new( client )
end
def client=(val)
if val.respond_to? :verify_credentials
@client = val
else
@client = RateLimitedClient.new( val )
end
end
def erase_older_than(expiration)
expiration = DateTime.now - expiration.seconds unless expiration.respond_to? :acts_like_date? and expiration.acts_like_date?
processed = 0
$stdout.write (storage ? "Backup and delete " : "Delete ")
begin
timeline.each_older_than(expiration) do |tweet|
storage.store( tweet ) if storage
client.tweet_destroy( tweet.attrs[:id] )
$stdout.write "."
processed += 1
end
ensure
$stdout.write " \nProcessed " + processed.to_s + " tweet(s)\n"
end
return true
end
end
class RateLimitedClient
attr_accessor :max_retries
attr_accessor :wait_if_overloaded
attr_accessor :wait_if_clienterror
def initialize(*options)
@client = Client.new(*options)
@max_retries = 5
@wait_if_overloaded = 250
@wait_if_clienterror = 250
end
def method_missing(m, *args, &block)
rate_limit(m, *args, &block)
end
def respond_to?(m)
@client.respond_to?(m)
end
private
def manageable_error?(error)
case error
when ::Twitter::Error::TooManyRequests
{
cause: "too many requests" ,
wait: error.rate_limit.reset_in + 15
}
when ::Twitter::Error::ServiceUnavailable
{
cause: "twitter overloaded" ,
wait: @wait_if_overloaded
}
when ::Twitter::Error::ClientError
if error.to_s.match(/Connection reset by peer/)
{
cause: "connection reset by peer" ,
wait: @wait_if_clienterror ,
exec: Proc.new { self.reset_connection! }
}
elsif error.to_s.match(/Timeout::Error/)
{
cause: "twitter timeout",
wait: @wait_if_clienterror ,
exec: Proc.new { self.reset_connection! }
}
else
false
end
when ::Timeout::Error
else
false
end
end
def rate_limit(method, *params, &block)
tries = 0
begin
tries += 1
result = @client.__send__(method,*params, &block)
rescue => error
data = manageable_error?(error)
raise unless data
if tries > @max_retries or not data[:wait] or data[:wait] == 0
$stderr.write "(#{data[:cause]})\n"
raise
else
$stderr.write "(#{data[:cause]}, try #{tries}, sleeping #{data[:wait]})"
sleep (data[:wait])
data[:exec].call if data[:exec]
retry
end
end
result
end
end
class TweetArchiveFile
attr_accessor :file
attr_accessor :last_id_processed
def initialize(file, last_id)
@file = file
@last_id_processed = last_id
end
def each_tweet_id
to_reach = @last_id_processed
io = File.open(@file)
io.each_line do |line|
if /^status_id: (\d+)/.match(line)
id = $1
if to_reach
to_reach = false if id == to_reach
next
end
yield id
@last_id_processed = id
end
end
end
alias_method :each, :each_tweet_id
end
class Client < ::Twitter::Client
def screen_name
unless @screen_name
@screen_name = verify_credentials.attrs[:screen_name]
end
@screen_name
end
def reset_connection!
@connection = nil
end
end
class Storage
attr_accessor :directory
def initialize(directory)
@directory = directory
end
def store(tweet)
File.write path_from_tweet(tweet), content_from_tweet(tweet)
end
def stored?(tweet)
File.exists? path_from_tweet(tweet)
end
def remove(tweet)
File.delete path_from_tweet(tweet)
end
private
def content_from_tweet(tweet)
JSON.generate(tweet.attrs)
end
def path_from_tweet(tweet)
@directory + "/" + id_from_tweet(tweet) + ".json"
end
def id_from_tweet(tweet)
tweet.attrs[:id_str]
end
end
class Timeline
def initialize(client, screen_name=nil)
@twitter = client
@screen_name = screen_name || client.screen_name
end
def each(options={})
tweets = fetch_first_tweets(options)
while tweets and tweets.count > 0
tweets.each do |tweet|
yield tweet
end
tweets = fetch_next_tweets
end
end
def each_older_than(limit, options={})
each do |tweet|
date = DateTime.parse tweet.attrs[:created_at]
yield tweet if date < limit
end
end
private
def fetch_first_tweets(options={})
@end_of_tweets = false
fetch_in_timeline
end
def fetch_next_tweets(options = {})
return false if @end_of_tweets
options[:max_id] ||= @next_tweet - 1
fetch_in_timeline
end
def fetch_in_timeline(options = {})
options[:count] ||= 200
options[:contributor_details] = true
options[:include_my_retweet] = true
options[:include_rts] = true
options[:include_entities] = true
options[:max_id] = @next_tweet - 1 if @next_tweet
tweets = real_fetch_in_timeline(options)
tweets ||= [ ]
@end_of_tweets = true if tweets.count == 0
@next_tweet = tweets.last.attrs[:id] if tweets.count > 0
tweets
end
def real_fetch_in_timeline(options)
@twitter.user_timeline(@screen_name, options)
end
end
class FakeTimeline < Timeline
attr_reader :tweet_ids
attr_accessor :ignore_missing
attr_accessor :ignore_suspended
def initialize(tweet_ids, *params)
super(*params)
self.tweet_ids = tweet_ids
self.ignore_missing = true
self.ignore_suspended = true
end
def tweet_ids=(val)
@enum = nil
@tweet_ids = val
end
def tweet_ids_enum
@enum ||= @tweet_ids.to_enum(:each_tweet_id)
end
private
def real_fetch_in_timeline(options)
begin
begin
tweet_id = tweet_ids_enum.next
rescue StopIteration => e
return nil
end
tweets = [ @twitter.status(tweet_id, options) ]
rescue ::Twitter::Error::NotFound => error
raise unless @ignore_missing
$stderr.write "#"
retry
rescue ::Twitter::Error::Forbidden => error
raise unless ( /User has been suspended/.match(error.to_s) and @ignore_suspended )
retry
end
return tweets
end
end
end
end