3
from flexget.utils.titles.parser import TitleParser
4
from flexget.utils import qualities
6
log = logging.getLogger('movieparser')
9
def diff_pos(string1, string2):
10
"""Returns first position where string1 and string2 differ."""
11
for (count, c) in enumerate(string1):
12
if len(string2) <= count:
14
if string2[count] != c:
18
class MovieParser(TitleParser):
23
TitleParser.__init__(self)
29
self.quality = qualities.UNKNOWN
33
return "<MovieParser(name=%s,year=%s,quality=%s)>" % (self.name, self.year, self.quality)
35
def parse(self, data=None):
36
"""Parse movie name. Populates name, year, quality and proper_count attributes"""
38
# Reset before parsing, so the parser can be reused.
44
for char in '[]()_,.':
45
data = data.replace(char, ' ')
47
# if there are no spaces
48
if data.find(' ') == -1:
49
data = data.replace('-', ' ')
51
# remove unwanted words (imax, ..)
52
self.remove_words(data, self.remove)
54
data = self.strip_spaces(data)
57
parts = data.split(' ')
60
for part_pos, part in enumerate(parts):
62
# Don't let the first word be cutoff word
71
# if length > 3 and whole word in uppers, consider as cut word (most likely a group name)
72
if len(part) > 3 and part.isupper() and part.isalpha() and part_pos > 0:
74
# check for cutoff words
75
if part.lower() in self.cutoffs:
78
if part.lower() in self.propers:
79
self.proper_count += 1
82
if cut and parts.index(part) < cut_part:
86
log.debug('parts: %s, cut is: %s' % (parts, parts[cut_part]))
88
# calculate cut positon from cut_part
89
abs_cut = len(' '.join(parts[:cut_part]))
91
log.debug('after parts check, cut data would be: `%s` abs_cut: %i' % (data[:abs_cut], abs_cut))
94
quality, remaining = qualities.quality_match(data)
96
self.quality = quality
97
# remaining string is same as data but quality information removed
98
# find out position where there is first difference, this is earliest
99
# quality bit, anything after that has no relevance to the movie name
100
dp = diff_pos(data, remaining)
102
log.debug('quality start: %s' % dp)
104
log.debug('quality cut is even shorter')
108
data = data[:abs_cut].strip()
109
log.debug('data cut to `%s` - this will be the name' % data)
116
self.year = int(year)