scripts

misc scripts and tools
git clone git://git.2f30.org/scripts.git
Log | Files | Refs

banddump (1477B)


      1 #!/usr/bin/env ruby
      2 
      3 # generate release metadata info from a bandcamp page
      4 
      5 require 'open-uri'
      6 require 'yaml'
      7 
      8 if ARGV.empty?
      9     puts 'usage: banddump url'
     10     exit
     11 end
     12 url = ARGV[0]
     13 
     14 # convert the album data js struct into json data
     15 def get_album_data_json page
     16     got_in = false
     17     json = ''
     18     page.lines do |l|
     19         if l.match(/^var TralbumData = {$/)
     20             got_in = true
     21             json << '{'
     22             next
     23         end
     24         if got_in
     25             if l.match(/^};$/)
     26                 json << '}'
     27                 break
     28             end
     29             if l.match(/^\s*\/\/.*/) # js line comment
     30                 next
     31             end
     32             l = l.gsub('" + "', '') # make runtime concat static
     33             json << l
     34         end
     35     end
     36     json
     37 end
     38 
     39 page = open(url).read
     40 data = YAML.load(get_album_data_json page)
     41 #puts data.to_yaml
     42 
     43 rel = {}
     44 rel['artist'] = data['artist']
     45 rel['album'] = data['current']['title']
     46 rel['year'] = Date.parse(data['current']['new_date']).year
     47 rel['masteryear'] = rel['year']
     48 rel['genre'] = nil
     49 rel['format'] = 'MP3'
     50 rel['comment'] = "Bandcamp: #{data['id']}"
     51 rel['imgurl'] = data['artFullsizeUrl']
     52 rel['tracklist'] = []
     53 data['trackinfo'].each do |s|
     54     trk = {}
     55     trk['pos'] = s['track_num']
     56     trk['title'] = s['title']
     57     trk['dl'] = s['file']['mp3-128'] if s['file']
     58     rel['tracklist'] << trk
     59 end
     60 
     61 puts rel.to_yaml
     62 
     63 # output download links as separate document
     64 puts '---'
     65 puts rel['tracklist'].collect {|s| s['dl']}