rack-musicindex 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,6 @@
1
1
  require 'builder'
2
+ require 'id3lib'
3
+ require 'kconv'
2
4
 
3
5
  module Rack
4
6
  class MusicIndex
@@ -13,37 +15,48 @@ module Rack
13
15
  end
14
16
 
15
17
  def call(env)
16
- status, headers, response = @app.call(env)
17
-
18
18
  path_info = env['PATH_INFO']
19
19
 
20
20
  update_files
21
21
 
22
22
  if dirs[path_info]
23
- headers['Content-Type'] = 'application/xml;charset=utf-8'
24
- body = podcast(env)
25
- headers["Content-Length"] = body.length.to_s
26
- response = [body]
27
- status = 200
23
+ serve_podcast(env)
28
24
  elsif static_paths.include?(path_info)
29
- body = open(static_paths[path_info], 'rb').read
30
- headers["Content-Type"] = 'audio/mpeg'
31
- headers["Content-Length"] = body.length.to_s
32
- response = [body]
33
- status = 200
25
+ serve_mp3(env)
26
+ else
27
+ status, headers, response = @app.call(env)
34
28
  end
35
-
36
- [status, headers, response]
37
29
  end
38
30
 
39
31
  private
40
32
 
33
+ def serve_podcast(env)
34
+ status, headers, response = @app.call(env)
35
+
36
+ body = podcast(env)
37
+ headers['Content-Type'] = 'application/xml;charset=utf-8'
38
+ headers["Content-Length"] = body.bytesize.to_s
39
+
40
+ [200, headers, [body]]
41
+ end
42
+
43
+ def serve_mp3(env)
44
+ status, headers, response = @app.call(env)
45
+ path_info = env['PATH_INFO']
46
+
47
+ body = open(static_paths[path_info], 'rb').read
48
+ headers["Content-Type"] = 'audio/mpeg'
49
+ headers["Content-Length"] = body.bytesize.to_s
50
+
51
+ [200, headers, [body]]
52
+ end
53
+
41
54
  def dirs
42
55
  @dirs
43
56
  end
44
57
 
45
- def files(media_dir)
46
- @files[media_dir]
58
+ def files(path)
59
+ @files[path]
47
60
  end
48
61
 
49
62
  def static_paths
@@ -62,6 +75,29 @@ module Rack
62
75
  end
63
76
  end
64
77
 
78
+ def id3(filename)
79
+ value = {}
80
+ tag = ID3Lib::Tag.new(filename)
81
+ {
82
+ :TIT2 => :name,
83
+ :TPE1 => :artist,
84
+ }.each do |id, key|
85
+ frame = tag.frame(id)
86
+
87
+ if frame
88
+ if frame[:textenc] == 1
89
+ v = Kconv.kconv(frame[:text] , Kconv::UTF8, Kconv::UTF16)
90
+ else
91
+ v = frame[:text]
92
+ end
93
+ end
94
+
95
+ value[key] = v
96
+ end
97
+
98
+ value
99
+ end
100
+
65
101
  def podcast(env)
66
102
  path = env['PATH_INFO']
67
103
  req = Rack::Request.new(env)
@@ -77,14 +113,23 @@ module Rack
77
113
  xml.link url
78
114
 
79
115
  files.each do |file|
116
+ tag = id3(file)
117
+ author = tag[:artist]
80
118
  name = ::File.basename(file)
81
119
  item_link = url + '/' + name
120
+
82
121
  xml.item do
83
- xml.title name
122
+ xml.title tag[:name] || name
84
123
  xml.description name
85
124
  xml.link item_link
86
125
  xml.guid item_link
87
126
  xml.enclosure :url => item_link
127
+
128
+ if author
129
+ xml.author author
130
+ xml.itunes :author, author
131
+ xml.itunes :summary, author
132
+ end
88
133
  end
89
134
  end
90
135
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module Musicindex
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.version = Rack::Musicindex::VERSION
17
17
 
18
18
  gem.add_dependency('builder')
19
+ gem.add_dependency('id3lib-ruby')
19
20
  gem.add_development_dependency('rspec', ['~> 2.8.0'])
20
21
  gem.add_development_dependency('rake')
21
22
  gem.add_development_dependency('sinatra')
@@ -36,10 +36,12 @@ describe Rack::MusicIndex do
36
36
  channel.xpath('description')[0].content.should eql('Generated by Rack::MusicIndex')
37
37
 
38
38
  items.size.should eql(1)
39
- item.xpath('title')[0].content.should eql('test.mp3')
39
+ item.xpath('title')[0].content.should eql('bar')
40
40
  item.xpath('link')[0].content.should eql('http://example.org/foo/test.mp3')
41
41
  item.xpath('guid')[0].content.should eql('http://example.org/foo/test.mp3')
42
42
  item.xpath('enclosure')[0]['url'].should eql('http://example.org/foo/test.mp3')
43
+ item.xpath('author')[0].content.should eql('foo')
44
+ item.xpath('itunes:author')[0].content.should eql('foo')
43
45
  end
44
46
 
45
47
  it 'should reflect file changes without restart' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-musicindex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-19 00:00:00.000000000 Z
12
+ date: 2012-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: id3lib-ruby
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: rspec
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -142,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
158
  version: '0'
143
159
  segments:
144
160
  - 0
145
- hash: 1861601957607199422
161
+ hash: -4013197686182090724
146
162
  required_rubygems_version: !ruby/object:Gem::Requirement
147
163
  none: false
148
164
  requirements:
@@ -151,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
167
  version: '0'
152
168
  segments:
153
169
  - 0
154
- hash: 1861601957607199422
170
+ hash: -4013197686182090724
155
171
  requirements: []
156
172
  rubyforge_project:
157
173
  rubygems_version: 1.8.24