-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-reader-console
executable file
·123 lines (97 loc) · 4.11 KB
/
google-reader-console
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
83
84
85
86
87
88
89
90
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
#!/usr/bin/env ruby
############################################################
# Copyright (c) 2010, iGenesis Ltda. #
# Author: Gustavo Machado C. Gama <gustavo.gama@gmail.com> #
############################################################
require 'optparse'
require 'irb'
require 'rack'
require 'json'
require 'google-reader/auth'
require 'google-reader/client'
require 'google-reader/user'
module Google; module Reader
class Console
include Google::Reader::Auth
include Google::Reader::Client
WEB_SERVER_PORT = 8088
@@rack_handler = Rack::Handler::default
def initialize(request_token_file, access_token_file)
@request_token_file = request_token_file
@access_token_file = access_token_file
if File.readable?(@request_token_file)
token, secret = IO.read(@request_token_file).split(/\r?\n/)
@request_token = ::OAuth::RequestToken.new(consumer, token, secret) if token && secret
end
if File.readable?(@access_token_file)
token, secret = IO.read(@access_token_file).split(/\r?\n/)
@access_token = ::OAuth::AccessToken.new(consumer, token, secret) if token && secret
end
# use the net::http of the AccessToken as a request proxy
self.request_proxy = access_token
end
def request_token
@request_token ||= begin
token = super
File.open(@request_token_file, 'w') do |file|
file.write "#{token.token}\n#{token.secret}\n"
end
token
end
end
def access_token
@access_token ||= begin
puts "please access #{request_token.authorize_url} using your web browser."
if @@rack_handler.respond_to?(:shutdown)
['INT', 'TERM'].each{|signal| trap(signal) { @@rack_handler.shutdown } }
end
@@rack_handler.run(self, :Port => WEB_SERVER_PORT)
token = super
File.open(@access_token_file, 'w') do |file|
file.write "#{token.token}\n#{token.secret}\n"
end
token
end
end
def google_reader_authorize_callback
"http://localhost:#{WEB_SERVER_PORT}/authorize"
end
#############################################################
# Rack Handler (automatically handles authorization callback)
#############################################################
# implement the Rack interface to handle the 'authorize callback'
def call(env)
if env['REQUEST_URI'].start_with?(self.google_reader_authorize_callback)
extract_verifier(env['REQUEST_URI'])
# stop the rack server
if @@rack_handler.respond_to?(:shutdown) # rack >= 1.2
@@rack_handler.shutdown
else
Process.kill('INT', 0)
end
[200, {'Content-Type' => 'text/plain'}, ["Request token accepted (#{@oauth_token}/#{@oauth_verifier}). Proceeding.\n"]]
else
[404, {'Content-Type' => 'text/plain'}, ['']]
end
end
end
end; end # module Google::Reader
############################ MAIN ###############################
# default token files
request_token_file = File.join(ENV['HOME'], '.cache', 'google', 'reader', 'oauth-request-token')
access_token_file = File.join(ENV['HOME'], '.cache', 'google', 'reader', 'oauth-access-token')
OptionParser.new do |opts|
opts.banner = "Usage: #$0 [-r <request-token-file>][-a <access-token-file>]\n"
opts.on '-r', '--request-token <request-token-file>', 'file with oauth request token' do |f| request_token_file = f; end
opts.on '-a', '--access-token <access-token-file> ', 'file with oauth access token' do |f| access_token_file = f; end
end.parse!
@console = Google::Reader::Console.new(request_token_file, access_token_file)
@user = @console.user
puts <<EOS
--------------- Google Reader interactive console --------------
The base client is available through the instance variable '@console'.
The authenticated user may be retrieved though '@console.user':
irb> puts @console.user.subscriptions.inspect
EOS
require 'irb'
IRB.start