Skip to content

Instantly share code, notes, and snippets.

@flavio
Created April 21, 2012 14:54
Show Gist options
  • Save flavio/2437530 to your computer and use it in GitHub Desktop.
Save flavio/2437530 to your computer and use it in GitHub Desktop.
jamendo downloader
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'json'
require 'ftools'
require 'cgi'
require 'optparse'
@download_dir = nil
@genre = nil
@top_num = nil
# move cursor to beginning of line
cr = "\r"
cwd = Dir.getwd
# ANSI escape code to clear line from cursor to end of line
# "\e" is an alternative to "\033"
# cf. http://en.wikipedia.org/wiki/ANSI_escape_code
clear = "\e[0K"
# reset lines
@reset = cr + clear
# if possible restore downloaded album_id array
begin
@downloaded = JSON.parse(File.new('downloaded.json').read.gsub(/\t|\r/,''))
rescue
@downloaded = []
end
# Add colorizing functionalities to String #####################################
class String
def red; colorize(self, "\e[1m\e[31m"); end
def green; colorize(self, "\e[1m\e[32m"); end
def dark_green; colorize(self, "\e[32m"); end
def yellow; colorize(self, "\e[1m\e[33m"); end
def dark_yellow; colorize(self, "\e[33m"); end
def blue; colorize(self, "\e[1m\e[34m"); end
def dark_blue; colorize(self, "\e[34m"); end
def pur; colorize(self, "\e[1m\e[35m"); end
def colorize(text, color_code) "#{color_code}#{text}\e[0m" end
end
################################################################################
# Functions ####################################################################
def download(genre,album,tracks_url)
artist_name = album['artist_name']
album_name = album['name']
album_id = album['id']
local_download_path = File.expand_path "#{@download_dir}/#{genre}/#{artist_name}/#{album_name}"
File.makedirs local_download_path
Dir.chdir local_download_path
print "\n\n"
puts "Going to download of \"#{album_name}\" by \"#{artist_name}\" finished".dark_blue
counter = 1
tracks_url.each do |url|
print "#{@reset}downloading #{counter}/#{tracks_url.size}"
$stdout.flush
open(CGI.unescape(url.split(/\?/).first.split(/\//).last),"wb").write(open(url).read)
counter +=1
end
@downloaded << album_id
puts "\nDownload finished".green
end
################################################################################
# parse command line options ###################################################
opt = OptionParser.new
opt.banner = "Usage: run_tests [options]"
opt.on( "-h", "--help", "Print this message" ) do
puts opt
exit
end
opt.on( "-g", "--genre", "=GENRE_NAME", "download music of the specified genre" ) do |val|
@genre = val
end
opt.on( "-d", "--download_dir", "=DONWLOAD_DIR", "place where the music files will be downloaded" ) do |val|
@download_dir = val
end
opt.on( "-t", "=TOP_NUM", "download only TOP_NUM album of the specified genre " ) do |val|
@top_num = val
end
begin
opt.parse!( ARGV )
rescue OptionParser::InvalidOption
puts $!
puts opt
exit
end
if ARGV.size > 0
puts "Too many arguments"
puts opt
exit
end
if @genre.nil?
puts 'You have to specify the genre'
puts opt
exit 1
end
if @top_num.nil?
puts 'You have to specify the top num param'
puts opt
exit 1
end
if @download_dir.nil?
@download_dir = "#{Dir.pwd}/downloaded_files"
puts 'You have not specified the download dir'
puts "Files will be download inside #{@download_dir}"
end
################################################################################
query_url = "http://api.jamendo.com/get2/id+name+artist_name/album/jsonpretty/?tag_idstr=#{@genre}&n=#{@top_num}&order=rating_desc"
query_url += "&idexclude=#{@downloaded.join('+')}" unless @downloaded.empty?
JSON.parse(open(query_url).read.gsub(/\t|\r/,'')).each do |album|
album_download_urls = "http://www.jamendo.com/get/track/id/album/audio/plain/#{album['id']}/"
tracks_url = []
open(album_download_urls).each do |track_url|
tracks_url << track_url.chomp
end
download(@genre, album, tracks_url)
end
Dir.chdir cwd
open('downloaded.json','w') { |f| f << @downloaded.to_json }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment