Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 243 lines (209 sloc) 7.474 kB
cb8463b @gimite Initial version.
gimite authored
1 # coding: utf-8
2
7350fd7 @gimite Adding copyright notice and README. Putting config constants in TSSCo…
gimite authored
3 # Copyright: Hiroshi Ichikawa <http://gimite.net/en/>
4 # License: New BSD License
5
efb1a25 @gimite Supporting Ruby 1.9.
gimite authored
6 $KCODE = "u" if RUBY_VERSION < "1.9.0"
cb8463b @gimite Initial version.
gimite authored
7
8 require "securerandom"
9 require "net/http"
10 require "pp"
11 require "cgi"
12 require "time"
13 require "enumerator"
14 require "rss"
15 require "open-uri"
16
17 require "rubygems"
18 require "json"
19 require "oauth"
20 require "twitter"
21 require "sinatra/base"
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
22 require "sinatra/async"
325bd73 @gimite Using sinatra-reloader.
gimite authored
23 require "sinatra/reloader"
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
24 require "em-http"
d4d9083 @gimite Adding Japanese interface.
gimite authored
25 require "http_accept_language"
cb8463b @gimite Initial version.
gimite authored
26
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
27 require "moji"
cb8463b @gimite Initial version.
gimite authored
28 require "tss_config"
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
29 require "tss_helper"
cb8463b @gimite Initial version.
gimite authored
30
31
d4d9083 @gimite Adding Japanese interface.
gimite authored
32 Sinatra::Request.send(:include, HttpAcceptLanguage)
33
34
cb8463b @gimite Initial version.
gimite authored
35 class TSSWebServer < Sinatra::Base
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
36
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
37 include(TSSHelper)
38 include(ERB::Util)
39
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
40 HASH_TAG_EXP = /^\#[a-zA-Z0-9_]+$/
41
7350fd7 @gimite Adding copyright notice and README. Putting config constants in TSSCo…
gimite authored
42 set(:port, TSSConfig::WEB_SERVER_PORT)
43 set(:environment, TSSConfig::SINATRA_ENVIRONMENT)
cb8463b @gimite Initial version.
gimite authored
44 set(:public, "./public")
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
45 set(:logging, true)
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
46 use(Rack::Session::Cookie, {
47 :key => TSSConfig::SESSION_COOKIE_KEY,
48 :path => "/",
49 :expire_after => 3 * 30 * 24 * 3600,
325bd73 @gimite Using sinatra-reloader.
gimite authored
50 # Reuses Twitter key. Anything secret is fine.
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
51 :secret => TSSConfig::TWITTER_API_WRITE_SECRET,
52 })
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
53 register(Sinatra::Async)
325bd73 @gimite Using sinatra-reloader.
gimite authored
54 configure(:development) do
55 register(Sinatra::Reloader)
56 end
cb8463b @gimite Initial version.
gimite authored
57
58 before() do
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
59 @twitter = get_twitter(session[:access_token], session[:access_token_secret])
d4d9083 @gimite Adding Japanese interface.
gimite authored
60 @lang = params[:hl]
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
61 if !@lang && ["/", "/search", "/js/search.js"].include?(request.path)
23f24c9 @gimite Fixing error when ACCEPT_LANGUAGE is not set.
gimite authored
62 @lang = request.compatible_language_from(["en", "ja"]) || "en"
0082cd4 @gimite Wouraround for weird redirect and weird content type.
gimite authored
63 redirect(TSSConfig::BASE_URL + to_url(request, {"hl" => @lang}))
d4d9083 @gimite Adding Japanese interface.
gimite authored
64 end
cb8463b @gimite Initial version.
gimite authored
65 end
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
66
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
67 aget("/") do
68 get_buzz_words("en") do |buzz_words|
69 buzz_words ||= []
5e56eab @gimite Avoiding choosing key word other than hash tag in home page.
gimite authored
70 query = params[:q] || buzz_words.grep(/^\#\S+$/)[0] || ""
0082cd4 @gimite Wouraround for weird redirect and weird content type.
gimite authored
71 content_type("text/html", :charset => "utf-8")
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
72 body(search(query, :search, true))
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
73 LOGGER.info("[web] GET /")
74 end
cb8463b @gimite Initial version.
gimite authored
75 end
76
77 get("/search") do
78 query = params[:q] || ""
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
79 return search(query, :search, false)
80 end
81
82 get("/ustream") do
9b6dee9 @chrismatthieu tweeking
authored
83 # key=339100083E900F21974AE0A065A54BF2
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
84 channel = params[:channel]
85 info = JSON.load(
9b6dee9 @chrismatthieu tweeking
authored
86 open("http://api.ustream.tv/json/channel/%s/getInfo?key=339100083E900F21974AE0A065A54BF2" % CGI.escape(channel)){ |f| f.read() })
87 @channel_id = info["results"]["id"] rescue "cnn"
88 query = info["results"]["socialStream"]["hashtag"] rescue "cnn"
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
89 return search(query, :ustream, false)
cb8463b @gimite Initial version.
gimite authored
90 end
91
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
92 post("/login") do
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
93 callback_url =
94 "#{TSSConfig::BASE_URL}/oauth_callback?redirect=" + CGI.escape(params[:redirect] || "")
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
95 request_token = self.oauth_consumer.get_request_token(:oauth_callback => callback_url)
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
96 session[:request_token] = request_token.token
97 session[:request_token_secret] = request_token.secret
cb8463b @gimite Initial version.
gimite authored
98 redirect(request_token.authorize_url)
99 end
100
101 get("/oauth_callback") do
102 request_token = OAuth::RequestToken.new(
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
103 self.oauth_consumer, session[:request_token], session[:request_token_secret])
cb8463b @gimite Initial version.
gimite authored
104 begin
105 @access_token = request_token.get_access_token(
106 {},
107 :oauth_token => params[:oauth_token],
108 :oauth_verifier => params[:oauth_verifier])
109 rescue OAuth::Unauthorized => @exception
d4d9083 @gimite Adding Japanese interface.
gimite authored
110 return erubis(%{ Authentication failed: <%=h @exception.message %> })
cb8463b @gimite Initial version.
gimite authored
111 end
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
112 @twitter = get_twitter(@access_token.token, @access_token.secret)
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
113 session[:access_token] = @access_token.token
114 session[:access_token_secret] = @access_token.secret
115 session[:screen_name] = @twitter.verify_credentials().screen_name
cb8463b @gimite Initial version.
gimite authored
116 if params[:redirect] && params[:redirect] =~ /\A\//
df10023 @gimite Fixing redirects.
gimite authored
117 redirect(TSSConfig::BASE_URL + params[:redirect])
cb8463b @gimite Initial version.
gimite authored
118 else
df10023 @gimite Fixing redirects.
gimite authored
119 redirect(TSSConfig::BASE_URL + "/")
cb8463b @gimite Initial version.
gimite authored
120 end
121 end
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
122
123 post("/update") do
124 @twitter.update(params[:status])
125 return "ok"
126 end
cb8463b @gimite Initial version.
gimite authored
127
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
128 aget("/buzz") do
d4d9083 @gimite Adding Japanese interface.
gimite authored
129 get_result = proc() do |lang_id, lang_name, &block|
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
130 get_buzz_words(lang_id) do |words|
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
131 words = (words || []).grep(HASH_TAG_EXP)[0, 10]
d4d9083 @gimite Adding Japanese interface.
gimite authored
132 block.call({"lang_id" => lang_id, "lang_name" => lang_name, "words" => words})
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
133 end
134 end
d4d9083 @gimite Adding Japanese interface.
gimite authored
135 get_result.call("en", "English") do |en_result|
136 get_result.call("ja", "Japanese") do |ja_result|
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
137 content_type("text/javascript", :charset => "utf-8")
d4d9083 @gimite Adding Japanese interface.
gimite authored
138 all_result = @lang == "ja" ? [ja_result, en_result] : [en_result, ja_result]
139 body(JSON.dump(all_result))
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
140 LOGGER.info("[web] GET /buzz")
141 end
cb8463b @gimite Initial version.
gimite authored
142 end
143 end
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
144
cb8463b @gimite Initial version.
gimite authored
145 get("/logout") do
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
146 session.clear()
df10023 @gimite Fixing redirects.
gimite authored
147 redirect("%s/?hl=%s" % [TSSConfig::BASE_URL, @lang])
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
148 end
149
150 get("/css/default.css") do
151 @webkit = request.user_agent =~ /AppleWebKit/
152 content_type("text/css")
d4d9083 @gimite Adding Japanese interface.
gimite authored
153 erubis(:"default.css")
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
154 end
155
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
156 get("/js/search.js") do
157 content_type("text/javascript")
158 erubis(:"search.js")
159 end
160
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
161 def get_twitter(access_token, access_token_secret)
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
162 if access_token && access_token_secret
d4d9083 @gimite Adding Japanese interface.
gimite authored
163 return Twitter::Client.new({
164 :consumer_key => TSSConfig::TWITTER_API_WRITE_KEY,
165 :consumer_secret => TSSConfig::TWITTER_API_WRITE_SECRET,
166 :oauth_token => access_token,
167 :oauth_token_secret => access_token_secret,
168 })
5b201ec @gimite Implementing session cleanup. Making /login POST. Adding log-out link…
gimite authored
169 else
170 return nil
171 end
172 end
173
174 def oauth_consumer
59617b7 @gimite Following reconnection wait spec in Twitter API document.
gimite authored
175 return OAuth::Consumer.new(
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
176 TSSConfig::TWITTER_API_WRITE_KEY,
177 TSSConfig::TWITTER_API_WRITE_SECRET,
3b6363c @gimite Fixing error on Twitter login.
gimite authored
178 :site => "http://twitter.com")
cb8463b @gimite Initial version.
gimite authored
179 end
180
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
181 def search(query, template, index)
182
5e56eab @gimite Avoiding choosing key word other than hash tag in home page.
gimite authored
183 @query = query
184 @query.force_encoding(Encoding::UTF_8) if @query.respond_to?(:force_encoding)
59617b7 @gimite Following reconnection wait spec in Twitter API document.
gimite authored
185 @index = index
d4d9083 @gimite Adding Japanese interface.
gimite authored
186 web_socket_url = "ws://%s:%d/" %
59617b7 @gimite Following reconnection wait spec in Twitter API document.
gimite authored
187 [URI.parse(TSSConfig::BASE_URL).host, TSSConfig::WEB_SOCKET_SERVER_PORT]
c3b9e8d @gimite Making it possible to tweet on the search result page. Switching to r…
gimite authored
188 @screen_name = session[:screen_name]
189 @support_update = @query =~ HASH_TAG_EXP
190 @show_update = params[:show_update]
d4d9083 @gimite Adding Japanese interface.
gimite authored
191 @show_update_url = to_url(request, {"show_update" => "true"})
192 @another_lang_url = to_url(request, {"hl" => @lang == "ja" ? "en" : "ja"})
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
193
194 if params[:title]
195 @head_title = @body_title = params[:title]
196 elsif !@query.empty? && !index
8d4e8d2 @chrismatthieu a few tweeks
authored
197 @head_title = "%s - Raply" % @query
198 @body_title = "Raply"
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
199 else
8d4e8d2 @chrismatthieu a few tweeks
authored
200 @head_title = @body_title = "Raply"
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
201 end
59617b7 @gimite Following reconnection wait spec in Twitter API document.
gimite authored
202 @logo_url = params[:logo]
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
203
d4d9083 @gimite Adding Japanese interface.
gimite authored
204 @js_vars_json = JSON.dump({
205 "query" => @query,
206 "lang" => @lang,
207 "web_socket_url" => web_socket_url,
208 })
95b457f @gimite Fixing a bug that it doesn't work when JSON from Twitter has more tha…
gimite authored
209 return erubis(template)
210
cb8463b @gimite Initial version.
gimite authored
211 end
212
888cf8b @gimite Using em-http-request instead of blocking open-uri to fetch Buzztter.…
gimite authored
213 def get_buzz_words(lang_id, &block)
214 http = EventMachine::HttpRequest.new("http://buzztter.com/#{lang_id}/rss").get()
215 http.callback() do
216 never_die() do
217 if http.response_header.status == 200
218 rss = RSS::Parser.parse(http.response)
219 yield(rss.items.map(){ |t| t.title })
220 else
221 LOGGER.error("[web] Buzztter fetch failed: status=%p" % http.response_header.status)
222 yield(nil)
223 end
224 end
225 end
226 http.errback() do
227 never_die() do
228 LOGGER.error("[web] Buzztter fetch failed: connection error")
229 yield(nil)
230 end
231 end
cb8463b @gimite Initial version.
gimite authored
232 end
233
d4d9083 @gimite Adding Japanese interface.
gimite authored
234 def to_url(request, params)
235 return "%s?%s" % [
236 request.path,
343ff4f @gimite Fixing internal server error on weird URL parameter.
gimite authored
237 request.params.merge(params).
238 map(){ |k, v| CGI.escape(k) + "=" + CGI.escape(v || "") }.join("&"),
d4d9083 @gimite Adding Japanese interface.
gimite authored
239 ]
240 end
241
cb8463b @gimite Initial version.
gimite authored
242 end
Something went wrong with that request. Please try again.