Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Download contents of inbox, sent and archived folders

  • Loading branch information...
commit abb067de3e4c9ef73c0b00af0364969db669e70c 1 parent 3e6502d
@ept authored
View
3  .gitignore
@@ -0,0 +1,3 @@
+closed.json
+messages.json
+sent.json
View
5 Gemfile
@@ -1,5 +1,8 @@
source :rubygems
+
gem 'em-http-request'
gem 'highline'
-gem 'deferrable_gratification'
gem 'json'
+
+# TODO: Update this when mainline DG has the features we want.
+gem 'deferrable_gratification', :git => 'git://github.com/ConradIrwin/deferrable_gratification.git'
View
11 Gemfile.lock
@@ -1,10 +1,15 @@
+GIT
+ remote: git://github.com/ConradIrwin/deferrable_gratification.git
+ revision: b4f93600b05cc7f431320472d3a6a788e246eba4
+ specs:
+ deferrable_gratification (0.3.1)
+ eventmachine
+
GEM
remote: http://rubygems.org/
specs:
addressable (2.2.6)
cookiejar (0.3.0)
- deferrable_gratification (0.3.1)
- eventmachine
em-http-request (1.0.1)
addressable (>= 2.2.3)
cookiejar
@@ -22,7 +27,7 @@ PLATFORMS
ruby
DEPENDENCIES
- deferrable_gratification
+ deferrable_gratification!
em-http-request
highline
json
View
4 lib/cotweet-export.rb
@@ -10,9 +10,9 @@
$stdout.sync = true
%w{
- connection
+ connection export
}.each do |filename|
require File.join(File.dirname(__FILE__), 'cotweet', filename)
end
-CoTweet::Connection.test!
+CoTweet::Export.run!
View
27 lib/cotweet/connection.rb
@@ -51,6 +51,7 @@ def get_json(path, query={})
get(:query => query, :head => {:cookie => cookies}).
transform do |http|
if http.response_header.status == 200
+ cmd.say "success: #{http.encode_query(http.req.uri, query)}"
JSON.parse(http.response)
else
cmd.say "Request to #{path} failed with HTTP #{http.response_header.status}"
@@ -59,13 +60,25 @@ def get_json(path, query={})
end
end
- def self.test!
- EM.run do
- new.login.bind! do |connection|
- connection.get_json '/api/1/channels/sources.json'
- end.bothback do |result|
- puts result.inspect
- EM.stop
+ # Values for timeline_name:
+ # * 'messages' is the inbox (non-archived messages). It has subcategories:
+ # 'dms' is received DMs, 'replies' is received @replies, 'mentions' is received @mentions
+ # * 'twitter_friends_timeline' is friends' updates
+ # * 'sent' is sent messages
+ # * 'closed' is the archive
+ def each_message(timeline_name, &block)
+ finished = false
+ waterline = nil
+ DG::loop_until(proc { finished }) do
+ query = {:limit => 40}
+ query[:max] = waterline if waterline
+
+ get_json("/api/1/timeline/#{timeline_name}.json", query).safe_callback do |json|
+ finished = json['items'].nil? || json['items'].empty?
+ unless finished
+ waterline = json['items'].last['id']
+ json['items'].each(&block)
+ end
end
end
end
View
59 lib/cotweet/export.rb
@@ -0,0 +1,59 @@
+module CoTweet
+ class Export
+
+ attr_reader :connection
+
+ def initialize(connection)
+ @connection = connection
+ end
+
+ def export_timeline(timeline_name)
+ file = File.open("#{timeline_name}.json", 'w')
+ file.puts '{"items": ['
+ first_message = true
+
+ connection.each_message(timeline_name) do |msg|
+ file.puts ',' unless first_message
+ file.write JSON.pretty_generate(msg)
+ first_message = false
+ end.bothback do
+ file.puts ']}'
+ file.close
+ end
+ end
+
+ def run
+ JoinCarefully.setup! *%w(messages sent closed).map(&method(:export_timeline))
+ end
+
+ def self.run!
+ EM.run do
+ Connection.new.login.bind! do |connection|
+ Export.new(connection).run
+ end.errback do |error|
+ puts "Error: #{error.inspect}"
+ puts error.backtrace.map{|line| " #{line}" }.join("\n") if error.respond_to? :backtrace
+ end.bothback do
+ EM.stop
+ end
+ end
+ end
+
+ # If any of the joined deferrables fails, fails immediately with that failure's error.
+ # Otherwise waits for all deferrables to succeed, and succeeds with an array of success results.
+ class JoinCarefully < DeferrableGratification::Combinators::Join
+ private
+ def done?
+ failures.length > 0 || all_completed?
+ end
+
+ def finish
+ if failures.empty?
+ succeed(successes)
+ else
+ fail(failures.first)
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.