Skip to content

Commit

Permalink
refacto + stream ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangenieur committed Dec 16, 2012
2 parents 52b8baa + 5b12250 commit 29624e9
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 110 deletions.
3 changes: 1 addition & 2 deletions README.md
@@ -1,5 +1,4 @@
AngelHack Fall 2012
hvid.io
=======

Hvid.io
4th place at AngelHack Paris Fall 2012
5 changes: 0 additions & 5 deletions public/javascripts/eventemitter.min.js

This file was deleted.

83 changes: 51 additions & 32 deletions public/javascripts/hvidio.js
Expand Up @@ -138,6 +138,7 @@
hvidio.fetch(keyword, function(data) {
$main.addClass('large');

console.log(data[0].msgs.length, data[0].provider, data[0].msgs[0].provider, data[0].msgs[0].text );
hvidio.templatize('#videosTemplate', { search: urlify(keyword), videos: data }, '#results');

//hvidio.loading(false);
Expand All @@ -153,79 +154,97 @@
return this;
},

order: function(videos) {
console.log("order", videos, this.keyword, $('#results'));
hvidio.templatize('#videosTemplate', { search: urlify(this.keyword), videos: videos }, '#results');
hvidio.loading(false);
hvidio.play(videos[0].embed);
hvidio.initScroller(true);
$close.fadeIn(5000);
insert_video: function(container, pos, video) {
var $html = $(hvidio.templatize('#videoTemplate', { video: video }));

elem = $(container + " > li:eq("+pos+")")
if ( elem.length ) {
elem.before($html);
} else {
$(container + " > li:eq("+(pos - 1)+")").after($html);
}
$html.css('visibility','visible').hide().fadeIn('fast');
},
get_video_score: function(video) {
//return - video.msgs.length;
return - (new Date(video.date)).valueOf();
},
fetch: function(keyword, callback) {
var self = this;

obs = new OrderByScore();
var videos = {};

search = Search(keyword)
.on("video.new", function() {
.reduce(function(video) {
})
.on("video.new", function(video) {
var pos;
this.msg = this.msgs[0];
this.score = _.reduce(this.msgs, function(memo, num) {
video.msg = video.msgs[0];
/*
video.score = _.reduce(video.msgs, function(memo, num) {
return (memo + (parseInt(num.votes) + 1)) || 1;
}, 0);
*/
video.score = self.get_video_score(video);
var pos = obs.get_pos(video.score);

this.date = this.msgs[0].post_date;
video.date = video.msgs[0].post_date;

if ((pos = this.embed.indexOf("?")) != -1) {
this.embed = this.embed.substr(0, pos);
if ((pos = video.embed.indexOf("?")) != -1) {
video.embed = video.embed.substr(0, pos);
}

$('#counter').text(counter++);

if (typeof search.initiated == "undefined") {
search.initiated = true;
scroll = false;
callback([this]);

callback([video]);
} else {
var html = hvidio.templatize('#videoTemplate', { video: this });
var $mylist = $("#video-list-" + urlify(keyword));
//$list.append(html);
/*
$mylist.append(html);
$(html).fadeIn()
*/
var $html = $(html);

$mylist.append($html);
self.insert_video("#video-list-" + urlify(keyword), pos, video);

hvidio.initScroller();

$html.css('visibility','visible').hide().fadeIn('slow');
}

}).on("video.update", function() {
console.log("update", this.dom_id);
var $tip = $('#' + this.dom_id + ' .tip'),
}).on("video.update", function(video) {
var tmp_score = self.get_video_score(video);
if (tmp_score != video.score) {
obs.remove_score(video.score);
video.score = tmp_score
var pos = obs.get_pos(video.score);
if ($("#video-list-" + urlify(keyword) + " > li:eq("+pos+")").attr("id") != video.dom_id) {
console.log("score != ?", tmp_score, video.score)
$('#' + video.dom_id).fadeOut().remove()
self.insert_video("#video-list-" + urlify(keyword), pos, video);
}
}

console.log("update", video.dom_id, video.provider, video.msgs[0].provider, video.msgs.length, video.msgs[0].text);
var $tip = $('#' + video.dom_id + ' .tip'),
score = parseInt($tip.text()) || 1,
newScore = score + 1; //(this.msgs[this.msgs.length - 1].votes || 1);
newScore = score + 1; //(video.msgs[video.msgs.length - 1].votes || 1);

$tip.text(newScore + '+');
$tip.addClass('incremented animated bounce');

}).on("finished", function() {
console.log("FINISHED");
hvidio.loading(false);
/*
self.order(
search.videos_by_date()
//search.videos_by_posts()
);
*/
});

return this;
},

convertId: function(id) {
return id.replace('/', '-');
},

templatize: function(template, data, output) {
var tmpl = $(template).html(),
html = _.template(tmpl, data );
Expand Down
94 changes: 35 additions & 59 deletions public/javascripts/live-search.coffee
@@ -1,82 +1,58 @@
window.VideoOrder = class OrderStream
constructor: (@videos) ->
set_order:
window.OrderByScore = class OrderByScore
constructor: () ->
@scores = []
@scores_hit = {}

get_pos: (score) ->
pos = _(@scores).sortedIndex score
unless @scores[pos]
@scores[pos] = score
else if (@scores[pos] != score) and (@scores[pos+1] != score)
@scores.splice(pos, 0, score)

@scores_hit[score] or= 0
@scores_hit[score]++

console.log "get_pos", score, pos#, @scores
pos

remove_score: (score) ->
@scores_hit[score]--
if @scores_hit[score] == 0
@scores = _(@scores).without score

window.SearchStream = class SearchStream

window.Search = class SearchStream
instances = {}

constructor: (search_term, opts) ->
if @constructor.name is "Search"
if @constructor.name is "SearchStream"
@search_term = search_term
@videos = {}
@events or= []
@ee = new EventEmitter2()

my_timer = null
search_me = =>
unless Search.socket?.emit?
unless SearchStream.socket?.emit?
my_timer = setInterval(search_me, 100) unless my_timer
else
clearInterval(my_timer) if my_timer
finished = _(@events).filter((e) -> e.finished?).pop().finished
Search.socket.emit "search", @search_term, finished
SearchStream.socket.emit "search", @search_term, =>
@ee.emit "finished"

_.defer search_me

#instances[search_term] = @
else
return instances[search_term] or= new Search(search_term, opts)

video_reduce: (video) ->
msg = video.msg
delete video.msg
video.dom_id = video.id.replace "/", "-"
video.msgs = []
(@videos[video.id] or= video).msgs.push msg
console.log "videos ", Object.keys(@videos).length
@when _(@videos).keys().length, video.id

videos_by_posts: ->
_(@videos).sortBy (v) -> - v.msgs.length

videos_by_date: ->
_(@videos).sortBy (v) -> - (new Date(v.date)).valueOf()

videos_ids: -> _(@videos).keys()

when: (num, cb) ->
if _.isFunction cb
@events.push
num: num
callback: cb
else if @events.length > 0
for i in [0..@events.length - 1]
if @events[i]?.num? and @events[i].num == num
cb = @events[i].callback
@events.splice i, 1
@when_done = true
cb.call @
else if @when_done and @events[i]?["video.new"]?
if (not @last_videos_length) or (@last_videos_length < num)
@events[i]["video.new"].call @videos[cb]
else if @when_done and @events[i]?["video.update"]?
if (not @last_videos_length) or (@last_videos_length == num)
@events[i]["video.update"].call @videos[cb]
@last_videos_length = num
@
return instances[search_term] or= new SearchStream(search_term, opts)

on: (msg, cb) ->
if _.isFunction cb
event = {}
event[msg] = cb
@when_done = true unless @events.length
@events.push event
@
on: (msg, cb) -> @ee.on msg, cb
emit: (msg, data...) -> @ee.emit msg, data...

@com_init: (socket) ->
console.log "handling init"
socket.on "search_result", (res) =>
@get(res.search_term)?.video_reduce video for video in res.videos
@get(res.search_term)?.ee.emit "data", res.data

Search.socket = socket
SearchStream.socket = socket

@get: (search_term) -> instances[search_term]

2 changes: 1 addition & 1 deletion public/stylesheets/style.css
Expand Up @@ -149,7 +149,7 @@ p.tagline {
position: relative;
}
#results .video {
visibility: hidden;
/*visibility: hidden;*/
float: left;
margin: 10px 10px;
min-height: 200px;
Expand Down
2 changes: 1 addition & 1 deletion public/stylesheets/style.less
Expand Up @@ -178,7 +178,7 @@ p.tagline {
}

.video {
visibility: hidden;
//visibility: hidden;
float: left;
margin: 10px 10px;
min-height: @cell-height;
Expand Down
4 changes: 1 addition & 3 deletions server.coffee
Expand Up @@ -28,9 +28,7 @@ require('zappajs') ->
message: (video) =>
@emit search_result:
search_term: @data
videos: [
video
]
data: video
exit: ->
console.log "exit #{worker}"
deferred.resolve()
Expand Down
2 changes: 1 addition & 1 deletion views/index.jade
Expand Up @@ -42,7 +42,7 @@ block content
| <h5 title="<%= video.title %>"><%= video.title || 'Unknown title' %></h5>
| <ul>
| <li>
| <time datetime="<%= video.msgs[0].post_date %>"><%= video.date %></time>
| <time datetime="<%= video.date %>" class="timeago"><%= video.date %></time>
| <br>
| from <a class="from" href="<%= video.url %>"><%= video.msgs[0].provider %></a>
| </li>
Expand Down
10 changes: 5 additions & 5 deletions views/layout.jade
Expand Up @@ -10,17 +10,17 @@ html
body
block content

//script(src='/javascripts/coffee-script.min.js')

script(src='/socket.io/socket.io.js')
script(src='/javascripts/eventemitter.min.js')
script(src='/javascripts/eventemitter2.js')
script(src='/javascripts/jquery.min.js')
script(src='/javascripts/jquery.timeago.min.js')
script(src='/javascripts/jquery.mousewheel.min.js')
script(src='/javascripts/jquery.ui.min.js')
script(src='/javascripts/jquery.mcustomscrollbar.min.js')
script(src='/javascripts/underscore.min.js')
script(src='/javascripts/canvasloader.min.js')
script(src='/javascripts/live-search.js')
//script(src='/javascripts/live-search.coffee',type="text/coffeescript")
script(src='/javascripts/hvidio.js')
//script(src='/javascripts/live-search.js')
script(src='/javascripts/coffee-script.min.js')
script(src='/javascripts/live-search.coffee',type="text/coffeescript")
script(src='/javascripts/hvidio.coffee',type="text/coffeescript")
2 changes: 1 addition & 1 deletion workers/twitter_search_worker.coffee
Expand Up @@ -14,7 +14,7 @@ tc = new ntwitter
video_search = (search, opts = {}) ->
_(opts).defaults
include_entities: on
rpp: 50
rpp: 20
result_type: "recent"
#lang: "fr"

Expand Down

0 comments on commit 29624e9

Please sign in to comment.