-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbandcamp-mp3-dl
69 lines (53 loc) · 1.45 KB
/
bandcamp-mp3-dl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
require 'curb'
require 'nokogiri'
require 'ostruct'
require 'pry'
require 'securerandom'
require 'json'
require 'taglib'
require 'time'
require 'zaru'
def getnoko(curl_res)
Nokogiri::HTML.parse(curl_res.body_str)
end
def gethref(str, domain)
str.match(/^\//) ? "https://#{domain}#{str}" : str
end
url = ARGV[0].gsub("http://","https://")
puts url.inspect
page = getnoko(Curl.get(url))
mp3script = page.css('script').select{|x|
h = x.to_html;
h.include?('bcbits.com') && h.include?('data-tralbum')
}
tralbum = mp3script[0].attr('data-tralbum')
if !tralbum
tralbum = mp3script[0].inner_text
end
manifest = JSON.parse(tralbum)
album = manifest['current']['title']
time = Time.parse(manifest['current']['release_date'])
tracks = manifest['trackinfo']
list = tracks.each_with_index do |x, i|
next unless x['file']
title = x['title']
url = x['file']['mp3-128']
trackNum = i + 1
#filepath = trackNum.to_s.rjust(2, "0") + " - " + title.gsub(/[^,?a-zA-Z0-9 _'"-]/, '') + ".mp3"
filepath = trackNum.to_s.rjust(2, "0") + " - " + Zaru.sanitize!(title) + ".mp3"
puts "Downloading #{title} to #{filepath} from #{url} ..."
Curl::Easy.download(url, filepath)
# Tag
tag = TagLib::FileRef.open(filepath) do |file|
unless file.null?
tag = file.tag
tag.title = title
tag.artist = manifest['artist']
tag.album = album
tag.track = trackNum
tag.year = time.year
file.save
end
end
end