Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Download conversation history for all correspondents
Browse files Browse the repository at this point in the history
  • Loading branch information
ept committed Feb 6, 2012
1 parent abb067d commit bf360ae
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
closed.json closed.json
messages.json messages.json
sent.json sent.json
conversations
1 change: 1 addition & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source :rubygems
gem 'em-http-request' gem 'em-http-request'
gem 'highline' gem 'highline'
gem 'json' gem 'json'
gem 'andand'


# TODO: Update this when mainline DG has the features we want. # TODO: Update this when mainline DG has the features we want.
gem 'deferrable_gratification', :git => 'git://github.com/ConradIrwin/deferrable_gratification.git' gem 'deferrable_gratification', :git => 'git://github.com/ConradIrwin/deferrable_gratification.git'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
addressable (2.2.6) addressable (2.2.6)
andand (1.3.1)
cookiejar (0.3.0) cookiejar (0.3.0)
em-http-request (1.0.1) em-http-request (1.0.1)
addressable (>= 2.2.3) addressable (>= 2.2.3)
Expand All @@ -27,6 +28,7 @@ PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
andand
deferrable_gratification! deferrable_gratification!
em-http-request em-http-request
highline highline
Expand Down
3 changes: 2 additions & 1 deletion lib/cotweet-export.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
require 'eventmachine' require 'eventmachine'
require 'deferrable_gratification' require 'deferrable_gratification'
require 'em-http' require 'em-http'
require 'set'


DG.enhance! EM::HttpClient DG.enhance! EM::HttpClient


$stdout.sync = true $stdout.sync = true


%w{ %w{
connection export connection download_queue export
}.each do |filename| }.each do |filename|
require File.join(File.dirname(__FILE__), 'cotweet', filename) require File.join(File.dirname(__FILE__), 'cotweet', filename)
end end
Expand Down
4 changes: 4 additions & 0 deletions lib/cotweet/connection.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,5 +82,9 @@ def each_message(timeline_name, &block)
end end
end end
end end

def get_conversation(user_id)
get_json "/api/1/twitterers/#{user_id}/conversations.json", :limit => 20
end
end end
end end
46 changes: 46 additions & 0 deletions lib/cotweet/download_queue.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,46 @@
module CoTweet
class DownloadQueue
MAX_CONCURRENCY = 10

def initialize(&block)
@operation = block
@items_seen = Set.new
@active = {}
@queued = []
end

def <<(item)
return if @items_seen.include? item
@items_seen << item

if has_capacity?
start(item)
else
@queued << item
end
end

def finish
return DG.success if idle?
@finishing ||= DG.blank
end

private

def idle?
@active.empty?
end

def has_capacity?
@active.size < MAX_CONCURRENCY
end

def start(item)
@active[item] = @operation.call(item).bothback do
@active.delete item
start(@queued.shift) if has_capacity? && !@queued.empty?
@finishing.succeed if @finishing && idle?
end
end
end
end
14 changes: 13 additions & 1 deletion lib/cotweet/export.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@ def export_timeline(timeline_name)
file.puts ',' unless first_message file.puts ',' unless first_message
file.write JSON.pretty_generate(msg) file.write JSON.pretty_generate(msg)
first_message = false first_message = false
msg['twitterer'].andand['id'].andand {|user_id| conversation_downloads << user_id }
end.bothback do end.bothback do
file.puts ']}' file.puts ']}'
file.close file.close
end end
end end


def conversation_downloads
@conversation_downloads ||= DownloadQueue.new do |user_id|
connection.get_conversation(user_id).safe_callback do |json|
File.open("conversations/#{user_id}.json", 'w') do |file|
file.write JSON.pretty_generate(json)
end
end
end
end

def run def run
JoinCarefully.setup! *%w(messages sent closed).map(&method(:export_timeline)) JoinCarefully.setup!(*%w(messages sent closed).map(&method(:export_timeline))).
bind! { conversation_downloads.finish }
end end


def self.run! def self.run!
Expand Down

0 comments on commit bf360ae

Please sign in to comment.