Class: CUI8Tracks::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/cui8tracks/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#apiObject

Returns the value of attribute api.



2
3
4
# File 'lib/cui8tracks/session.rb', line 2

def api
  @api
end

#configObject

Returns the value of attribute config.



2
3
4
# File 'lib/cui8tracks/session.rb', line 2

def config
  @config
end

#current_mixObject

Returns the value of attribute current_mix.



2
3
4
# File 'lib/cui8tracks/session.rb', line 2

def current_mix
  @current_mix
end

#current_trackObject

Returns the value of attribute current_track.



2
3
4
# File 'lib/cui8tracks/session.rb', line 2

def current_track
  @current_track
end

#setObject

Returns the value of attribute set.



2
3
4
# File 'lib/cui8tracks/session.rb', line 2

def set
  @set
end

Instance Method Details

#authorize(username, password) ⇒ Object



31
32
33
34
35
36
# File 'lib/cui8tracks/session.rb', line 31

def authorize(username, password)
  raise 'config sesms not loaded.' unless config
  @api = CUI8Tracks::API.new(username, password)
  @api.session = self
  @api.
end

#avail_commandsObject



63
64
65
# File 'lib/cui8tracks/session.rb', line 63

def avail_commands
  %w{ pause skip skip_mix toggle_like like unlike toggle_fav fav unfav toggle_follow follow unfollow open help exit}
end

#execute(command) ⇒ Object



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
# File 'lib/cui8tracks/session.rb', line 67

def execute(command)
  case command
  when 'p'
    execute 'pause'
  when 'pause'
    logger.info 'pause'
    current_track.pause
  when 'skip'
    logger.info 'skip track'
    current_track.stop
  when 'skip_mix'
    logger.info 'skip mix'
    current_mix.skip
  when 's'
    execute 'skip'
  when 'h'
    execute 'help'
  when '?'
    execute 'help'
  when 'help'
    logger.info "available commands:"
    logger.info avail_commands
  when 'exit'
    current_track.stop
    exit

  when 'toggle_like'
    current_mix.toggle_like
    logger.info "toggled like mix"
  when 'like'
    current_mix.like
    logger.info "liked mix"
  when 'unlike'
    current_mix.unlike
    logger.info "unliked mix"

  when 'toggle_fav'
    current_track.toggle_fav
    logger.info "toggled favorite track"
  when 'fav'
    current_track.fav
    logger.info "favorited track"
  when 'unfav'
    current_track.unfav
    logger.info "unfavorited track"

  when 'toggle_follow'
    current_mix.user.toggle_follow
    logger.info "toggled follow user"
  when 'follow'
    current_mix.user.follow
    logger.info "followed user"
  when 'unfollow'
    current_mix.user.unfollow
    logger.info "unfollowed user"

  when 'open'
    logger.info "open current mix"
    system "open #{current_mix.restful_url}"
  else
    logger.info "unknown command: #{command}"
    execute 'help'
  end
end

#load_config(argv) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cui8tracks/session.rb', line 11

def load_config(argv)
  opt = OptionParser.new
  @config = {:per_page => 1, :page => 1 }

  OptionParser.new {|opt|
    opt.on('-q QUERY', '--query') {|v| @config[:q] = v}
    opt.on('-t TAG', '--tag')   {|v| @config[:tag] = v}
    opt.on('-u USER', '--user')  {|v| @config[:user] = v}
    opt.on('-s SORT', '--sort', '[recent|hot|popular|random]')  {|v| @config[:sort] = v}
    opt.on('--no-play', "don't play tracks")  {|v| @config[:no_play] = true}
    opt.on('--play_from FROM', 'play from [FROM]th mix')  {|v| @config[:play_from] = v.to_i}
    opt.on('--verbose', 'print mplayer output')  {|v| @config[:verbose] = v}
    opt.on('--debug', 'debug-mode')  {|v| @config[:debug] = v}
    opt.parse(argv)
  }
  logger.debug @config

  system 'mplayer >& /dev/null' or raise 'mplayer seems not installed'
end

#loggerObject



4
5
6
7
8
9
# File 'lib/cui8tracks/session.rb', line 4

def logger
  return @logger if @logger
  @logger = Logger.new STDOUT
  @logger.level = self.config[:debug] ? Logger::DEBUG : Logger::INFO
  @logger
end

#playObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cui8tracks/session.rb', line 38

def play
  @set = set = CUI8Tracks::Set.new
  set.session = self
  %w{q tag user sort}.each{ |key|
    set.instance_variable_set('@' + key, config[key.to_sym])
  }
  if config[:play_from]
    set.page = config[:play_from]
  end
  set.each_mix{ |mix|
    @current_mix = mix
    logger.info "Playing mix #{set.page} / #{set.total_entries}"
    mix.info
    mix.each_track{ |track|
      @current_track = track
      logger.info "Playing track"
      track.info
      track.play
      while track.playing?
        sleep 1
      end
    }
  }
end

#start_input_threadObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cui8tracks/session.rb', line 132

def start_input_thread
  Thread.new {
    Readline.completion_proc = lambda {|input|
      avail_commands.grep(/\A#{Regexp.quote input}/)
    }
    while line = Readline.readline('> ')
      begin
        line = line.chomp.strip rescue ''
        if line.empty?
          Readline::HISTORY.pop
          next
        end
        execute line
      rescue => e
        logger.error "#{e.class}, #{e.message}"
        puts e.backtrace.join("\n")
      end
    end
  }
end