Skip to content

Instantly share code, notes, and snippets.

@danhigham
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save danhigham/a5135643a2d583c95efe to your computer and use it in GitHub Desktop.

Select an option

Save danhigham/a5135643a2d583c95efe to your computer and use it in GitHub Desktop.
Stream mixcloud mixes directly to MPlayer (requires nokogiri and mplayer)
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'net/http'
require 'uri'
require 'fileutils'
server_range = (13..20)
mplayer_folder = "#{ENV['HOME']}/.mplayer"
input_file = "#{mplayer_folder}/cmd.fifo" # << FIFO for sending commands to mplayer (http://www.mplayerhq.hu/DOCS/tech/slave.txt)
FileUtils.mkdir_p(mplayer_folder)
`mkfifo #{input_file}`
url = ARGV[0] # mix url e.g https://www.mixcloud.com/benxo/ben-xo-pixelated-raincloud-2015-05-05/
doc = Nokogiri::HTML(open(url))
meta_title = doc.search("meta[@property='og:title']").first.attributes["content"].value
puts "Finding ... #{meta_title}"
preview_btn = doc.search("span.play-button-cloudcast-page").first
return if preview_btn.nil?
preview_url = preview_btn.attributes["m-preview"].value
preview_url.gsub!('previews', 'c/originals')
preview_url.gsub!(/stream[0-9][0-9]/, "streamXX")
content_url = nil
server_range.each do |i|
url = preview_url.sub('streamXX', "stream#{i}")
uri = URI(url)
http = Net::HTTP.start(uri.host)
res = http.head(uri.path)
http.finish
if res.code == "200"
content_url = url
break
end
end
puts "No original found, trying downsampled"
if content_url.nil?
preview_url.gsub!('originals/', 'm4a/64/')
preview_url.gsub!('.mp3', '.m4a')
server_range.each do |i|
url = preview_url.sub('streamXX', "stream#{i}")
uri = URI(url)
http = Net::HTTP.start(uri.host)
res = http.head(uri.path)
http.finish
if res.code == "200"
content_url = url
break
end
end
end
return if content_url.nil?
puts "Playing #{content_url}"
mplayer_cmd = "wget #{content_url} -q -O - | mplayer -cache 8192 -input file=#{input_file} -"
exec mplayer_cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment