-
Notifications
You must be signed in to change notification settings - Fork 1
/
PopulateTweets.rb
82 lines (56 loc) · 1.97 KB
/
PopulateTweets.rb
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
require_relative 'lookup_tweets/requests/StatusesLookup'
require 'trollop'
require 'json'
USAGE = %Q{
get_tweets_lookup: returns the tweet objects associated with a given set of tweet ids
Example:
ruby get_tweets_lookup -p oauth.properties --ids <file_of_tweet_ids>
Usage:
ruby get_statuses_lookup.rb <options>
The following options are required:
}
def parse_command_line
options_props = {type: :string, required: true}
options_input = {type: :string, required: true}
opts = Trollop::options do
version "2017 K. Stowe, based on get_lookup 0.1 (c) 2015 Kenneth M. Anderson"
banner USAGE
opt :props, "OAuth Properties File", options_props
opt :input, "File to populate tweets to", options_input
end
unless File.exist?(opts[:props])
Trollop::die :props, "must point to a valid oauth properties file"
end
unless File.exist?(opts[:input])
Trollop::die :input, "must point to an existing annotation file"
end
opts
end
if __FILE__ == $0
STDOUT.sync = true
input = parse_command_line
params = { id: "" }
data = { props: input[:props] }
args = { params: params, data: data }
twitter = StatusesLookup.new(args)
json_data = JSON.parse( File.read ( input[:input] ) )
list_of_ids = json_data.keys
result = {}
tot = 0
while list_of_ids.length > 0
puts String(list_of_ids.length) + " tweets to process..."
ids = list_of_ids.shift(100)
twitter.params[:id] = ids.join(",")
twitter.collect do |tweets|
tot += tweets.length
tweets.each do |tweet|
result[tweet["id_str"]] = {"text" => tweet["text"], "annotations" => json_data[tweet["id_str"]]}
end
end
end
puts "Writing annotated tweets to " + String(input[:input] + "-t")
File.open(input[:input] + "-t", "w") do |f|
f.write(result.to_json)
end
puts String(tot) + " tweets written of " + String(json_data.keys.length)
end