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
|
# File 'lib/akane-bigquery/cli.rb', line 91
def prepare(source, prefix)
limit = 524288000
count = -1
bytes = 0
new_io = lambda do
bytes = 0
count += 1
path = File.join(prefix, "tweets.#{count.to_s.rjust(4,'0')}.txt")
puts "=> Using #{path}"
File.open(path, 'w')
end
io = new_io.call
months = options[:months] && options[:months].split(/,/)
before = options[:before] && Time.parse(options[:before])
userdirs = Dir.entries(File.join(source, "users"))
userdirs.each_with_index do |user_dirname, index|
next if user_dirname == "." || user_dirname == ".."
puts " * #{user_dirname} (#{index.succ}/#{userdirs.size}, #{((index.succ/userdirs.size.to_f)*100).to_i}%)"
userdir = File.join(source, "users", user_dirname)
= if options[:months]
months.map { |_| File.join(userdir, "tweets.#{_}.txt") }
else
Dir[File.join(userdir, 'tweets.*.txt')]
end
.each do |file|
begin
File.open(file, 'r') do ||
.each_line do |line|
json = line.chomp
= Oj.load(json)
created_at = Time.parse(['created_at'.freeze])
next if before && before <= created_at
new_json = {
'json'.freeze => json,
'id_str'.freeze => ['id_str'.freeze],
'id'.freeze => ['id'.freeze],
'text'.freeze => ['text'.freeze],
'lang'.freeze => ['lang'.freeze],
'source'.freeze => ['source'.freeze],
'in_reply_to_status_id'.freeze => ['in_reply_to_status_id'.freeze],
'in_reply_to_status_id_str'.freeze => ['in_reply_to_status_id_str'.freeze],
'in_reply_to_user_id'.freeze => ['in_reply_to_user_id'.freeze],
'in_reply_to_user_id_str'.freeze => ['in_reply_to_user_id_str'.freeze],
'in_reply_to_screen_name'.freeze => ['in_reply_to_screen_name'.freeze],
'user'.freeze => {
'id_str'.freeze => ['user'.freeze]['id_str'.freeze],
'id'.freeze => ['user'.freeze]['id'.freeze],
'name'.freeze => ['user'.freeze]['name'.freeze],
'screen_name'.freeze => ['user'.freeze]['screen_name'.freeze],
'protected'.freeze => ['user'.freeze]['protected'.freeze],
},
'created_at'.freeze => created_at.to_i
}
if ['coordinates'.freeze]
new_json['coordinates_longitude'.freeze] = ['coordinates'.freeze]['coordinates'.freeze][0]
new_json['coordinates_latitude'.freeze] = ['coordinates'.freeze]['coordinates'.freeze][1]
end
if ['place'.freeze]
place = ['place'.freeze]
new_json['place'.freeze] = {
'id'.freeze => place['id'.freeze],
'country'.freeze => place['country'.freeze],
'country_code'.freeze => place['country_code'.freeze],
'name'.freeze => place['name'.freeze],
'full_name'.freeze => place['full_name'.freeze],
'place_type'.freeze => place['place_type'.freeze],
'url'.freeze => place['url'.freeze],
}
end
new_json_str = Oj.dump(new_json)
io.puts new_json_str
bytes += new_json_str.size + 1
io = new_io.call if limit <= bytes
end
end
rescue Errno::ENOENT
end
end
end
end
|