Class: Showfix::Episode

Inherits:
Object
  • Object
show all
Defined in:
lib/showfix/episode.rb

Constant Summary collapse

VALID_EXTENSIONS =

Video files, and some subtitles

%w(avi mkv mp4 mpg mpeg mov m4v srt)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ Episode

Returns a new instance of Episode.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/showfix/episode.rb', line 12

def initialize(filename, options={})

  raise ArgumentError, 'no filename' unless filename

  @filename = filename
  @options = {
    season: nil,
    series: nil
    }.merge(options)

  @cleaner = Cleaner.new(@options)
  @parser = Parser.new(@options)

  @season ||= options[:season] # if they passed in a default season
  @series ||= options[:series]

  @file_no_ext = @filename.chomp(File.extname(@filename))
  @extension = File.extname(@filename).gsub(/\./, '').downcase

  self.update_episode_info
end

Instance Attribute Details

#episodeObject

Returns the value of attribute episode.



9
10
11
# File 'lib/showfix/episode.rb', line 9

def episode
  @episode
end

#extensionObject (readonly)

Returns the value of attribute extension.



10
11
12
# File 'lib/showfix/episode.rb', line 10

def extension
  @extension
end

#file_no_extObject (readonly)

Returns the value of attribute file_no_ext.



10
11
12
# File 'lib/showfix/episode.rb', line 10

def file_no_ext
  @file_no_ext
end

#filenameObject (readonly)

Returns the value of attribute filename.



10
11
12
# File 'lib/showfix/episode.rb', line 10

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/showfix/episode.rb', line 10

def options
  @options
end

#seasonObject

Returns the value of attribute season.



9
10
11
# File 'lib/showfix/episode.rb', line 9

def season
  @season
end

#seriesObject

Returns the value of attribute series.



9
10
11
# File 'lib/showfix/episode.rb', line 9

def series
  @series
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/showfix/episode.rb', line 9

def title
  @title
end

Instance Method Details

#acceptable_file?Boolean

Determines if this is a file we should work with

Returns:

  • (Boolean)


43
44
45
# File 'lib/showfix/episode.rb', line 43

def acceptable_file?
  VALID_EXTENSIONS.include?(self.extension)
end

#has_episode_info?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/showfix/episode.rb', line 38

def has_episode_info?
  !@season.nil? && !@episode.nil? && @season.is_a?(Fixnum) && @episode.is_a?(Fixnum)
end

#skip?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/showfix/episode.rb', line 34

def skip?
  !acceptable_file? || !has_episode_info?
end

#to_formatted_sObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/showfix/episode.rb', line 54

def to_formatted_s

  f_series = ""
  if @series && @series.strip.size > 0
    f_series = "#{@series.strip}."
  end

  f_title = ""
  if @title && @title.strip.size > 0
    f_title = ".#{@title.strip}"
  end

  "%sS%.2dE%.2d%s.%s" % [f_series, @season, @episode, f_title, @extension]
end

#update_episode_infoObject

Extract info from the filename, and update fields as necessary



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/showfix/episode.rb', line 70

def update_episode_info
  info = @parser.parse(@file_no_ext)

  if info
    keys = info.names
    @series ||= @cleaner.clean(info[:series]) if keys.include?('series')
    @season ||= info[:season].to_i if keys.include?('season')
    @episode ||= info[:episode].to_i if keys.include?('episode')
    @title ||= @cleaner.clean(info[:title]) if keys.include?('title')
  end
end

#update_season_episode(input) ⇒ Object

Parse season/episode info inputted by the user from the commandline



48
49
50
51
52
# File 'lib/showfix/episode.rb', line 48

def update_season_episode(input)
  # 1.1 => S1, E1
  # 1.1-2 => S1, E1 & E2
  # 1 => E1 (expect season to be from command line)
end