Class: Lexicon

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

Class Method Summary collapse

Class Method Details

.parse(item) ⇒ Object



2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/lexicon.rb', line 2

def self.parse(item)
  if item.is_a?(String)
    title = item
  else
    title = item.title
  end
  
  parsed = {
    :title => nil,
    :series => nil,
    :episode => nil,
    :size => nil,
    :publish_date => nil,
    :high_def => false
  }

  tokens = title.split(/\.| /)
  titles = []
  state = :title
  
  while tokens.length > 0
    token = tokens.shift
    
    case(state)
    when :title
      if token =~ /s(\d+)e(\d+)/i
        parsed[:series] = Regexp.last_match[1].to_i
        parsed[:episode] = Regexp.last_match[2].to_i
        state = :done
      elsif token =~ /^(\d\d?)x?(\d\d)$/i
        parsed[:series] = Regexp.last_match[1].to_i
        parsed[:episode] = Regexp.last_match[2].to_i
        state = :done
      elsif token =~ /^s(\d+)$/i
        parsed[:series] = Regexp.last_match[1].to_i
        state = :series
      else
        titles << token
      end
    when :series
      if token =~ /^e(\d+)$/i
        parsed[:episode] = Regexp.last_match[1].to_i
        state = :done
      end
    when :done
      if token =~ /HDTV|720p|1080|mkv/i
        parsed[:high_def] = true
      end
    end
  end

  # Reparse to look for some hd/sd info
  if state != :done
    tokens = title.split(/\.| /)
    titles = [] if state == :title
    
    while tokens.length > 0
      token = tokens.shift
      case(state)
      when :title
        if token =~ /HDTV|720p|1080|mkv/i
          parsed[:high_def] = true
          state = :done
        else
          titles << token
        end
      when :series
        if token =~ /HDTV|720p|1080|mkv/i
          parsed[:high_def] = true
          state = :done
        end
      end
    end     
  end

  # Try to split on some common lines
  if state != :done
    tokens = title.split(/\.| /)
    titles = [] if state == :title
    
    while tokens.length > 0
      token = tokens.shift
      case(state)
      when :title
        if token =~ /vid|DVDRip|xvid|divx|repack|internal/i
          state = :done
        elsif token =~ /avi|ts|mkv|mpg/i
          state = :done
        else
          titles << token
        end
      when :series
        if token =~ /vid|DVDRip|xvid|divx|repack|internal/i
          state = :done
        elsif token =~ /avi|ts|mkv|mpg/i
          state = :done
        end
      end
    end        
  end

  if state == :title
    titles = title.split(/\.| /)
  end

  parsed[:title] = titles.join(' ')
  parsed[:publish_date] = item.published if item.published
  return parsed
end