-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
155 lines (137 loc) · 4.42 KB
/
app.rb
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
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
require 'sinatra'
require 'sinatra/streaming'
require 'oauth'
require 'readit'
require 'read_it_later'
require 'haml'
require 'json'
enable :sessions
set :haml, :format => :html5
# CONFIG START
RIL_KEY = ENV['RIL_KEY']
RD_KEY = ENV['RD_KEY']
RD_SECRET = ENV['RD_SECRET']
if ENV['RACK_ENV'] == 'development'
HOST_CALLBACK = "http://localhost:3000/auth/callback"
else
HOST_CALLBACK = "http://readability-importer.heroku.com/auth/callback"
end
# CONFIG END
get '/' do
session.clear
haml :index
end
get '/auth/callback' do
if params[:oauth_verifier].nil?
'Looks like you denied us access. Halting. <a href="/">Try again</a>'
else
@request_token = session[:request_token]
@access_token = @request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
session[:access_token] = @access_token
# grab article count from readability
Readit::Config.consumer_key = RD_KEY
Readit::Config.consumer_secret = RD_SECRET
@api = Readit::API.new @access_token.token, @access_token.secret
page = 1
bookmarks = []
while new_bookmarks = @api.bookmarks(:page => page)
bookmarks += new_bookmarks
page = page + 1
end
ril_user = ReadItLater::User.new(session[:ril_username], session[:ril_password])
ril = ReadItLater.new(RIL_KEY)
ril_response = ril.auth(ril_user)
if ril_response[:status] != 200
'Oops, your ReadItLater credentials are no good. Halting. <a href="/">Try again</a>'
else
ril_articles = ril.get(ril_user, {:state => :unread})
url_json = JSON.parse(ril_articles[:text])
haml :auth, :locals => {
:ril_article_count => url_json['list'].count,
:readability_article_count => bookmarks.count
}
end
end
end
get '/go' do
@access_token = session[:access_token]
if @access_token.nil?
"Oops, we have no login information for you. <a href=\"/\">Go Home</a>"
else
Readit::Config.consumer_key = RD_KEY
Readit::Config.consumer_secret = RD_SECRET
@api = Readit::API.new @access_token.token, @access_token.secret
ril_user = ReadItLater::User.new(session[:ril_username], session[:ril_password])
ril = ReadItLater.new(RIL_KEY)
ril_response = ril.auth(ril_user)
if ril_response[:status] != 200
'Oops, your ReadItLater credentials are no good. Halting. <a href="/">Try again</a>'
else
ril_articles = ril.get(ril_user, {:state => :unread})
url_json = JSON.parse(ril_articles[:text])
url_list = []
stream(:keep_open) do |out|
url_json['list'].to_a.reverse.each do |key,value|
out.puts "Adding #{value['url']} <br />"
out.flush
@api.bookmark :url => value['url']
end
session.clear
out.puts "Okay, #{@api.me.username}, I sent #{url_json['list'].count} URLs to Readability. Enjoy."
out.flush
end
end
end
end
get '/delete' do
@access_token = session[:access_token]
if @access_token.nil?
"Oops, we have no login information for you. <a href=\"/\">Go Home</a>"
else
Readit::Config.consumer_key = RD_KEY
Readit::Config.consumer_secret = RD_SECRET
@api = Readit::API.new @access_token.token, @access_token.secret
# now, delete all the bookmarks
page = 1
bookmarks = []
while new_bookmarks = @api.bookmarks(:page => page)
bookmarks += new_bookmarks
page = page + 1
end
bookmark_count = bookmarks.count
stream(:keep_open) do |out|
bookmarks.each do |b|
out.puts "Deleting ID #{b.id}<br/>"
out.flush
p "#{b.id}"
@api.delete_bookmark(b.id)
end
session.clear
out << "Okay, I deleted #{bookmark_count} bookmarks from your Readability account.<br /><a href=\"/\">Go Home</a>."
end
end
end
post '/auth' do
# test for readitlater credentials
ril_user = ReadItLater::User.new(params[:username], params[:password])
ril = ReadItLater.new(RIL_KEY)
ril_response = ril.auth(ril_user)
if ril_response[:status] != 200
redirect '/'
end
session.clear
session[:ril_username] = params[:username]
session[:ril_password] = params[:password]
# oauth with readability
@callback_url = HOST_CALLBACK
@consumer = OAuth::Consumer.new(RD_KEY, RD_SECRET,
{
:site => "https://www.readability.com",
:authorize_path=>"/api/rest/v1/oauth/authorize/",
:access_token_path => "/api/rest/v1/oauth/access_token/",
:request_token_path=>"/api/rest/v1/oauth/request_token/"
})
@request_token = @consumer.get_request_token(:oauth_callback => @callback_url)
session[:request_token] = @request_token
redirect @request_token.authorize_url(:oauth_callback => @callback_url)
end