Class: Switchboard::Commands::PEP::Tune

Inherits:
Switchboard::Command show all
Defined in:
lib/switchboard/commands/pep/tune.rb

Constant Summary

Constants inherited from Switchboard::Command

Switchboard::Command::DEFAULT_OPTIONS, Switchboard::Command::OPTIONS

Class Method Summary collapse

Methods inherited from Switchboard::Command

description, help, options, to_command, to_command_name

Class Method Details

.run!Object



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
# File 'lib/switchboard/commands/pep/tune.rb', line 10

def self.run!
  begin
    require 'appscript'
  rescue LoadError => e
    gem = e.message.split("--").last.strip
    puts "The #{gem} gem is required for this command to work."
    exit 1
  end

  switchboard = Switchboard::Client.new do
    @tune_helper = Jabber::UserTune::Helper.new(client, nil)

    itunes = Appscript.app('iTunes')
    old_track_info = nil
    last_state = :paused

    while !shutdown?
      track = itunes.current_track.get
      state = itunes.player_state.get

      if track && state == :playing

        artist = track.artist.get
        name = track.name.get
        source = track.album.get
        track_info = [artist, name, source]

        if track_info != old_track_info
          duration = track.duration.get.to_i
          track = track.track_number.get.to_s

          puts "Now playing: #{name} by #{artist}"
          tune = Jabber::UserTune::Tune.new \
            artist,
            name,
            duration,
            track,
            source

          begin
            @tune_helper.now_playing(tune)
          rescue Jabber::ServerError => e
            puts e
          end
        end

      elsif state != last_state
        track_info = nil
        begin
          @tune_helper.stop_playing
        rescue Jabber::ServerError => e
          puts e
        end
      end

      old_track_info = track_info
      last_state = state
      sleep 1
    end
  end

  switchboard.on_shutdown do
    begin
      @tune_helper.stop_playing
    rescue Jabber::ServerError => e
      puts e
    end
  end

  switchboard.plug!(AutoAcceptJack)

  switchboard.run!
end