Class: Freedb
- Inherits:
-
Object
- Object
- Freedb
- Defined in:
- lib/freedb.rb,
ext/freedb_cdrom/freedb_cdrom.c
Defined Under Namespace
Classes: FetchCGI, FetchDisk, FetchNet
Constant Summary collapse
- VERSION =
"0.6"
- PROTO_LEVEL =
5
- CD_FRAME =
75
- VALID_CATEGORIES =
[ "blues", "classical", "country", "data", "folk", "jazz", "misc", "newage", "reggae", "rock", "soundtrack" ]
Instance Attribute Summary collapse
-
#artist ⇒ Object
artist of the CD, must not be empty.
-
#category ⇒ Object
freedb category, must be one of
Freedb::VALID_CATEGORIES
. -
#discid ⇒ Object
readonly
cddbid of the CD.
-
#ext_infos ⇒ Object
extended infos of the CD.
-
#genre ⇒ Object
arbitraty string for the genre.
-
#length ⇒ Object
readonly
total length of the CD.
-
#query ⇒ Object
readonly
the complete string used to query the database.
-
#raw_response ⇒ Object
readonly
string containing raw entry from freedb database.
-
#results ⇒ Object
readonly
an array with all possible results for this CD.
-
#title ⇒ Object
title of the CD, must not be empty.
-
#tracks ⇒ Object
an array of hashs containing following keys: “title” (must not be empty), “length”, “ext” (for extended infos).
-
#year ⇒ Object
year of the cd (0 if not known).
Instance Method Summary collapse
-
#close ⇒ Object
close all pending connections.
-
#fetch_cgi(server = "www.freedb.org", port = 80, proxy = nil, proxy_port = nil, path = "/~cddb/cddb.cgi") ⇒ Object
Query database using CGI (HTTP) method.
-
#fetch_disk(directory, win_format = false) ⇒ Object
Query database using local directory.
-
#fetch_net(server = "freedb.org", port = 8880) ⇒ Object
(also: #fetch)
Query database using network Fill the
results
array with multiple results. -
#get_result(index) ⇒ Object
Retrieve full result from the database.
-
#initialize(param = "/dev/cdrom", is_query = false) ⇒ Freedb
constructor
If
is_query
is false, the discid of the CD inparam
is dumped. -
#submit_http(from = "user@localhost", server = "freedb.org", port = 80, path = "/~cddb/submit.cgi", submit_mode = "submit") ⇒ Object
(also: #submit)
submit the current Freedb object using http
from
is an email adress used to return submissions errorssubmit_mode
can be set to “test” to check submission validity (for developpers) return nil. -
#submit_mail(smtp_server, from = "localuser@localhost", port = 25, to = "[email protected]") ⇒ Object
submit the current Freedb object using smtp return
nil
.
Constructor Details
#initialize(param = "/dev/cdrom", is_query = false) ⇒ Freedb
If is_query
is false, the discid of the CD in param
is dumped. Else param
is considered as a valid freedb query string and is used directly.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/freedb.rb', line 182 def initialize(param = "/dev/cdrom", is_query = false) @query = if is_query param else require "freedb_cdrom/freedb_cdrom" get_cdrom(param) end q = @query.split(" ") @discid = q[0] nb_tracks = q[1].to_i @length = q[-1].to_i @offsets = q[2...-1] << @length*CD_FRAME @offsets.collect! { |x| x.to_i } @tracks = Array.new nb_tracks.times { |i| t = Hash.new t["length"] = ((@offsets[i+1]-@offsets[i]).to_f/CD_FRAME).round @tracks << t } @revision = 0 @raw_response = "" end |
Instance Attribute Details
#artist ⇒ Object
artist of the CD, must not be empty
159 160 161 |
# File 'lib/freedb.rb', line 159 def artist @artist end |
#category ⇒ Object
freedb category, must be one of Freedb::VALID_CATEGORIES
165 166 167 |
# File 'lib/freedb.rb', line 165 def category @category end |
#discid ⇒ Object (readonly)
cddbid of the CD
144 145 146 |
# File 'lib/freedb.rb', line 144 def discid @discid end |
#ext_infos ⇒ Object
extended infos of the CD
178 179 180 |
# File 'lib/freedb.rb', line 178 def ext_infos @ext_infos end |
#genre ⇒ Object
arbitraty string for the genre
168 169 170 |
# File 'lib/freedb.rb', line 168 def genre @genre end |
#length ⇒ Object (readonly)
total length of the CD
150 151 152 |
# File 'lib/freedb.rb', line 150 def length @length end |
#query ⇒ Object (readonly)
the complete string used to query the database
147 148 149 |
# File 'lib/freedb.rb', line 147 def query @query end |
#raw_response ⇒ Object (readonly)
string containing raw entry from freedb database
156 157 158 |
# File 'lib/freedb.rb', line 156 def raw_response @raw_response end |
#results ⇒ Object (readonly)
an array with all possible results for this CD
153 154 155 |
# File 'lib/freedb.rb', line 153 def results @results end |
#title ⇒ Object
title of the CD, must not be empty
162 163 164 |
# File 'lib/freedb.rb', line 162 def title @title end |
#tracks ⇒ Object
an array of hashs containing following keys: “title” (must not be empty), “length”, “ext” (for extended infos)
175 176 177 |
# File 'lib/freedb.rb', line 175 def tracks @tracks end |
#year ⇒ Object
year of the cd (0 if not known)
171 172 173 |
# File 'lib/freedb.rb', line 171 def year @year end |
Instance Method Details
#close ⇒ Object
close all pending connections
354 355 356 357 |
# File 'lib/freedb.rb', line 354 def close @handler.close if @handler @handler = nil end |
#fetch_cgi(server = "www.freedb.org", port = 80, proxy = nil, proxy_port = nil, path = "/~cddb/cddb.cgi") ⇒ Object
Query database using CGI (HTTP) method. Fill the results
array with multiple results. return nil if no match found
222 223 224 225 |
# File 'lib/freedb.rb', line 222 def fetch_cgi(server = "www.freedb.org", port = 80, proxy = nil, proxy_port = nil, path = "/~cddb/cddb.cgi") @handler = FetchCGI.new(server, port, proxy, proxy_port, path) _fetch end |
#fetch_disk(directory, win_format = false) ⇒ Object
Query database using local directory. Set win_format
to true if the database has windows format (see freedb howto in “misc/” for details) return nil if no match found
230 231 232 233 |
# File 'lib/freedb.rb', line 230 def fetch_disk(directory, win_format = false) @handler = FetchDisk.new(directory, win_format) _fetch end |
#fetch_net(server = "freedb.org", port = 8880) ⇒ Object Also known as: fetch
Query database using network Fill the results
array with multiple results. return nil if no match found
212 213 214 215 |
# File 'lib/freedb.rb', line 212 def fetch_net(server = "freedb.org", port = 8880) @handler = FetchNet.new(server, port) _fetch end |
#get_result(index) ⇒ Object
Retrieve full result from the database. If index
is a Fixnum, get the index
‘th result in the result
array If index
is a String, index
is the freedb category
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/freedb.rb', line 287 def get_result(index) if index.is_a?(String) idx = nil @results.each_with_index { |r, i| if r =~ /^#{index}/ idx = i end } else idx = index end md = /^\S+ [0-9a-fA-F]{8}/.match(@results[idx]) @handler.send_cmd("read", md[0]) # swallow the whole response into a hash response = Hash.new each_line(@handler) { |line| @raw_response << line + "\n" case line when /^(\d+) (\S+)/, /^([A-Za-z0-9_]+)=(.*)/ key = $1.upcase val = $2.gsub(/\\(.)/) { case $1 when "t" "\t" when "n" "\n" else $1 end } (response[key] ||= '') << val when /^# Revision: (\d+)/ @revision = $1.to_i end } @category = response['210'] @genre = response['DGENRE'] @year = response['DYEAR'].to_i @ext_infos = response['EXTD'] # Use a regexp instead of a bare string to avoid ruby >= 1.7 warning @artist, @title = response['DTITLE'].split(/ \/ /, 2) # A self-titled album may not have a title part @title ||= @artist response.each { |key, val| case key when /^4\d\d$/ raise(FreedbError, val) when /^TTITLE(\d+)$/ i = $1.to_i @tracks[i]["title"] = val when /^EXTT(\d+)$/ i = $1.to_i @tracks[i]["ext"] = val end } self end |
#submit_http(from = "user@localhost", server = "freedb.org", port = 80, path = "/~cddb/submit.cgi", submit_mode = "submit") ⇒ Object Also known as: submit
submit the current Freedb object using http from
is an email adress used to return submissions errors submit_mode
can be set to “test” to check submission validity (for developpers) return nil
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/freedb.rb', line 239 def submit_http(from = "user@localhost", server = "freedb.org", port = 80, path = "/~cddb/submit.cgi", submit_mode = "submit") require "net/http" headers = { "Category" => @category, "Discid" => @discid, "User-Email" => from, "Submit-Mode" => submit_mode, "Charset" => "ISO-8859-1", "X-Cddbd-Note" => "Sent by ruby-freedb #{VERSION}" } Net::HTTP.start(server, port) { |http| reply, body = http.post(path, submit_body(), headers) if reply.code != 200 raise(FreedbError, "Bad response from server: '#{body.chop}'") end } nil end |
#submit_mail(smtp_server, from = "localuser@localhost", port = 25, to = "[email protected]") ⇒ Object
submit the current Freedb object using smtp return nil
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/freedb.rb', line 261 def submit_mail(smtp_server, from = "localuser@localhost", port = 25, to = "[email protected]") # +to+ can be set to "[email protected]" to check validity (for # developpers) require "net/smtp" header = { "From" => from, "To" => to, "Subject" => "cddb #{@category} #{@discid}", "MIME-Version" => "1.0", "Content-Type" => "text/plain", "Content-Transfer-Encoding" => "quoted-printable", "X-Cddbd-Note" => "Sent by ruby-freedb #{VERSION}" } msg = "" header.each { |k, v| msg << "#{k}: #{v}\r\n" } msg << "\r\n" msg << submit_body Net::SMTP.start(smtp_server, port) { |smtp| smtp.send_mail(msg, from, to) } nil end |