-
Notifications
You must be signed in to change notification settings - Fork 1
/
itunes.rb
178 lines (142 loc) · 3.29 KB
/
itunes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'rubygems'
require 'activesupport'
require 'appscript'
require 'track'
require 'core_extensions'
require 'library'
class ITunes
include Library
attr_accessor :connect
SERVER_PLAYLIST_NAME = 'controllercontroller'
MIN_VOLUME = 0
MAX_VOLUME = 100
def initialize
init_playlist
playpause
end
def app
itunes_connection
end
def server_playlist
playlists[SERVER_PLAYLIST_NAME]
end
def add_track(track_index)
app.tracks[track_index.to_i].duplicate(:to => server_playlist)
end
def remove_track(track_index)
current_tracks_ref[track_index.to_i].delete
end
def add_album(name)
find_tracks_by_album(name).each do |track|
add_track(track.library_index)
end
end
def clear_playlist
current_tracks_ref.delete
end
def current_playlist
begin
app.current_playlist.get
rescue Appscript::CommandError
init_playlist
playpause
retry
end
end
def current_tracks_ref
current_playlist.tracks
end
def current_tracks
current_tracks_ref.get.map{|t| Track.new(t)}
end
def current_tracks_with_indexes
current_tracks.zip(current_tracks_ref.index.get)
end
def playlist_names
playlists.keys
end
def playlists
app.user_playlists.get.inject({}) do |playlists, list|
playlists.merge(list.name.get => list)
end
end
def current_track
begin
Track.new(app.current_track.get)
rescue Appscript::CommandError
playpause
retry
end
end
def time_left_in_current_track
seconds = (current_track.duration - app.player_position.get)
"%i:%02i" % [seconds / 60, seconds % 60]
end
def player_state
app.player_state.get
end
def increase_volume(incr)
new_volume = @volume_before_mute || (current_volume + incr).at_most(MAX_VOLUME)
app.sound_volume.set(new_volume)
end
def decrease_volume(incr)
new_volume = (current_volume - incr).at_least(MIN_VOLUME)
app.sound_volume.set(new_volume)
end
def set_volume(level)
app.sound_volume.set(level.at_least(MIN_VOLUME).at_most(MAX_VOLUME))
end
def mute
@volume_before_mute = current_volume
app.sound_volume.set(0)
end
def muted?
current_volume == MIN_VOLUME
end
def unmute
app.sound_volume.set(@volume_before_mute.to_i)
@volume_before_mute = nil
end
def current_volume
app.sound_volume.get
end
def play
app.play
end
def pause
app.pause
end
# ensure there's a current_track and current_playlist
def playpause
if app.player_state.get == :stopped
app.play(server_playlist)
app.pause
end
end
def add_default_track
add_track(1)
end
def create_playlist
app.user_playlists.make(:new => :playlist, :with_properties => {:name => SERVER_PLAYLIST_NAME})
end
def server_playlist_exists?
playlist_names.include?(SERVER_PLAYLIST_NAME)
end
def init_playlist
create_playlist unless server_playlist_exists?
add_default_track if playlist_empty?
end
def playlist_empty?
!server_playlist.tracks.get.any?
end
def itunes_connection
begin
@connect ||= Appscript.app('iTunes')
rescue
reconnect and retry
end
end
def reconnect
@connect = Appscript.app('iTunes')
end
end