Skip to content

Instantly share code, notes, and snippets.

@danhigham
Last active December 29, 2015 18:09

Revisions

  1. danhigham revised this gist Nov 29, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dl_mixcloud_cast.rb
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ def percent_of(n)
    f = File.open("#{mix_name}.m4a", "wb")

    begin
    (20..30).each do |x|
    (15..30).each do |x|

    puts "Trying stream#{x}.mixcloud.com"

  2. danhigham revised this gist Nov 29, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions dl_mixcloud_cast.rb
    Original file line number Diff line number Diff line change
    @@ -72,6 +72,4 @@ def percent_of(n)
    f.close
    end


    puts preview_url
    end
  3. danhigham revised this gist Nov 29, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    source 'http://rubygems.org'

    gem 'httparty'
    gem 'nokogiri'
  4. danhigham revised this gist Nov 29, 2013. 1 changed file with 17 additions and 2 deletions.
    19 changes: 17 additions & 2 deletions dl_mixcloud_cast.rb
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,12 @@
    require 'nokogiri'
    require 'open-uri'

    class Numeric
    def percent_of(n)
    self.to_f / n.to_f * 100.0
    end
    end

    user = ARGV[0]

    response = HTTParty.get("http://api.mixcloud.com/#{user}/cloudcasts/")
    @@ -39,12 +45,17 @@
    http.request_get(stream_url) do |resp|

    if resp.code != "404"
    content_length = resp['Content-Length'].to_i
    done = 0

    puts "Writing to #{mix_name}.m4a"
    puts "Writing to #{mix_name}.m4a (#{content_length} bytes)"

    resp.read_body do |segment|
    f.write(segment)
    print "."
    done = done + segment.length
    print "#{done} bytes - "
    print "%.2f%%" % done.percent_of(content_length)
    print 13.chr
    end

    puts "Done!"
    @@ -55,8 +66,12 @@
    end
    end


    end
    ensure
    f.close
    end


    puts preview_url
    end
  5. danhigham created this gist Nov 29, 2013.
    62 changes: 62 additions & 0 deletions dl_mixcloud_cast.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    #!/usr/bin/env ruby

    require 'httparty'
    require 'json'
    require 'nokogiri'
    require 'open-uri'

    user = ARGV[0]

    response = HTTParty.get("http://api.mixcloud.com/#{user}/cloudcasts/")
    mixes = JSON.parse(response.body)

    mixes["data"].each do |m|
    mix_url = m["key"]

    mix_response = HTTParty.get("http://api.mixcloud.com#{mix_url}")
    mix = JSON.parse(mix_response.body)

    url = mix["url"]
    mix_name = mix["name"]

    doc = Nokogiri::HTML(open(url))
    preview_url = doc.css('//*[@id="player-play"]').first.attributes["data-preview-url"].value

    puts "Preview : #{preview_url}"

    stream_id = preview_url.scan(/previews([^\.]+)/)[0][0]
    stream_url = "/cloudcasts/m4a/64#{stream_id}.m4a"

    f = File.open("#{mix_name}.m4a", "wb")

    begin
    (20..30).each do |x|

    puts "Trying stream#{x}.mixcloud.com"

    Net::HTTP.start("stream#{x}.mixcloud.com") do |http|

    http.request_get(stream_url) do |resp|

    if resp.code != "404"

    puts "Writing to #{mix_name}.m4a"

    resp.read_body do |segment|
    f.write(segment)
    print "."
    end

    puts "Done!"

    break
    end

    end
    end

    end
    ensure
    f.close
    end
    end