Class: Rtlog::Archive
- Inherits:
-
Object
- Object
- Rtlog::Archive
- Includes:
- DirUtils
- Defined in:
- lib/rtlog/archives.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
- #logger ⇒ Object
Instance Method Summary collapse
- #day_entry(day) ⇒ Object
- #download(option = {}) ⇒ Object
- #download_all ⇒ Object
-
#initialize(config) ⇒ Archive
constructor
A new instance of Archive.
- #month_entry(month) ⇒ Object
- #next_month_entry(month_entry) ⇒ Object
- #previous_month_entry(month_entry) ⇒ Object
- #recent_day_entries(size = 7) ⇒ Object
- #recent_entry_id ⇒ Object
- #save(status) ⇒ Object
- #temp_dir ⇒ Object
- #twitter_id ⇒ Object
- #user_info ⇒ Object (also: #user)
- #year_entries ⇒ Object
Methods included from DirUtils
Constructor Details
#initialize(config) ⇒ Archive
Returns a new instance of Archive.
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/rtlog/archives.rb', line 219 def initialize config @config = config if config['twitter_id'] && config['twitter_password'] @tw = Rubytter.new(config['twitter_id'], config['twitter_password']) elsif config['consumer-key'] && config['consumer-secret'] && config['access-token'] && config['access-token-secret'] consumer = OAuth::Consumer.new( config['consumer-key'], config['consumer-secret'], :site => 'http://twitter.com' ) access_token = OAuth::AccessToken.new( consumer, config['access-token'], config['access-token-secret'] ) @tw = OAuthRubytter.new(access_token) else @tw = Rubytter.new end @year_entries = nil Time.zone = @config['time_zone'] || user_info.time_zone end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
216 217 218 |
# File 'lib/rtlog/archives.rb', line 216 def config @config end |
Instance Method Details
#day_entry(day) ⇒ Object
326 327 328 329 |
# File 'lib/rtlog/archives.rb', line 326 def day_entry day path = File.join( temp_dir, sprintf('%04d', day.year), sprintf('%02d', day.month), sprintf('%02d', day.day) ) DayEntry.new(config, path) end |
#download(option = {}) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/rtlog/archives.rb', line 340 def download option={} option['count'] ||= (@config['count'] || 200) option.reject! { |k,v| v==nil } timeline = @tw.user_timeline( twitter_id, option ) return false if timeline.size == 0 return false if timeline.last.id == option['max_id'] timeline.each do |status| status = ActiveSupport::JSON.decode(status.to_json) save status yield status if block_given? end @year_entries = nil @recent_day_entries = nil return timeline.last.id end |
#download_all ⇒ Object
359 360 361 362 363 364 365 366 367 |
# File 'lib/rtlog/archives.rb', line 359 def download_all max_id = nil while true max_id = download('max_id' => max_id) unless block_given? max_id = download('max_id' => max_id) { |status| yield status } if block_given? break unless max_id sleep 10 end end |
#month_entry(month) ⇒ Object
321 322 323 324 |
# File 'lib/rtlog/archives.rb', line 321 def month_entry month path = File.join( temp_dir, sprintf('%04d', month.year), sprintf('%02d', month.month) ) MonthEntry.new(config, path) end |
#next_month_entry(month_entry) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/rtlog/archives.rb', line 305 def next_month_entry month_entry return_next_entry = false year_entries.each do |y| return y.month_entries.first if return_next_entry && y.month_entries.size > 0 index = y.month_entries.index(month_entry) if index == nil next elsif y.month_entries.size == (index + 1) return_next_entry = true else return y.month_entries[index+1] end end return nil end |
#previous_month_entry(month_entry) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/rtlog/archives.rb', line 286 def previous_month_entry month_entry previous_year = nil previous_month = nil year_entries.each do |y| index = y.month_entries.index(month_entry) if index == 0 if previous_year && previous_year.month_entries.size > 0 return previous_year.month_entries.last else return nil end elsif index return y.month_entries[index-1] end previous_year = y end return nil end |
#recent_day_entries(size = 7) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/rtlog/archives.rb', line 261 def recent_day_entries size=7 unless defined?(@recent_day_entries) && @recent_day_entries @recent_day_entries = [] year_entries.each do |y| y.month_entries.each do |m| m.day_entries.each do |d| @recent_day_entries << d return @recent_day_entries if @recent_day_entries.size == size end end end end @recent_day_entries end |
#recent_entry_id ⇒ Object
253 254 255 256 257 258 259 |
# File 'lib/rtlog/archives.rb', line 253 def recent_entry_id year_entries.first.month_entries.first.day_entries.first.tweets do |tweet| return tweet.id end rescue nil end |
#save(status) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/rtlog/archives.rb', line 369 def save status Tweet.new(config, status).medias.each do |m| logger.debug("Download media: #{m.class}, #{m.original_thumbnail_url}") m.download sleep 2 end date = Time.zone.parse(status['created_at']) date = DateTime.parse(date.to_s) path = "#{temp_dir}/#{date.strftime('%Y/%m/%d')}/#{status['id']}.json" FileUtils.mkdir_p( File.dirname(path) ) unless File.exist?( File.dirname(path) ) File.open(path, "w") { |file| file.write(status.to_json) } logger.debug("Tweet is saved: #{path}") end |
#temp_dir ⇒ Object
331 332 333 334 |
# File 'lib/rtlog/archives.rb', line 331 def temp_dir @temp_dir ||= File.(@config['temp_dir']) @temp_dir end |
#twitter_id ⇒ Object
336 337 338 |
# File 'lib/rtlog/archives.rb', line 336 def twitter_id @config['twitter_id'] end |
#user_info ⇒ Object Also known as: user
247 248 249 250 |
# File 'lib/rtlog/archives.rb', line 247 def user_info @userinfo ||= @tw.user( twitter_id ) @userinfo end |