Skip to content

Commit

Permalink
Fixing a bug that it doesn't work when JSON from Twitter has more tha…
Browse files Browse the repository at this point in the history
…n one field named "text" (which is now true).

Adding incomplete Ustream mode.
  • Loading branch information
gimite committed Dec 14, 2010
1 parent 343ff4f commit 95b457f
Show file tree
Hide file tree
Showing 7 changed files with 811 additions and 513 deletions.
21 changes: 12 additions & 9 deletions lib/tss_em_web_socket_server.rb
Expand Up @@ -187,15 +187,18 @@ def reconnect_to_stream(by_timer = false)
end
end,
:on_entry => proc() do |json|
if json =~ /"text":"(([^"\\]|\\.)*)"/
text = $1.downcase
json.slice!(json.length - 1, 1) # Deletes last '}'
data = '{"entries": [%s,"now":%f}]}' % [json, Time.now.to_f()]
for query, wsocks in @query_to_wsocks
if text.index(query)
for wsock in wsocks
wsock.send(data)
end
# For efficiency, avoids parsing and redumping JSON.
# Instead, uses regexp to extract Tweet text to match search query, and uses string
# manipulation to add additional field "now".
# There may be more than one "text" in different level and it's hard to find real one,
# so joins values of all "text" field.
text = json.scan(/"text":"(([^"\\]|\\.)*)"/).map(){ |a, b| a }.join(" ").downcase
json.slice!(json.length - 1, 1) # Deletes last '}'
data = '{"entries": [%s,"now":%f}]}' % [json, Time.now.to_f()]
for query, wsocks in @query_to_wsocks
if text.index(query)
for wsock in wsocks
wsock.send(data)
end
end
end
Expand Down
38 changes: 31 additions & 7 deletions lib/tss_web_server.rb
Expand Up @@ -58,7 +58,7 @@ class TSSWebServer < Sinatra::Base
before() do
@twitter = get_twitter(session[:access_token], session[:access_token_secret])
@lang = params[:hl]
if !@lang && ["/", "/search"].include?(request.path)
if !@lang && ["/", "/search", "/js/search.js"].include?(request.path)
@lang = request.compatible_language_from(["en", "ja"]) || "en"
redirect(TSSConfig::BASE_URL + to_url(request, {"hl" => @lang}))
end
Expand All @@ -69,14 +69,23 @@ class TSSWebServer < Sinatra::Base
buzz_words ||= []
query = params[:q] || buzz_words.grep(/^\#\S+$/)[0] || buzz_words[0] || ""
content_type("text/html", :charset => "utf-8")
body(search(query, true))
body(search(query, :search, true))
LOGGER.info("[web] GET /")
end
end

get("/search") do
query = params[:q] || ""
return search(query, false)
return search(query, :search, false)
end

get("/ustream") do
channel = params[:channel]
info = JSON.load(
open("http://api.ustream.tv/json/channel/%s/getInfo" % CGI.escape(channel)){ |f| f.read() })
@channel_id = info["results"]["id"]
query = info["results"]["socialStream"]["hashtag"]
return search(query, :ustream, false)
end

post("/login") do
Expand Down Expand Up @@ -143,6 +152,11 @@ class TSSWebServer < Sinatra::Base
erubis(:"default.css")
end

get("/js/search.js") do
content_type("text/javascript")
erubis(:"search.js")
end

def get_twitter(access_token, access_token_secret)
if access_token && access_token_secret
return Twitter::Client.new({
Expand All @@ -163,7 +177,8 @@ def oauth_consumer
:site => "http://twitter.com")
end

def search(query, index)
def search(query, template, index)

@query = query.force_encoding(Encoding::UTF_8)
@index = index
web_socket_url = "ws://%s:%d/" %
Expand All @@ -173,15 +188,24 @@ def search(query, index)
@show_update = params[:show_update]
@show_update_url = to_url(request, {"show_update" => "true"})
@another_lang_url = to_url(request, {"hl" => @lang == "ja" ? "en" : "ja"})
@title = params[:title]

if params[:title]
@head_title = @body_title = params[:title]
elsif !@query.empty? && !index
@head_title = "%s - Tweet Search Stream" % @query
@body_title = "Tweet Search Stream"
else
@head_title = @body_title = "Tweet Search Stream"
end
@logo_url = params[:logo]

@js_vars_json = JSON.dump({
"query" => @query,
"lang" => @lang,
"web_socket_url" => web_socket_url,
})
# Uses Erubis instead of ERB here because ERB causes encoding error for unknown reason.
return erubis(:search)
return erubis(template)

end

def get_buzz_words(lang_id, &block)
Expand Down
1 change: 0 additions & 1 deletion tss_server.rb
Expand Up @@ -21,7 +21,6 @@
require "tss_em_web_socket_server"


#WebSocket.debug = true
root_dir = File.dirname(File.expand_path(__FILE__))

opts = {
Expand Down
30 changes: 30 additions & 0 deletions views/default.css.erubis
Expand Up @@ -47,6 +47,36 @@ img {
margin-right: 100px;
}

.ustream-left-column {
float: left;
width: 480px;
overflow: hidden;
}

.ustream-right-column {
margin-left: 500px;
margin-right: 100px;
}

.video-container {
overflow: hidden;
z-index: 1;
}

.overlay-container {
position: absolute;
overflow: hidden;
}

.overlay {
position: absolute;
font-size: 120%;
font-weight: bold;
color: gray;
white-space: nowrap;
z-index: 2;
}

.buzz-nav {
float: right;
}
Expand Down

0 comments on commit 95b457f

Please sign in to comment.