shithub: rc

ref: f3afdf563f784c0a5703496f48c1a72dff4617f4
dir: /bandcamp/

View raw version
#!/bin/rc
# data matches 'https://[^.]+\.bandcamp.com/(track|album)/[^ /]+'
# plumb start window -scroll bandcamp $data
if(! ~ $1 https://?*.bandcamp.com/^(track album)^/?*){
	echo 'usage: bandcamp https://artist.bandcamp.com/(track|album)/name' >[1=2]
	exit usage
}
{echo $1; hget $1} | awk '
	BEGIN{
		getline
		sub("^https:/", "/tmp/mp3")
		if(sub("\.bandcamp\.com/track", "/singles")){
			single = $0
			system("mkdir -p `{basename -d "single"}")
		}
		else{
			sub("\.bandcamp\.com/album", "")
			dir = $0
			system("mkdir -p "dir)
		}
		RS = "{"
	}
	!single && /"@type":"ListItem"/{n++}
	!single && match($0, /"@id":"[^"]+\/track\/[^\/"]+/){
		track[n] = substr($0, RSTART, RLENGTH)
		sub(".+/", "", track[n])
	}
	/"name":"file_mp3-128"/{
		match($0, /"https?:\/\/[^"]+/)
		if(single){
			src = substr($0, RSTART + 1, RLENGTH - 1)
			print "track: "single".mp3"
			system("hget ''"src"'' > "single".mp3")
		}
		else
			url[n] = substr($0, RSTART + 1, RLENGTH - 1)
	}
	match($0, /"text":"[^"]+/){
		lyr = substr($0, RSTART + 8, RLENGTH - 8)
		gsub(/\\"/, "\"", lyr)
		gsub(/\\r\\n/, "\n", lyr)
		if(!single)
			lyrics[n] = lyr
	}
	match($0, /<meta name="description" content="([^"]|\n)+/){
		info = substr($0, RSTART + 35, RLENGTH - 35)
		gsub("\r", "", info)
		gsub("&amp;#39;", "''", info)
		gsub("&amp;amp;", "\&", info)
	}
	match($0, /<a class="popupImage" href="[^"]+">/){
		src = substr($0, RSTART + 28, RLENGTH - 30)
		match(src, /\.[^.]+$/)
		if(single)
			art = single substr(src, RSTART, RLENGTH)
		else
			art = dir"/art"substr(src, RSTART, RLENGTH)
		print "art: "art
		system("hget ''"src"'' > "art)
	}
	END{
		if(single){
			out = single".txt"
			if(info){
				print "single info: "out
				print info > out
			}
			if(lyr){
				print "lyrics: "lyr
				print lyr >> out
			}
		}
		else{
			if(info){
				out = dir"/info.txt"
				print "album info: "out
				print info > out
			}
			if(n > 99) fmt = "%s/%03d_%s"
			else if(n > 9) fmt = "%s/%02d_%s"
			else fmt = "%s/%d_%s"
			for(i = 1; i <= n; i++){
				out = sprintf(fmt".mp3", dir, i, track[i])
				print "track: "out
				system("hget ''"url[i]"'' > "out)
				if(lyrics[i]){
					out = sprintf(fmt".txt", dir, i, track[i])
					print "lyrics: "out
					print lyrics[i] > out
				}
			}
		}
	}
'