the_little_streamer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +1 -1
  2. data/bin/the_little_streamer +148 -119
  3. metadata +4 -4
data/README.md CHANGED
@@ -10,7 +10,7 @@ Extremely simple music streamer using Sinatra.
10
10
 
11
11
  the_little_streamer /path/to/music
12
12
 
13
- Browse to http://localhost:4567
13
+ Browse to [http://localhost:4567](http://localhost:4567)
14
14
 
15
15
  ## Dependencies
16
16
 
@@ -13,117 +13,146 @@ abort "Point me to the music!" unless ARGV[0]
13
13
 
14
14
  #Wrap up in HTML
15
15
  def html body
16
- <<-HTML
17
- <html>
18
- <body>
19
- #{body}
20
- </body>
21
- </html>
22
- HTML
16
+ <<-HTML
17
+ <html>
18
+ <head>
19
+ <style type="text/css">
20
+ #{css}
21
+ </style>
22
+ </head>
23
+ <body>
24
+ #{body}
25
+ </body>
26
+ </html>
27
+ HTML
28
+ end
29
+
30
+ def css
31
+ <<-CSS
32
+ body {
33
+ font-family: Helvetica,Verdana,Arial,sans-serif;
34
+ font-size: 13pt;
35
+ }
36
+
37
+ span#title {
38
+ font-size: 14pt;
39
+ font-weight: bold;
40
+ }
41
+
42
+ span#artist {
43
+ font-size: 14pt;
44
+ font-weight: bold;
45
+ }
46
+
47
+ span#album {
48
+ font-style: italic;
49
+ }
50
+ CSS
23
51
  end
24
52
 
25
53
  #Create a link
26
54
  def link root, path, text = path
27
- "<a href='#{root}#{CGI.escape path}'>#{text}</a>"
55
+ full_path = root.split('/').map! {|r| CGI.escape r }.join('/') << "/" << CGI.escape(path)
56
+ "<a href=\"#{full_path}\">#{text}</a>"
28
57
  end
29
58
 
30
59
  #Audio tag
31
60
  def audio path
32
- <<-HTML
33
- <audio id='player' onEnded='javascript:play_next()' src=#{("/" << CGI.escapeHTML(path)).inspect} autobuffer controls autoplay >
34
- You need the power of HTML5!
35
- </audio>
36
- <script type="text/javascript">
37
- document.getElementById('player').volume = 0.3;
38
- </script>
39
- HTML
61
+ <<-HTML
62
+ <audio id='player' onEnded='javascript:play_next()' src=#{("/" << CGI.escapeHTML(path)).inspect} autobuffer controls autoplay >
63
+ You need the power of HTML5!
64
+ </audio>
65
+ <script type="text/javascript">
66
+ document.getElementById('player').volume = 0.3;
67
+ </script>
68
+ HTML
40
69
  end
41
70
 
42
71
  #Javascript for playlist
43
72
  def playlist songs
