class YoutubeDL::OutputParser

Attributes

state[R]

Public Class Methods

new() click to toggle source
# File lib/youtube_dl/output_parser.rb, line 6
def initialize
  @state = State.new
end

Public Instance Methods

process(line) click to toggle source
# File lib/youtube_dl/output_parser.rb, line 12
def process(line)
  line = line.strip

  m = regex.match(line)
  return :unparsable if m.nil?

  if m[:destination]
    state.destination = Pathname(m[:destination])
    return :destination
  end

  if m[:info_json]
    state.info_json = Pathname(m[:info_json]) if m[:info_json]
    return :info_json
  end

  if m[:progress]
    if m[:progress]
      if m[:progress] == '100'
        state.progress = 100
      else
        state.progress = m[:progress].to_f
      end
    end
    state.total_size = Filesize.from("#{m[:total_size]} #{m[:total_size_unit]}") if m[:total_size]
    state.speed = Filesize.from("#{m[:speed]} #{m[:speed_unit]}") if m[:speed]
    return :progress
  end

  if m[:error]
    state.error = m[:error] if m[:error]
    return :error
  end

  raise 'Not sure how I got here...'
end

Private Instance Methods

destination_regex() click to toggle source
# File lib/youtube_dl/output_parser.rb, line 76
def destination_regex
  %r{
    \[download\] \s+ Destination: \s+
    (?<destination>.*)
  }x
end
error_regex() click to toggle source
# File lib/youtube_dl/output_parser.rb, line 98
def error_regex
  %r{
    ERROR: \s
    (?<error>.*)
  }x
end
existing_destination_regex() click to toggle source
# File lib/youtube_dl/output_parser.rb, line 83
def existing_destination_regex
  %r{
    \[download\] \s
    (?<destination>.*?) \s
    has \s already \s been \s downloaded
  }x
end
info_json_regex() click to toggle source
# File lib/youtube_dl/output_parser.rb, line 91
def info_json_regex
  %r{
    \[info\] \s Writing \s video \s description \s metadata \s as \s JSON \s to: \s
    (?<info_json>.*)
  }x
end
num_regex(name) click to toggle source
# File lib/youtube_dl/output_parser.rb, line 105
def num_regex(name)
  %r{
    (?<#{name}>
      \d+(\.\d+)?
    )
  }x
end
num_with_unit_regex(name) click to toggle source
# File lib/youtube_dl/output_parser.rb, line 121
def num_with_unit_regex(name)
  %r{
    #{num_regex(name)}
    #{unit_regex(name)}
  }x
end
progress_regex() click to toggle source
# File lib/youtube_dl/output_parser.rb, line 61
def progress_regex
  %r{
    \[download\] \s+
    #{num_regex('progress')}%
    (
      \s+ of \s+
      #{num_with_unit_regex('total_size')}
      (
        \s+ at \s+
        #{num_with_unit_regex('speed')}
      )?
    )?
  }x
end
regex() click to toggle source
# File lib/youtube_dl/output_parser.rb, line 51
def regex
  %r{
    #{progress_regex} |
    #{destination_regex} |
    #{existing_destination_regex} |
    #{info_json_regex} |
    #{error_regex}
  }x
end
unit_regex(name) click to toggle source
# File lib/youtube_dl/output_parser.rb, line 113
def unit_regex(name)
  %r{
    (?<#{name}_unit>
      \w+
    )
  }x
end