Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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