Module: Play

Defined in:
lib/play.rb,
lib/play/configuration.rb

Overview

‘Play` is a simple system for playing media from the command line.

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

Version =
4

Class Method Summary collapse

Class Method Details

.configure(&block) ⇒ Object

Sets up the default configuration for ‘play`.



42
43
44
# File 'lib/play.rb', line 42

def self.configure &block
  Configuration.default = Configuration.new &block
end

.Play(parameters, options = Configuration.new) ⇒ Object

This method is responsible for ferreting out a particular piece of media, and playing it.

Raises:

  • (ArgumentError)


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
# File 'lib/play.rb', line 11

def self.Play parameters, options = Configuration.new
  query, season, episode = parameters
  
  files = options[:directories].inject(Array.new) do |acc, dir|
    options[:extensions].inject(acc) {|acc, ext| acc + Dir[File.expand_path File.join(dir, '**', ['*', ext].join('.'))] }
  end.select do |file|
    queries = [[query]]
    queries << ["#{season}x", "S#{season}", "Season #{season}"] if season
    queries << ["x#{episode}", "E#{episode}", "Episode #{episode}"] if episode
    queries.all? {|a| a.any? {|e| file.include? e }}
  end.reject {|file| file =~ /sample/i }
  
  raise ArgumentError, "Your query matched nothing" if files.empty?
  
  if options[:latest]
    # TODO: This should really parse the episode number and shit.
    file = files.sort {|f1, f2| File.size(f1) <=> File.size(f2) }.last
  else
    files.each.with_index {|file, i| puts "#{i + 1}: #{file}"}
    print "Please select a number: "
    file = files[STDIN.gets.chomp.to_i - 1]
  end
  
  args = [options[:player], file]
  args += ['-vf', "expand=" + [:top, :bottom, :left, :right].map {|side| "-" + (options[["padding", side].join('_').intern] || 0).to_s}.join(':')]
  args += ['-ss', options[:spot].to_s] if options[:spot]
  system *args
end