Skip to content

Instantly share code, notes, and snippets.

@s2t2
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2t2/2a24d01976b7aab74b27 to your computer and use it in GitHub Desktop.
Save s2t2/2a24d01976b7aab74b27 to your computer and use it in GitHub Desktop.
download tracks from the hype machine

hypem-downloader-ruby

Installation

git clone https://gist.github.com/s2t2/2a24d01976b7aab74b27
cd 2a24d01976b7aab74b27
bundle install

Usage

Download songs from the popular page.

ruby tracks.rb

Gem usage (in development).

Hypem::Tracks.download(:from_url => "http://hypem.com/artist/Sia/1/?sortby=favorite")
Hypem::Tracks.download(:from_url => "http://hypem.com/popular")

Disclaimer

This script is for educational/theoretical purposes. Don't use if in violation of hype machine terms of service.

Reference

source 'https://rubygems.org'
gem 'httparty'
gem 'nokogiri'
gem 'json'
group :development, :test do
gem 'pry'
end
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.0)
httparty (0.13.3)
json (~> 1.8)
multi_xml (>= 0.5.2)
json (1.8.2)
method_source (0.8.2)
mini_portile (0.6.2)
multi_xml (0.5.5)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
slop (3.6.0)
PLATFORMS
ruby
DEPENDENCIES
httparty
json
nokogiri
pry
module Hypem
class MockObjects
def serve_source
###response = <<-JSON
### {"itemid":"1zht4","type":"SC","url":"http:\/\/api.soundcloud.com\/tracks\/113324557\/stream?consumer_key=nH8p0jYOkoVEZgJukRlG6w"}
###JSON
###JSON.parse(response) #> {"itemid"=>"1zht4", "type"=>"SC", "url"=>"http://api.soundcloud.com/tracks/113324557/stream?consumer_key=nH8p0jYOkoVEZgJukRlG6w"}
{"itemid"=>"1zht4", "type"=>"SC", "url"=>"http://api.soundcloud.com/tracks/113324557/stream?consumer_key=nH8p0jYOkoVEZgJukRlG6w"}
end
def track
{"type"=>"normal", "id"=>"1zht4", "time"=>260, "ts"=>1387207295, "postid"=>2378176, "posturl"=>"http://www.indieshuffle.com/best-artist-collaborations-2013/", "fav"=>0, "key"=>"5373d0eae8c78b030cccfcb0f95baa80", "artist"=>"Sia", "song"=>"Elastic Heart feat. The Weeknd & Diplo", "is_sc"=>true, "is_bc"=>false}
end
def track_list
{
"page_cur"=>"/artist/Sia/1/?sortby=favorite",
"page_num"=>"1",
"tracks"=>[
{
"type"=>"normal",
"id"=>"1zht4",
"time"=>260,
"ts"=>1387207295,
"postid"=>2378176,
"posturl"=>"http://www.indieshuffle.com/best-artist-collaborations-2013/",
"fav"=>0,
"key"=>"5373d0eae8c78b030cccfcb0f95baa80",
"artist"=>"Sia",
"song"=>"Elastic Heart feat. The Weeknd & Diplo",
"is_sc"=>true,
"is_bc"=>false
},
{
"type"=>"normal",
"id"=>"235dm",
"time"=>216,
"ts"=>1422756847,
"postid"=>2613089,
"posturl"=>"http://www.cougarmicrobes.com/2015/01/cm-top-albums-2014-sia-1000-forms-fear/",
"fav"=>0,
"key"=>"be476e99c1a3dddf5f2db1bf1c20443c",
"artist"=>"Sia",
"song"=>"Chandelier",
"is_sc"=>true,
"is_bc"=>false
}
],
"page_name"=>"search",
"page_mode"=>"artist",
"page_arg"=>"Sia",
"page_sort"=>"favorite",
"title"=>"Sia / mp3s and music from the best music blogs",
"page_next"=>"/artist/Sia/2?sortby=favorite"
}
end
end
end
require 'httparty'
require 'nokogiri'
require 'json'
require 'pry'
module Hypem
class Tracks
def self.download(options = {})
page_url = options[:from_url] || "http://hypem.com/popular"
downloads_dir = options[:to_dir] || "#{Dir.getwd}/tracks" # "#{Dir.home}/Desktop/hypem_downloads"
binding.pry
# Create downloads directory.
FileUtils.mkdir_p(downloads_dir)
# Get track list.
page_source = HTTParty.get(page_url).parsed_response
document = Nokogiri::HTML(page_source)
track_list_data_element = document.at_css("#displayList-data")
track_list = JSON.parse(track_list_data_element)
tracks = track_list["tracks"]
tracks.each do |track|
begin
file_name = "#{track["song"]} by #{track["artist"]}.mp3"
# Get track source url.
serve_source_url = "http://hypem.com/serve/source/#{track["id"]}/#{track["key"]}"
serve_source = HTTParty.get(serve_source_url).parsed_response
raise "REJECTED -- #{file_name}" if serve_source.include?("You are not authorized")
track_source_url = serve_source["url"]
# Download file from track source.
destination_file = "#{downloads_dir}/#{destination_file}"
File.write(destination_file, track_source_url)
rescue => e
puts "#{e.class} -- #{e.message}"
next
end
end
end
end
end
Hypem::Tracks.download(:from_url => "http://hypem.com/popular")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment