Skip to content

Instantly share code, notes, and snippets.

@aont
Last active March 1, 2019 05:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aont/5331237 to your computer and use it in GitHub Desktop.
Save aont/5331237 to your computer and use it in GitHub Desktop.
modify cdrdao's TOC file using Gracenote Database
#!/usr/bin/ruby
require 'rexml/document'
require "net/https"
require "uri"
def to_frame(min, sec, f)
return (min*60+sec)*75+f
end
def query(doc)
https = Net::HTTP::new("cXXXXXXX.web.cddbp.net", 443)
https.use_ssl = true
https.start
res = https.post("/webapi/xml/1.0/", doc.to_s)
https.finish
return REXML::Document.new res.body
end
def add_auth(queries)
auth = queries.add_element("AUTH")
auth.add_element("CLIENT").text = "XXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth.add_element("USER").text = "XXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end
def is_res_ok(res)
res.elements["RESPONSES/RESPONSE"].attributes["STATUS"] == "OK"
end
def album_toc(toc)
doc = REXML::Document::new
queries = doc.add_element("QUERIES")
add_auth(queries)
query = queries.add_element "QUERY"
query.attributes["CMD"]="ALBUM_TOC"
query.add_element("MODE").text = "SINGLE_BEST"
query.add_element("TOC").add_element("OFFSETS").text = toc.join(" ")
# option = query.add_element("OPTION")
# option.add_element("PARAMETER").text = "SELECT_EXTENDED"
# option.add_element("VALUE").text = "COVER,REVIEW,ARTIST_BIOGRAPHY,ARTIST_IMAGE"
return doc
end
def toc_cdtext(toc_str, album, out)
tracknum = 0
toc_str.lines do |line|
if (line=~/^FILE/)
out << "CD_TEXT {\n"
out << " LANGUAGE 0 {\n"
track = album.elements["./TRACK[#{tracknum}]"]
title = track.elements['./TITLE']
out << " TITLE \"#{title.text}\"\n" if title
artist = track.elements['./ARTIST']
out << " PERFORMER \"#{artist.text}\"\n" if artist
out << " }\n"
out << "}\n"
end
out << line
if (line=~/^CATALOG/)
out << "CD_TEXT {\n"
out << " LANGUAGE_MAP {\n"
out << " 0: 9\n"
out << " }\n"
out << " LANGUAGE 0 {\n"
title = album.elements['./TITLE']
out << " TITLE \"#{title.text}\"\n" if title
artist = album.elements['./ARTIST']
out << " PERFORMER \"#{artist.text}\"\n" if artist
out << " }\n"
out << "}\n"
end
if (line=~/^TRACK AUDIO/)
tracknum += 1
end
end
end
def fget_toc(str)
frame_ary = []
last_frame = nil
str.lines do |line|
if ( line=~/^FILE\s("[^"]*")\s([^\s]+)\s([^\s]+)$/ )
fn=$1
start_str=$2
length_str=$3
start_frame = 150 +
if(start_str=~/(\d*):?(\d*):?(\d*)/)
to_frame($1.to_i, $2.to_i, $3.to_i)
end
frame_ary << start_frame
length_frame =
if(length_str=~/(\d*):?(\d*):?(\d+)/)
to_frame($1.to_i, $2.to_i, $3.to_i)
end
last_frame = start_frame + length_frame
end
end
frame_ary << last_frame
return frame_ary
end
toc_str = $stdin.read
res_toc = query(album_toc(fget_toc(toc_str)))
throw if !is_res_ok(res_toc)
album = res_toc.elements["RESPONSES/RESPONSE/ALBUM"]
toc_cdtext(toc_str, album, $stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment