Skip to content

Commit

Permalink
Sometimes I feel that I am really quite dense
Browse files Browse the repository at this point in the history
  • Loading branch information
melito committed Jan 7, 2010
1 parent c339e4f commit 27929f3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 50 deletions.
8 changes: 1 addition & 7 deletions groovedown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ class App < Sinatra::Base

get "/songs/:id" do
@stream = Stream.new(params[:id])
if data = @stream.get
content_type "mp3"
attachment "#{params[:name]||params[:id]}.mp3"
halt data.to_s
else
"They don't like us. :("
end
redirect "/songs/#{@stream.key}/#{@stream.server}"
end

end
Expand Down
46 changes: 3 additions & 43 deletions models/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# We shouldn't depend on ruby to do the chunking, proxing, and handling seeks.
# Just grab a stream and forward on to the client. Digging into HAproxy guts :-\
class Stream
attr_reader :song_id
attr_reader :song_id, :server, :key

def initialize(song_id)
@song_id = song_id
Expand All @@ -13,48 +13,8 @@ def initialize(song_id)
:content_type => "application/json")

@stream_result = JSON.parse(response)["result"]["result"]
@stream_key = @stream_result["streamKey"]
@stream_server = @stream_result["streamServer"]
@stream_url = "http://#{@stream_server}/stream.php"
end

def get
if @stream_key
RestClient.post(@stream_url, :steamKey => @stream_key, :content_type => "application/x-www-form-urlencoded")
end
end

def length
@length.to_s ||= "0"
end

def each
url = URI.parse(@stream_url)
req = Net::HTTP::Post.new(url.path)
req.set_form_data({'streamKey' => @stream_key})

#http = Net::HTTP.new(url.host, url.port)
#puts "curl -v -d 'streamKey=#{@stream_key}' #{@stream_url}"
#http.post(url.path, "streamKey=#{@stream_key}") do |chunk|
# @length = 0 if @length.nil?
# @length = chunk.size+4096
# #p @length
# p chunk.content_length
# yield chunk
#end

Net::HTTP.new(url.host, url.port).start do |http|
http.request(req) do |res|
@length = res.content_length

res.read_body do |chunk|
@length = @length# - chunk.size
yield chunk.to_s
end

end
end

@key = @stream_result["streamKey"]
@server = @stream_result["streamServer"]
end

end
11 changes: 11 additions & 0 deletions views/search.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div id="results">
<div class="toolbar">
<h1 id="search_results">Search Results</h1>
<a href='#' class='button back'>Back</a>
</div>
<ul>
<% for result in @results %>
<li><%= link_to result.name, "/songs/#{result.song_id}" %></li>
<% end %>
</ul>
</div>

0 comments on commit 27929f3

Please sign in to comment.