Sha256: 266ebbb2abbcde035effc0af9eea0121ae576d55c9600ad8b0fbc66ab12a96bb

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

module AppleTvConverter
  class FilenameParser
    attr_accessor :tvshow_name, :season_number, :episode_number, :last_episode_number

    def initialize(path)
      @path = path
    end

    def tvshow_name
      @tvshow_name ||= parse_tvshow_name
    end

    def season_number
      @season_number ||= parse_season_number
    end

    def episode_number
      @episode_number ||= parse_episode_number
    end

    def last_episode_number
      @last_episode_number ||= parse_last_episode_number
    end

    private

    def basename
      @basename ||= File.basename(@path)
    end

    def format1_match
      # /.*?S(\d+)E(\d+)(?:(?:[-E]+(\d+))*).*?/ -> S00E01, S00E01(E02)+, S00E01(-E02)+, S00E01(-02)+, S00 E01( E02)+
      @format1_match ||= basename.match(/.*?S(\d+)\s*E(\d+)(?:(?:[-E]+(\d+)\s*)*).*?/i)
    end

    def format2_match
      # /(\d+)x(\d+)(?:(?:_?(?:\1)x(\d+))*)/ -> 0x01, 0x01(_0x02)+ , assuming same season number (0x01_1x02 fails!)
      @format2_match ||= basename.match(/(\d+)x(\d+)(?:(?:_?(?:\1)x(\d+))*)/i)
    end

    def format3_match
      # /(\d+)_?of_?\d+/i -> 1_of_12, 1of12. Only episode numbers
      @format3_match ||= basename.match(/(\d+)_?of_?\d+/i)
    end

    def parse_tvshow_name
      test_path = File.expand_path(File.basename(File.dirname(@path)) =~ /^season\s*\d+/i ? File.dirname(File.dirname(@path)) : File.dirname(@path))
      match = test_path.match(/.*\/(.*?)(?:S(\d+))?$/i)
      match[1].strip
    end

    def parse_season_number
      return format1_match[1].to_i if format1_match
      return format2_match[1].to_i if format2_match
    end

    def parse_episode_number
      return format1_match[2].to_i if format1_match
      return format2_match[2].to_i if format2_match
      return format3_match[1].to_i if format3_match
    end

    def parse_last_episode_number
      return format1_match[3].to_i if format1_match && format1_match[3]
      return format2_match[3].to_i if format2_match && format2_match[3]
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
apple-tv-converter-0.7.4 lib/apple_tv_converter/filename_parser.rb
apple-tv-converter-0.7.3 lib/apple_tv_converter/filename_parser.rb
apple-tv-converter-0.7.2 lib/apple_tv_converter/filename_parser.rb
apple-tv-converter-0.7.1 lib/apple_tv_converter/filename_parser.rb
apple-tv-converter-0.7.0 lib/apple_tv_converter/filename_parser.rb
apple-tv-converter-0.6.2 lib/apple_tv_converter/filename_parser.rb
apple-tv-converter-0.6.0 lib/apple_tv_converter/filename_parser.rb