Skip to content
Browse files

ID3 tag support. fix #1

  • Loading branch information...
1 parent da08b02 commit 84237d9e1fd9061a1fb2481a79314fad75597d96 @youpy committed Jun 19, 2012
Showing with 39 additions and 2 deletions.
  1. +35 −1 lib/rack/musicindex.rb
  2. +1 −0 rack-musicindex.gemspec
  3. +3 −1 spec/rack/musicindex_spec.rb
View
36 lib/rack/musicindex.rb
@@ -1,4 +1,6 @@
require 'builder'
+require 'id3lib'
+require 'kconv'
module Rack
class MusicIndex
@@ -62,6 +64,29 @@ def update_files
end
end
+ def id3(filename)
+ value = {}
+ tag = ID3Lib::Tag.new(filename)
+ {
+ :TIT2 => :name,
+ :TPE1 => :artist,
+ }.each do |id, key|
+ frame = tag.frame(id)
+
+ if frame
+ if frame[:textenc] == 1
+ v = Kconv.kconv(frame[:text] , Kconv::UTF8, Kconv::UTF16)
+ else
+ v = frame[:text]
+ end
+ end
+
+ value[key] = v
+ end
+
+ value
+ end
+
def podcast(env)
path = env['PATH_INFO']
req = Rack::Request.new(env)
@@ -77,14 +102,23 @@ def podcast(env)
xml.link url
files.each do |file|
+ tag = id3(file)
+ author = tag[:artist]
name = ::File.basename(file)
item_link = url + '/' + name
+
xml.item do
- xml.title name
+ xml.title tag[:name] || name
xml.description name
xml.link item_link
xml.guid item_link
xml.enclosure :url => item_link
+
+ if author
+ xml.author author
+ xml.itunes :author, author
+ xml.itunes :summary, author
+ end
end
end
end
View
1 rack-musicindex.gemspec
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
gem.version = Rack::Musicindex::VERSION
gem.add_dependency('builder')
+ gem.add_dependency('id3lib-ruby')
gem.add_development_dependency('rspec', ['~> 2.8.0'])
gem.add_development_dependency('rake')
gem.add_development_dependency('sinatra')
View
4 spec/rack/musicindex_spec.rb
@@ -36,10 +36,12 @@ def app
channel.xpath('description')[0].content.should eql('Generated by Rack::MusicIndex')
items.size.should eql(1)
- item.xpath('title')[0].content.should eql('test.mp3')
+ item.xpath('title')[0].content.should eql('bar')
item.xpath('link')[0].content.should eql('http://example.org/foo/test.mp3')
item.xpath('guid')[0].content.should eql('http://example.org/foo/test.mp3')
item.xpath('enclosure')[0]['url'].should eql('http://example.org/foo/test.mp3')
+ item.xpath('author')[0].content.should eql('foo')
+ item.xpath('itunes:author')[0].content.should eql('foo')
end
it 'should reflect file changes without restart' do

0 comments on commit 84237d9

Please sign in to comment.
Something went wrong with that request. Please try again.