itunes-web-frontend 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2008, Ole Friis Østergaard
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'itunes-web-frontend'
@@ -0,0 +1,135 @@
1
+ # Gems to install: sinatra, rubyosa
2
+ # Before starting up, open iTunes and make sure that all songs in your whole
3
+ # song collection are shown in the main window. Otherwise, when you select a
4
+ # song, only that song will be played.
5
+
6
+ require 'rubygems'
7
+ require 'sinatra'
8
+ require 'rbosa'
9
+
10
+ # Put us in production, so we don't need to specify that on the command-line
11
+ Sinatra.application.options.env = :production
12
+
13
+ # Just make a quick test to see if iTunes is running
14
+ itunes = OSA.app('iTunes')
15
+ begin
16
+ itunes.name
17
+ rescue
18
+ puts 'You need to start iTunes'
19
+ exit
20
+ end
21
+
22
+ puts "Remember to select 'All genres', 'All artists', 'All albums' in iTunes, or you'll get annoyed sooner or later..."
23
+
24
+ # Organize and cache the whole music collection.
25
+ # (Ugly code...)
26
+ album_struct = Hash.new
27
+ itunes.sources.each do |source|
28
+ source.playlists.find_all {|p| p.name == 'Music' }.each do |playlist|
29
+ playlist.tracks.each do |track|
30
+ album = track.album
31
+ album_struct[album] ||= []
32
+ album_struct[album] << track
33
+ end
34
+ end
35
+ end
36
+ album_struct.each_value {|tracks| tracks.sort_by {|track| track.track_number }}
37
+ album_id = 0
38
+ albums = Hash.new
39
+ Album = Struct.new(:id, :title, :artist, :tracks)
40
+ album_struct.each_key do |title|
41
+ album_id += 1
42
+ albums[album_id] = Album.new(album_id, title, album_struct[title][0].artist, album_struct[title])
43
+ end
44
+
45
+ # Helper method, to be used later
46
+ def get_and_back(url)
47
+ get url do
48
+ yield
49
+ redirect '/'
50
+ end
51
+ end
52
+
53
+ # Main page
54
+ get '/' do
55
+ @headline = 'Stopped'
56
+ if (itunes.player_state.name != 'stopped')
57
+ track = itunes.current_track
58
+ name, artist, album = track.name, track.artist, track.album
59
+ @headline = "#{name} (#{artist}, from #{album}): #{track.time}"
60
+ end
61
+ erb <<END
62
+ <html>
63
+ <body>
64
+ <h3><%= @headline %></h3>
65
+ <table>
66
+ <tr><td>Track:</td><td><a href="prev">previous</a></td><td><a href="next">next</a></td></tr>
67
+ <tr><td>Volume:</td><td><a href="down">down</a></td><td><a href="up">up</a></td></tr>
68
+ <tr><td>Playback:</td><td><a href="pause">pause</a></td><td><a href="play">play</a></td></tr>
69
+ <tr><td colspan="3"><a href="albums?sort=title">Albums by title</a>
70
+ <a href="albums?sort=artist">Albums by artist</a></td></tr>
71
+ </table>
72
+ </body>
73
+ </html>
74
+ END
75
+ end
76
+
77
+ # List of albums, sorted by any given attribute (defaults to 'title')
78
+ get '/albums' do
79
+ order_by = params[:sort] || 'title'
80
+ @sorted_albums = albums.values.sort_by {|album| album.send order_by }
81
+ erb <<END
82
+ <html>
83
+ <body>
84
+ <p><a href="/">Back</a></p>
85
+ <table>
86
+ <tr><td>Album</td><td>Artist</td></tr>
87
+ <% for album in @sorted_albums do %>
88
+ <tr><td><a href="/albums/<%= album.id %>"><%= album.title %></a></td>
89
+ <td><a href="/albums/<%= album.id %>"><%= album.artist %></a></td></tr>
90
+ <% end %>
91
+ </table>
92
+ </body>
93
+ </html>
94
+ END
95
+ end
96
+
97
+ # Contents of a single album
98
+ get '/albums/:id' do
99
+ @album = albums[params[:id].to_i]
100
+ erb <<END
101
+ <html>
102
+ <body>
103
+ <p><a href="/">Back</a></p>
104
+ <table>
105
+ <tr><td>Track</td><td>Length</td></tr>
106
+ <% @album.tracks.each_with_index do |track, number| %>
107
+ <tr><td><a href="/albums/<%= @album.id %>/<%= number %>"><%= track.name %></a></td>
108
+ <td><a href="/albums/<%= @album.id %>/<%= number %>"><%= track.time %></a></td>
109
+ <% end %>
110
+ </table>
111
+ </body>
112
+ </html>
113
+ END
114
+ end
115
+
116
+ # Starts playing a specific track
117
+ get '/albums/:album_id/:track_number' do
118
+ album_id = params[:album_id].to_i
119
+ track_number = params[:track_number].to_i
120
+
121
+ # The following line works in Tiger, but not Leopard (damn Apple!)
122
+ #albums[album_id].tracks[track_number].play(:once => false)
123
+ # ...so we'll do this instead:
124
+ system "osascript -e 'tell application \"iTunes\" to play (tracks of (playlist \"Music\") whose database ID is #{albums[album_id].tracks[track_number].database_id})'"
125
+
126
+ redirect '/'
127
+ end
128
+
129
+ # Basic controls from the main page
130
+ get_and_back('/next') { itunes.next_track }
131
+ get_and_back('/prev') { itunes.previous_track }
132
+ get_and_back('/up') { itunes.sound_volume += 5 }
133
+ get_and_back('/down') { itunes.sound_volume -= 5 }
134
+ get_and_back('/pause') { itunes.pause }
135
+ get_and_back('/play') { itunes.play }
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: itunes-web-frontend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ole Friis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-15 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rubyosa
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.4.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: sinatra
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ version:
35
+ description:
36
+ email: olefriis@gmail.com
37
+ executables:
38
+ - start-itunes-web-frontend
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - bin/start-itunes-web-frontend
45
+ - lib/itunes-web-frontend.rb
46
+ - README
47
+ has_rdoc: false
48
+ homepage: http://itunes-frontend.rubyforge.org/
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: iTunes web frontend
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Simple web front-end for controlling an iTunes instance
73
+ test_files: []
74
+