44
- <<-JAVASCRIPT
45
- <script type="text/javascript">
46
- var player = document.getElementById('player');
47
- var artist = document.getElementById('artist');
48
- var album = document.getElementById('album');
49
- var title = document.getElementById('song_title');
50
- var playlist = [#{songs.map { |s| song_to_js s }.join "," }]
51
- var current_song = 0;
52
-
53
- play_next = function(reverse) {
54
- if(reverse && current_song > 0)
55
- current_song--;
56
- else if(!reverse && current_song < playlist.length)
57
- current_song++;
58
- else
59
- return;
60
-
61
- var song = playlist[current_song];
62
- player.src = song.path;
63
- artist.innerHTML = song.artist
64
- album.innerHTML = song.album
65
- title.innerHTML = song.title
66
- player.play();
67
- }
68
- </script>
69
- JAVASCRIPT
73
+ <<-JAVASCRIPT
74
+ <script type="text/javascript">
75
+ var player = document.getElementById('player');
76
+ var artist = document.getElementById('artist');
77
+ var album = document.getElementById('album');
78
+ var title = document.getElementById('title');
79
+ var playlist = [#{songs.map { |s| song_to_js s }.join "," }]
80
+ var current_song = 0;
81
+
82
+ play_next = function(reverse) {
83
+ if(reverse && current_song > 0)
84
+ current_song--;
85
+ else if(!reverse && current_song < playlist.length)
86
+ current_song++;
87
+ else
88
+ return;
89
+
90
+ var song = playlist[current_song];
91
+ player.src = song.path;
92
+ artist.innerHTML = song.artist
93
+ album.innerHTML = song.album
94
+ title.innerHTML = song.title
95
+ player.play();
96
+ }
97
+ </script>
98
+ JAVASCRIPT
70
99
  end
71
100
 
72
101
  #Javascript for a song
73
102
  def song_to_js song
74
- <<-JAVASCRIPT
75
- { artist: #{song.artist.inspect},
76
- album: #{song.album.inspect},
77
- title: #{song.title.inspect},
78
- path: #{(CGI.escapeHTML "/" << song.path).inspect} }
79
- JAVASCRIPT
103
+ <<-JAVASCRIPT
104
+ { artist: #{song.artist.inspect},
105
+ album: #{song.album.inspect},
106
+ title: #{song.title.inspect},
107
+ path: #{(CGI.escapeHTML "/" << song.path).inspect} }
108
+ JAVASCRIPT
80
109
  end
81
110
 
82
111
  #Back/forward links
83
112
  def prev_and_next
84
- "<a href='javascript:play_next(true)'>Prev</a>&nbsp;&nbsp;&nbsp;<a href='javascript:play_next()'>Next</a>"
113
+ "<a href='javascript:play_next(true)'>Prev</a>&nbsp;&nbsp;&nbsp;<a href='javascript:play_next()'>Next</a>"
85
114
  end
86
115
 
87
116
  #HTML for playing all songs by artist
88
117
  def play_artist artist
89
- songs = []
118
+ songs = []
90
119
 
91
- Music[artist].each_value do |album|
92
- album.values.sort_by { |s| s.track }.each do |song|
93
- songs << song
94
- end
95
- end
120
+ Music[artist].each_value do |album|
121
+ album.values.sort_by { |s| s.track }.each do |song|
122
+ songs << song
123
+ end
124
+ end
96
125
 
97
- html <<-HTML
98
- #{song_info songs.first}<br/>
99
- #{audio songs.first.path}<br/>
100
- #{prev_and_next}
101
- #{playlist songs}
102
- HTML
126
+ html <<-HTML
127
+ #{song_info songs.first}<br/>
128
+ #{audio songs.first.path}<br/>
129
+ #{prev_and_next if songs.length > 1}
130
+ #{playlist songs}
131
+ HTML
103
132
  end
104
133
 
105
134
  #HTML for playing all songs on an album
106
135
  def play_album artist, album
107
- songs = Music[artist][album].values.sort_by { |s| s.track }
136
+ songs = Music[artist][album].values.sort_by { |s| s.track }
108
137
 
109
- html <<-HTML
110
- #{song_info songs.first}<br/>
111
- #{audio songs.first.path}<br/>
112
- #{prev_and_next}
113
- #{playlist songs}
114
- HTML
138
+ html <<-HTML
139
+ #{song_info songs.first}<br/>
140
+ #{audio songs.first.path}<br/>
141
+ #{prev_and_next if songs.length > 1}
142
+ #{playlist songs}
143
+ HTML
115
144
  end
116
145
 
117
146
  #HTML for song information header
118
147
  def song_info song
119
- "\"<span id='song_title'>#{song.title}</span>\" by <span id='artist'>#{song.artist}</span> (<span id='album'>#{song.album}</span>)"
148
+ "<span id='title'>#{song.title}</span> by <span id='artist'>#{song.artist}</span><br/><span id='album'>#{song.album}</span>"
120
149
  end
121
150
 
122
151
  #Storing all music information in here
123
152
  Music = Hash.new do |h,k|
124
- h[k] = Hash.new do |hash, key|
125
- hash[key] = Hash.new
126
- end
153
+ h[k] = Hash.new do |hash, key|
154
+ hash[key] = Hash.new
155
+ end
127
156
  end
128
157
 
129
158
  #Song information
@@ -140,79 +169,79 @@ set :run, true
140
169
 
141
170
  #Grab information from all the song files
142
171
  Dir.glob "#{ARGV[0]}/**/*.{mp3,ogg}" do |file|
143
- begin
144
- info = TagLib2::File.new(file)
172
+ begin
173
+ info = TagLib2::File.new(file)
145
174
 
146
- artist, album, title, track = info.artist, info.album, info.title, info.track
147
- artist ||= "Unknown"
148
- album ||= "Unknown"
149
- info = nil
175
+ artist, album, title, track = info.artist, info.album, info.title, info.track
176
+ artist ||= "Unknown"
177
+ album ||= "Unknown"
178
+ info = nil
150
179
 
151
- if title
152
- [artist, album, title].each { |i| i.tr!('/', '-') }
153
- Music[artist][album][title] = Song.new(artist, album, title, track, Pathname.new(file).relative_path_from(root).to_s)
154
- end
155
- rescue Exception => e
156
- $stderr.puts e if $DEBUG
157
- end
180
+ if title
181
+ [artist, album, title].each { |i| i.tr!('/', '-') }
182
+ Music[artist][album][title] = Song.new(artist, album, title, track, Pathname.new(file).relative_path_from(root).to_s)
183
+ end
184
+ rescue Exception => e
185
+ $stderr.puts e if $DEBUG
186
+ end
158
187
  end
159
188
 
160
189
  GC.start #Clean up after TagLib2
161
190
 
162
191
  #List artists
163
192
  get '/' do
164
- path = '/artist/'
165
- html Music.keys.sort.map { |a| link path, a }.join "<br/>"
193
+ path = '/artist/'
194
+ html Music.keys.sort.map { |a| link path, a }.join "<br/>"
166
195
  end
167
196
 
168
197
  #List albums by given artist
169
198
  get '/artist/:artist/?' do |artist|
170
- unless Music[artist].empty?
171
- path = "/artist/#{artist}/album/"
172
- html Music[artist].keys.sort.map { |a| link path, a }.join("<br/>") << "<br/><br/>" <<
173
- link("/artist/#{artist}/", "play", "Play All")
174
- else
175
- html "Could not find <b>#{artist}</b>."
176
- end
199
+ unless Music[artist].empty?
200
+ path = "/artist/#{artist}/album/"
201
+ html Music[artist].keys.sort.map { |a| link path, a }.join("<br/>") << "<br/><br/>" <<
202
+ link("/artist/#{artist}/", "play", "Play All")
203
+ else
204
+ html "Could not find <b>#{artist}</b>."
205
+ end
177
206
  end
178
207
 
179
208
  #Play songs by given artist
180
209
  get '/artist/:artist/play/?' do |artist|
181
- unless Music[artist].empty?
182
- play_artist artist
183
- else
184
- html "Could not find <b>#{artist}</b>."
185
- end
210
+ unless Music[artist].empty?
211
+ play_artist artist
212
+ else
213
+ html "Could not find <b>#{artist}</b>."
214
+ end
186
215
  end
187
216
 
188
217
  #List songs on given album
189
218
  get '/artist/:artist/album/:album/?' do |artist, album|
190
- unless Music[artist][album].empty?
191
- path = "/artist/#{artist}/album/#{album}/song/"
192
- html Music[artist][album].values.sort_by { |s| s.track || 0}.map { |s| link path, s.title }.join("<br/>") << "<br/><br/>" <<
193
- link("/artist/#{artist}/album/#{album}/", "play", "Play All")
194
- else
195
- html "Could not find <b>#{album}</b> for <b>#{artist}</b>."
196
- end
219
+ unless Music[artist][album].empty?
220
+ path = "/artist/#{artist}/album/#{album}/song/"
221
+ html Music[artist][album].values.sort_by { |s| s.track || 0}.map { |s| link path, s.title }.join("<br/>") << "<br/><br/>" <<
222
+ link("/artist/#{artist}/album/#{album}/", "play", "Play All")
223
+ else
224
+ html "Could not find <b>#{album}</b> for <b>#{artist}</b>."
225
+ end
197
226
  end
198
227
 
199
228
  #Play all songs from a given album
200
229
  get '/artist/:artist/album/:album/play/?' do |artist, album|
201
- if Music[artist][album]
202
- play_album artist, album
203
- else
204
- html "Could not find <b>#{album}</b> by <b>#{artist}</b>."
205
- end
230
+ if Music[artist][album]
231
+ play_album artist, album
232
+ else
233
+ html "Could not find <b>#{album}</b> by <b>#{artist}</b>."
234
+ end
206
235
  end
207
236
 
208
237
  #Play song!
209
238
  get '/artist/:artist/album/:album/song/:song/?' do |artist, album, title|
210
- if song = Music[artist][album][title]
211
- html <<-HTML
212
- #{song_info song}<br/>
213
- #{audio song.path}
214
- HTML
215
- else
216
- html "Could not find <b>#{title}</b> on <b>#{album}</b> by <b>#{artist}</b>."
217
- end
239
+ if song = Music[artist][album][title]
240
+ html <<-HTML
241
+ #{song_info song}<br/>
242
+ #{audio song.path}
243
+ HTML
244
+ else
245
+ html "Could not find <b>#{title}</b> on <b>#{album}</b> by <b>#{artist}</b>."
246
+ end
218
247
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the_little_streamer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Justin Collins
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-14 00:00:00 -07:00
18
+ date: 2011-04-01 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency