def __init__(self, filename, options=None, **kwargs): options = dict(options or {}) for k, v in kwargs.items(): if k not in options or not options[k]: options[ k] = v # options dict has priority over keyword arguments self._validate_options(options) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) clean_function = None if options and options.get('clean_function'): clean_function = options.get('clean_function') if not hasattr(clean_function, '__call__'): module, function = clean_function.rsplit('.') if not module: module = 'guessit.textutils' clean_function = getattr(__import__(module), function) if not clean_function: log.error( 'Can\'t find clean function %s. Default will be used.' % options.get('clean_function')) clean_function = clean_default else: clean_function = clean_default self.match_tree = MatchTree(filename, clean_function=clean_function) self.options = options self._transfo_calls = [] # sanity check: make sure we don't process a (mostly) empty string if clean_function(filename).strip() == '': return from guessit.plugins import transformers try: mtree = self.match_tree if 'type' in self.options: mtree.guess.set('type', self.options['type'], confidence=0.0) # Process for transformer in transformers.all_transformers(): disabled = options.get('disabled_transformers') if not disabled or not transformer.name in disabled: self._process(transformer, False) # Post-process for transformer in transformers.all_transformers(): disabled = options.get('disabled_transformers') if not disabled or not transformer.name in disabled: self._process(transformer, True) log.debug('Found match tree:\n%s' % u(mtree)) except TransformerException as e: log.debug('An error has occurred in Transformer %s: %s' % (e.transformer, e))
def guess_properties(string): """Extract properties from `string` using guessit's `guess_properties` transformer. :param str string: the string potentially containing properties. :return: the guessed properties. :rtype: dict """ mtree = MatchTree(string) get_transformer('guess_properties').process(mtree) return mtree.matched()
def __init__(self, filename, options=None, **kwargs): options = dict(options or {}) for k, v in kwargs.items(): if k not in options or not options[k]: options[k] = v # options dict has priority over keyword arguments self._validate_options(options) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) clean_function = None if options and options.get('clean_function'): clean_function = options.get('clean_function') if not hasattr(clean_function, '__call__'): module, function = clean_function.rsplit('.') if not module: module = 'guessit.textutils' clean_function = getattr(__import__(module), function) if not clean_function: log.error('Can\'t find clean function %s. Default will be used.' % options.get('clean_function')) clean_function = clean_default else: clean_function = clean_default self.match_tree = MatchTree(filename, clean_function=clean_function) self.options = options self._transfo_calls = [] # sanity check: make sure we don't process a (mostly) empty string if clean_function(filename).strip() == '': return from guessit.plugins import transformers try: mtree = self.match_tree if 'type' in self.options: mtree.guess.set('type', self.options['type'], confidence=0.0) # Process for transformer in transformers.all_transformers(): disabled = options.get('disabled_transformers') if not disabled or not transformer.name in disabled: self._process(transformer, False) # Post-process for transformer in transformers.all_transformers(): disabled = options.get('disabled_transformers') if not disabled or not transformer.name in disabled: self._process(transformer, True) log.debug('Found match tree:\n%s' % u(mtree)) except TransformerException as e: log.debug('An error has occurred in Transformer %s: %s' % (e.transformer, e))
def __init__(self, filename, options=None, **kwargs): options = dict(options or {}) for k, v in kwargs.items(): if k not in options or not options[k]: options[ k] = v # options dict has priority over keyword arguments self._validate_options(options) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) self.match_tree = MatchTree(filename) self.options = options self._transfo_calls = [] # sanity check: make sure we don't process a (mostly) empty string if clean_string(filename) == '': return from guessit.plugins import transformers try: mtree = self.match_tree if 'type' in self.options: mtree.guess.set('type', self.options['type'], confidence=0.0) # Process for transformer in transformers.all_transformers(): self._process(transformer, False) # Post-process for transformer in transformers.all_transformers(): self._process(transformer, True) log.debug('Found match tree:\n%s' % u(mtree)) except TransformerException as e: log.debug('An error has occurred in Transformer %s: %s' % (e.transformer, e))
def __init__(self, filename, filetype='autodetect', opts=None, transfo_opts=None): if opts is None: opts = [] if not isinstance(opts, list): raise ValueError('opts must be a list of option names! Received: type=%s val=%s', type(opts), opts) if transfo_opts is None: transfo_opts = {} if not isinstance(transfo_opts, dict): raise ValueError('transfo_opts must be a dict of { transfo_name: (args, kwargs) }. ' + 'Received: type=%s val=%s', type(transfo_opts), transfo_opts) valid_filetypes = ('autodetect', 'subtitle', 'info', 'video', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo') if filetype not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % valid_filetypes) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) self.filename = filename self.match_tree = MatchTree(filename) self.filetype = filetype self.opts = opts self.transfo_opts = transfo_opts self._transfo_calls = [] # sanity check: make sure we don't process a (mostly) empty string if clean_string(filename) == '': return try: mtree = self.match_tree mtree.guess.set('type', filetype, confidence=1.0) for transformer in transformers.extensions.objects(): self._apply_transfo(transformer) log.debug('Found match tree:\n%s' % u(mtree)) except TransfoException as e: log.debug('An error has occured in Transformer %s: %s' % (e.transformer, e))
def __init__(self, filename, options=None, **kwargs): options = dict(options or {}) for k, v in kwargs.items(): if k not in options or not options[k]: options[k] = v # options dict has priority over keyword arguments self._validate_options(options) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) self.match_tree = MatchTree(filename) self.options = options self._transfo_calls = [] # sanity check: make sure we don't process a (mostly) empty string if clean_string(filename) == '': return from guessit.plugins import transformers try: mtree = self.match_tree if 'type' in self.options: mtree.guess.set('type', self.options['type'], confidence=0.0) # Process for transformer in transformers.all_transformers(): self._process(transformer, False) # Post-process for transformer in transformers.all_transformers(): self._process(transformer, True) log.debug('Found match tree:\n%s' % u(mtree)) except TransformerException as e: log.debug('An error has occured in Transformer %s: %s' % (e.transformer, e))
def __init__(self, filename, filetype='autodetect'): """An iterative matcher tries to match different patterns that appear in the filename. The 'filetype' argument indicates which type of file you want to match. If it is 'autodetect', the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized 'filetype' values are: [ autodetect, subtitle, movie, moviesubtitle, episode, episodesubtitle ] The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, x264 is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter'v' in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. """ valid_filetypes = ('autodetect', 'subtitle', 'video', 'movie', 'moviesubtitle', 'episode', 'episodesubtitle') if filetype not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % valid_filetypes) if not isinstance(filename, unicode): log.debug('WARNING: given filename to matcher is not unicode...') self.match_tree = MatchTree(filename) mtree = self.match_tree mtree.guess.set('type', filetype, confidence=1.0) def apply_transfo(transfo_name, *args, **kwargs): transfo = __import__('guessit.transfo.' + transfo_name, globals=globals(), locals=locals(), fromlist=['process'], level=-1) transfo.process(mtree, *args, **kwargs) # 1- first split our path into dirs + basename + ext apply_transfo('split_path_components') # 2- guess the file type now (will be useful later) apply_transfo('guess_filetype', filetype) if mtree.guess['type'] == 'unknown': return # 3- split each of those into explicit groups (separated by parentheses # or square brackets) apply_transfo('split_explicit_groups') # 4- try to match information for specific patterns if mtree.guess['type'] in ('episode', 'episodesubtitle'): strategy = ['guess_date', 'guess_video_rexps', 'guess_episodes_rexps', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_weak_episodes_rexps', 'guess_language'] else: strategy = ['guess_date', 'guess_year', 'guess_video_rexps', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language'] for name in strategy: apply_transfo(name) # more guessers for both movies and episodes for name in ['guess_bonus_features']: apply_transfo(name) # split into '-' separated subgroups (with required separator chars # around the dash) apply_transfo('split_on_dash') # 5- try to identify the remaining unknown groups by looking at their # position relative to other known elements if mtree.guess['type'] in ('episode', 'episodesubtitle'): apply_transfo('guess_episode_info_from_position') else: apply_transfo('guess_movie_title_from_position') # 6- perform some post-processing steps apply_transfo('post_process') log.debug('Found match tree:\n%s' % (to_utf8(unicode(mtree))))
class IterativeMatcher(object): """An iterative matcher tries to match different patterns that appear in the filename. The ``filetype`` argument indicates which type of file you want to match. If it is undefined, the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized ``filetype`` values are: ``['subtitle', 'info', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo']`` ``options`` is a dict of options values to be passed to the transformations used by the matcher. The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following:: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, ``x264`` (in the middle) is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter ``v`` in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. """ def __init__(self, filename, options=None, **kwargs): options = dict(options or {}) for k, v in kwargs.items(): if k not in options or not options[k]: options[k] = v # options dict has priority over keyword arguments self._validate_options(options) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) if options and options.get('clean_function'): clean_function = options.get('clean_function') if not hasattr(clean_function, '__call__'): module, function = clean_function.rsplit('.') if not module: module = 'guessit.textutils' clean_function = getattr(__import__(module), function) if not clean_function: log.error('Can\'t find clean function %s. Default will be used.' % options.get('clean_function')) clean_function = clean_default else: clean_function = clean_default self.match_tree = MatchTree(filename, clean_function=clean_function) self.options = options self._transfo_calls = [] # sanity check: make sure we don't process a (mostly) empty string if clean_function(filename).strip() == '': return from guessit.plugins import transformers try: mtree = self.match_tree if 'type' in self.options: mtree.guess.set('type', self.options['type'], confidence=0.0) # Process for transformer in transformers.all_transformers(): disabled = options.get('disabled_transformers') if not disabled or transformer.name not in disabled: self._process(transformer, False) # Post-process for transformer in transformers.all_transformers(): disabled = options.get('disabled_transformers') if not disabled or transformer.name not in disabled: self._process(transformer, True) log.debug('Found match tree:\n%s' % u(mtree)) except TransformerException as e: log.debug('An error has occurred in Transformer %s: %s' % (e.transformer, e)) def _process(self, transformer, post=False): if not hasattr(transformer, 'should_process') or transformer.should_process(self.match_tree, self.options): if post: transformer.post_process(self.match_tree, self.options) else: transformer.process(self.match_tree, self.options) self._transfo_calls.append(transformer) @property def second_pass_options(self): second_pass_options = {} for transformer in self._transfo_calls: if hasattr(transformer, 'second_pass_options'): transformer_second_pass_options = transformer.second_pass_options(self.match_tree, self.options) if transformer_second_pass_options: second_pass_options.update(transformer_second_pass_options) return second_pass_options @staticmethod def _validate_options(options): valid_filetypes = ('subtitle', 'info', 'video', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo') type_ = options.get('type') if type_ and type_ not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % (valid_filetypes,)) def matched(self): return self.match_tree.matched()
class IterativeMatcher(object): """An iterative matcher tries to match different patterns that appear in the filename. The ``filetype`` argument indicates which type of file you want to match. If it is undefined, the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized ``filetype`` values are: ``['subtitle', 'info', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo']`` ``options`` is a dict of options values to be passed to the transformations used by the matcher. The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following:: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, ``x264`` (in the middle) is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter ``v`` in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. """ def __init__(self, filename, options=None, **kwargs): options = dict(options or {}) for k, v in kwargs.items(): if k not in options or not options[k]: options[ k] = v # options dict has priority over keyword arguments self._validate_options(options) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) self.match_tree = MatchTree(filename) self.options = options self._transfo_calls = [] # sanity check: make sure we don't process a (mostly) empty string if clean_string(filename) == '': return from guessit.plugins import transformers try: mtree = self.match_tree if 'type' in self.options: mtree.guess.set('type', self.options['type'], confidence=0.0) # Process for transformer in transformers.all_transformers(): self._process(transformer, False) # Post-process for transformer in transformers.all_transformers(): self._process(transformer, True) log.debug('Found match tree:\n%s' % u(mtree)) except TransformerException as e: log.debug('An error has occurred in Transformer %s: %s' % (e.transformer, e)) def _process(self, transformer, post=False): if not hasattr(transformer, 'should_process') or transformer.should_process( self.match_tree, self.options): if post: transformer.post_process(self.match_tree, self.options) else: transformer.process(self.match_tree, self.options) self._transfo_calls.append(transformer) @property def second_pass_options(self): second_pass_options = {} for transformer in self._transfo_calls: if hasattr(transformer, 'second_pass_options'): transformer_second_pass_options = transformer.second_pass_options( self.match_tree, self.options) if transformer_second_pass_options: second_pass_options.update(transformer_second_pass_options) return second_pass_options def _validate_options(self, options): valid_filetypes = ('subtitle', 'info', 'video', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo') type = options.get('type') if type and type not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % (valid_filetypes, )) def matched(self): return self.match_tree.matched()
class IterativeMatcher(object): def __init__(self, filename, filetype='autodetect'): """An iterative matcher tries to match different patterns that appear in the filename. The 'filetype' argument indicates which type of file you want to match. If it is 'autodetect', the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized 'filetype' values are: [ autodetect, subtitle, movie, moviesubtitle, episode, episodesubtitle ] The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, x264 is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter'v' in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. """ valid_filetypes = ('autodetect', 'subtitle', 'video', 'movie', 'moviesubtitle', 'episode', 'episodesubtitle') if filetype not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % valid_filetypes) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') self.match_tree = MatchTree(filename) mtree = self.match_tree mtree.guess.set('type', filetype, confidence=1.0) def apply_transfo(transfo_name, *args, **kwargs): transfo = __import__('guessit.transfo.' + transfo_name, globals=globals(), locals=locals(), fromlist=['process'], level=-1) transfo.process(mtree, *args, **kwargs) # 1- first split our path into dirs + basename + ext apply_transfo('split_path_components') # 2- guess the file type now (will be useful later) apply_transfo('guess_filetype', filetype) if mtree.guess['type'] == 'unknown': return # 3- split each of those into explicit groups (separated by parentheses # or square brackets) apply_transfo('split_explicit_groups') # 4- try to match information for specific patterns # NOTE: order needs to comply to the following: # - website before language (eg: tvu.org.ru vs russian) # - language before episodes_rexps # - properties before language (eg: he-aac vs hebrew) # - release_group before properties (eg: XviD-?? vs xvid) if mtree.guess['type'] in ('episode', 'episodesubtitle'): strategy = [ 'guess_date', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language', 'guess_video_rexps', 'guess_episodes_rexps', 'guess_weak_episodes_rexps' ] else: strategy = [ 'guess_date', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language', 'guess_video_rexps' ] for name in strategy: apply_transfo(name) # more guessers for both movies and episodes for name in ['guess_bonus_features', 'guess_year', 'guess_country']: apply_transfo(name) # split into '-' separated subgroups (with required separator chars # around the dash) apply_transfo('split_on_dash') # 5- try to identify the remaining unknown groups by looking at their # position relative to other known elements if mtree.guess['type'] in ('episode', 'episodesubtitle'): apply_transfo('guess_episode_info_from_position') else: apply_transfo('guess_movie_title_from_position') # 6- perform some post-processing steps apply_transfo('post_process') log.debug('Found match tree:\n%s' % u(mtree)) def matched(self): # we need to make a copy here, as the merge functions work in place and # calling them on the match tree would modify it parts = [node.guess for node in self.match_tree.nodes() if node.guess] parts = copy.deepcopy(parts) # 1- try to merge similar information together and give it a higher # confidence for int_part in ('year', 'season', 'episodeNumber'): merge_similar_guesses(parts, int_part, choose_int) for string_part in ('title', 'series', 'container', 'format', 'releaseGroup', 'website', 'audioCodec', 'videoCodec', 'screenSize', 'episodeFormat', 'audioChannels'): merge_similar_guesses(parts, string_part, choose_string) # 2- merge the rest, potentially discarding information not properly # merged before result = merge_all(parts, append=['language', 'subtitleLanguage', 'other']) log.debug('Final result: ' + result.nice_string()) return result
def process_node(self, node, iterative=True, partial_span=None): if partial_span: value = node.value[partial_span[0]:partial_span[1]] else: value = node.value string = ' %s ' % value # add sentinels if not self.options: matcher_result = self.guess_func(string, node) else: matcher_result = self.guess_func(string, node, self.options) if matcher_result: if not isinstance(matcher_result, Guess): result, span = matcher_result else: result, span = matcher_result, matcher_result.metadata().span if result: # readjust span to compensate for sentinels span = (span[0] - 1, span[1] - 1) # readjust span to compensate for partial_span if partial_span: span = (span[0] + partial_span[0], span[1] + partial_span[0]) partition_spans = None if self.options and 'skip_nodes' in self.options: skip_nodes = self.options.get('skip_nodes') for skip_node in skip_nodes: if skip_node.parent.node_idx == node.node_idx[:len(skip_node.parent.node_idx)] and\ skip_node.span == span or\ skip_node.span == (span[0] + skip_node.offset, span[1] + skip_node.offset): if partition_spans is None: partition_spans = _get_split_spans(node, skip_node.span) else: new_partition_spans = [] for partition_span in partition_spans: tmp_node = MatchTree(value, span=partition_span, parent=node) tmp_partitions_spans = _get_split_spans(tmp_node, skip_node.span) new_partition_spans.extend(tmp_partitions_spans) partition_spans.extend(new_partition_spans) if not partition_spans: # restore sentinels compensation if isinstance(result, Guess): guess = result else: guess = Guess(result, confidence=self.confidence, input=string, span=span) if not iterative: found_guess(node, guess, logger=self.logger) else: absolute_span = (span[0] + node.offset, span[1] + node.offset) node.partition(span) if node.is_leaf(): found_guess(node, guess, logger=self.logger) else: found_child = None for child in node.children: if child.span == absolute_span: found_guess(child, guess, logger=self.logger) found_child = child break for child in node.children: if child is not found_child: self.process_node(child) else: for partition_span in partition_spans: self.process_node(node, partial_span=partition_span)
def __init__(self, filename, filetype='autodetect'): """An iterative matcher tries to match different patterns that appear in the filename. The 'filetype' argument indicates which type of file you want to match. If it is 'autodetect', the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized 'filetype' values are: [ autodetect, subtitle, movie, moviesubtitle, episode, episodesubtitle ] The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, x264 is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter'v' in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. """ valid_filetypes = ('autodetect', 'subtitle', 'video', 'movie', 'moviesubtitle', 'episode', 'episodesubtitle') if filetype not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % valid_filetypes) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') self.match_tree = MatchTree(filename) mtree = self.match_tree mtree.guess.set('type', filetype, confidence=1.0) def apply_transfo(transfo_name, *args, **kwargs): transfo = __import__('guessit.transfo.' + transfo_name, globals=globals(), locals=locals(), fromlist=['process'], level=-1) transfo.process(mtree, *args, **kwargs) # 1- first split our path into dirs + basename + ext apply_transfo('split_path_components') # 2- guess the file type now (will be useful later) apply_transfo('guess_filetype', filetype) if mtree.guess['type'] == 'unknown': return # 3- split each of those into explicit groups (separated by parentheses # or square brackets) apply_transfo('split_explicit_groups') # 4- try to match information for specific patterns # NOTE: order needs to comply to the following: # - website before language (eg: tvu.org.ru vs russian) # - language before episodes_rexps # - properties before language (eg: he-aac vs hebrew) # - release_group before properties (eg: XviD-?? vs xvid) if mtree.guess['type'] in ('episode', 'episodesubtitle'): strategy = [ 'guess_date', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language', 'guess_video_rexps', 'guess_episodes_rexps', 'guess_weak_episodes_rexps' ] else: strategy = [ 'guess_date', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language', 'guess_video_rexps' ] for name in strategy: apply_transfo(name) # more guessers for both movies and episodes for name in ['guess_bonus_features', 'guess_year', 'guess_country']: apply_transfo(name) # split into '-' separated subgroups (with required separator chars # around the dash) apply_transfo('split_on_dash') # 5- try to identify the remaining unknown groups by looking at their # position relative to other known elements if mtree.guess['type'] in ('episode', 'episodesubtitle'): apply_transfo('guess_episode_info_from_position') else: apply_transfo('guess_movie_title_from_position') # 6- perform some post-processing steps apply_transfo('post_process') log.debug('Found match tree:\n%s' % u(mtree))
def __init__(self, filename, filetype='autodetect', opts=None, transfo_opts=None): """An iterative matcher tries to match different patterns that appear in the filename. The 'filetype' argument indicates which type of file you want to match. If it is 'autodetect', the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized 'filetype' values are: [ autodetect, subtitle, info, movie, moviesubtitle, movieinfo, episode, episodesubtitle, episodeinfo ] The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, x264 is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter'v' in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. When you create the Matcher, you can pass it: - a list 'opts' of option names, that act as global flags - a dict 'transfo_opts' of { transfo_name: (transfo_args, transfo_kwargs) } with which to call the transfo.process() function. """ valid_filetypes = ('autodetect', 'subtitle', 'info', 'video', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo') if filetype not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % valid_filetypes) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) if opts is None: opts = [] if not isinstance(opts, list): raise ValueError('opts must be a list of option names! Received: type=%s val=%s', type(opts), opts) if transfo_opts is None: transfo_opts = {} if not isinstance(transfo_opts, dict): raise ValueError('transfo_opts must be a dict of { transfo_name: (args, kwargs) }. '+ 'Received: type=%s val=%s', type(transfo_opts), transfo_opts) self.match_tree = MatchTree(filename) # sanity check: make sure we don't process a (mostly) empty string if clean_string(filename) == '': return mtree = self.match_tree mtree.guess.set('type', filetype, confidence=1.0) def apply_transfo(transfo_name, *args, **kwargs): transfo = __import__('guessit.transfo.' + transfo_name, globals=globals(), locals=locals(), fromlist=['process'], level=0) default_args, default_kwargs = transfo_opts.get(transfo_name, ((), {})) all_args = args or default_args all_kwargs = dict(default_kwargs) all_kwargs.update(kwargs) # keep all kwargs merged together transfo.process(mtree, *all_args, **all_kwargs) # 1- first split our path into dirs + basename + ext apply_transfo('split_path_components') # 2- guess the file type now (will be useful later) apply_transfo('guess_filetype', filetype) if mtree.guess['type'] == 'unknown': return # 3- split each of those into explicit groups (separated by parentheses # or square brackets) apply_transfo('split_explicit_groups') # 4- try to match information for specific patterns # NOTE: order needs to comply to the following: # - website before language (eg: tvu.org.ru vs russian) # - language before episodes_rexps # - properties before language (eg: he-aac vs hebrew) # - release_group before properties (eg: XviD-?? vs xvid) if mtree.guess['type'] in ('episode', 'episodesubtitle', 'episodeinfo'): strategy = [ 'guess_date', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language', 'guess_video_rexps', 'guess_episodes_rexps', 'guess_weak_episodes_rexps' ] else: strategy = [ 'guess_date', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language', 'guess_video_rexps' ] if 'nolanguage' in opts: strategy.remove('guess_language') for name in strategy: apply_transfo(name) # more guessers for both movies and episodes apply_transfo('guess_bonus_features') apply_transfo('guess_year', skip_first_year=('skip_first_year' in opts)) if 'nocountry' not in opts: apply_transfo('guess_country') apply_transfo('guess_idnumber') # split into '-' separated subgroups (with required separator chars # around the dash) apply_transfo('split_on_dash') # 5- try to identify the remaining unknown groups by looking at their # position relative to other known elements if mtree.guess['type'] in ('episode', 'episodesubtitle', 'episodeinfo'): apply_transfo('guess_episode_info_from_position') else: apply_transfo('guess_movie_title_from_position') # 6- perform some post-processing steps apply_transfo('post_process') log.debug('Found match tree:\n%s' % u(mtree))
class IterativeMatcher(object): """An iterative matcher tries to match different patterns that appear in the filename. The ``filetype`` argument indicates which type of file you want to match. If it is ``'autodetect'``, the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized ``filetype`` values are: ``['autodetect', 'subtitle', 'info', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo']`` ``opts`` is a list of option names, that act as global flags for the matcher ``transfo_opts`` is a dict of args to be passed to the transformations used by the matcher. Its schema is: ``{ transfo_name: (transfo_args, transfo_kwargs) }`` The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following:: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, ``x264`` (in the middle) is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter ``v`` in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. """ def __init__(self, filename, filetype='autodetect', opts=None, transfo_opts=None): if opts is None: opts = [] if not isinstance(opts, list): raise ValueError('opts must be a list of option names! Received: type=%s val=%s', type(opts), opts) if transfo_opts is None: transfo_opts = {} if not isinstance(transfo_opts, dict): raise ValueError('transfo_opts must be a dict of { transfo_name: (args, kwargs) }. ' + 'Received: type=%s val=%s', type(transfo_opts), transfo_opts) valid_filetypes = ('autodetect', 'subtitle', 'info', 'video', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo') if filetype not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % valid_filetypes) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) self.filename = filename self.match_tree = MatchTree(filename) self.filetype = filetype self.opts = opts self.transfo_opts = transfo_opts self._transfo_calls = [] # sanity check: make sure we don't process a (mostly) empty string if clean_string(filename) == '': return try: mtree = self.match_tree mtree.guess.set('type', filetype, confidence=1.0) for transformer in transformers.extensions.objects(): self._apply_transfo(transformer) log.debug('Found match tree:\n%s' % u(mtree)) except TransfoException as e: log.debug('An error has occured in Transformer %s: %s' % (e.transformer, e)) def _apply_transfo(self, transformer, *args, **kwargs): default_args, default_kwargs = self.transfo_opts.get(transformer.fullname, ((), {})) all_args = args or default_args or () all_kwargs = dict(default_kwargs) if default_kwargs else {} all_kwargs.update(kwargs) # keep all kwargs merged together if not hasattr(transformer, 'should_process') or transformer.should_process(self): transformer.process(self.match_tree, *all_args, **all_kwargs) self._transfo_calls.append((transformer, all_args, all_kwargs)) @property def second_pass_options(self): opts = list(self.opts) transfo_opts = dict(self.transfo_opts.items()) for transformer, _, _ in self._transfo_calls: if hasattr(transformer, 'second_pass_options'): c_opts, c_transfo_opts = transformer.second_pass_options(self.match_tree) if c_opts or c_transfo_opts: transfo_opts[transformer.fullname] = c_opts, c_transfo_opts return opts, transfo_opts def matched(self): return self.match_tree.matched()
def __init__(self, filename, filetype="autodetect", opts=None): """An iterative matcher tries to match different patterns that appear in the filename. The 'filetype' argument indicates which type of file you want to match. If it is 'autodetect', the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized 'filetype' values are: [ autodetect, subtitle, movie, moviesubtitle, episode, episodesubtitle ] The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, x264 is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter'v' in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. """ valid_filetypes = ("autodetect", "subtitle", "video", "movie", "moviesubtitle", "episode", "episodesubtitle") if filetype not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % valid_filetypes) if not PY3 and not isinstance(filename, unicode): log.warning("Given filename to matcher is not unicode...") filename = filename.decode("utf-8") filename = normalize_unicode(filename) if opts is None: opts = [] elif isinstance(opts, base_text_type): opts = opts.split() self.match_tree = MatchTree(filename) mtree = self.match_tree mtree.guess.set("type", filetype, confidence=1.0) def apply_transfo(transfo_name, *args, **kwargs): transfo = __import__( "guessit.transfo." + transfo_name, globals=globals(), locals=locals(), fromlist=["process"], level=0 ) transfo.process(mtree, *args, **kwargs) # 1- first split our path into dirs + basename + ext apply_transfo("split_path_components") # 2- guess the file type now (will be useful later) apply_transfo("guess_filetype", filetype) if mtree.guess["type"] == "unknown": return # 3- split each of those into explicit groups (separated by parentheses # or square brackets) apply_transfo("split_explicit_groups") # 4- try to match information for specific patterns # NOTE: order needs to comply to the following: # - website before language (eg: tvu.org.ru vs russian) # - language before episodes_rexps # - properties before language (eg: he-aac vs hebrew) # - release_group before properties (eg: XviD-?? vs xvid) if mtree.guess["type"] in ("episode", "episodesubtitle"): strategy = [ "guess_date", "guess_website", "guess_release_group", "guess_properties", "guess_language", "guess_video_rexps", "guess_episodes_rexps", "guess_weak_episodes_rexps", ] else: strategy = [ "guess_date", "guess_website", "guess_release_group", "guess_properties", "guess_language", "guess_video_rexps", ] if "nolanguage" in opts: strategy.remove("guess_language") for name in strategy: apply_transfo(name) # more guessers for both movies and episodes apply_transfo("guess_bonus_features") apply_transfo("guess_year", skip_first_year=("skip_first_year" in opts)) if "nocountry" not in opts: apply_transfo("guess_country") # split into '-' separated subgroups (with required separator chars # around the dash) apply_transfo("split_on_dash") # 5- try to identify the remaining unknown groups by looking at their # position relative to other known elements if mtree.guess["type"] in ("episode", "episodesubtitle"): apply_transfo("guess_episode_info_from_position") else: apply_transfo("guess_movie_title_from_position") # 6- perform some post-processing steps apply_transfo("post_process") log.debug("Found match tree:\n%s" % u(mtree))
class IterativeMatcher(object): def __init__(self, filename, filetype='autodetect', opts=None, transfo_opts=None): """An iterative matcher tries to match different patterns that appear in the filename. The 'filetype' argument indicates which type of file you want to match. If it is 'autodetect', the matcher will try to see whether it can guess that the file corresponds to an episode, or otherwise will assume it is a movie. The recognized 'filetype' values are: [ autodetect, subtitle, info, movie, moviesubtitle, movieinfo, episode, episodesubtitle, episodeinfo ] The IterativeMatcher works mainly in 2 steps: First, it splits the filename into a match_tree, which is a tree of groups which have a semantic meaning, such as episode number, movie title, etc... The match_tree created looks like the following: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000 111 0000011111111111112222222222222233333333444444444444444455555555666777777778888888 000 0000000000000000000000000000000001111112011112222333333401123334000011233340000000 000 __________________(The.Prestige).______.[____.HP.______.{__-___}.St{__-___}.Chaps].___ xxxxxttttttttttttt ffffff vvvv xxxxxx ll lll xx xxx ccc [XCT].Le.Prestige.(The.Prestige).DVDRip.[x264.HP.He-Aac.{Fr-Eng}.St{Fr-Eng}.Chaps].mkv The first 3 lines indicates the group index in which a char in the filename is located. So for instance, x264 is the group (0, 4, 1), and it corresponds to a video codec, denoted by the letter'v' in the 4th line. (for more info, see guess.matchtree.to_string) Second, it tries to merge all this information into a single object containing all the found properties, and does some (basic) conflict resolution when they arise. When you create the Matcher, you can pass it: - a list 'opts' of option names, that act as global flags - a dict 'transfo_opts' of { transfo_name: (transfo_args, transfo_kwargs) } with which to call the transfo.process() function. """ valid_filetypes = ('autodetect', 'subtitle', 'info', 'video', 'movie', 'moviesubtitle', 'movieinfo', 'episode', 'episodesubtitle', 'episodeinfo') if filetype not in valid_filetypes: raise ValueError("filetype needs to be one of %s" % valid_filetypes) if not PY3 and not isinstance(filename, unicode): log.warning('Given filename to matcher is not unicode...') filename = filename.decode('utf-8') filename = normalize_unicode(filename) if opts is None: opts = [] if not isinstance(opts, list): raise ValueError( 'opts must be a list of option names! Received: type=%s val=%s', type(opts), opts) if transfo_opts is None: transfo_opts = {} if not isinstance(transfo_opts, dict): raise ValueError( 'transfo_opts must be a dict of { transfo_name: (args, kwargs) }. ' + 'Received: type=%s val=%s', type(transfo_opts), transfo_opts) self.match_tree = MatchTree(filename) # sanity check: make sure we don't process a (mostly) empty string if clean_string(filename) == '': return mtree = self.match_tree mtree.guess.set('type', filetype, confidence=1.0) def apply_transfo(transfo_name, *args, **kwargs): transfo = __import__('guessit.transfo.' + transfo_name, globals=globals(), locals=locals(), fromlist=['process'], level=0) default_args, default_kwargs = transfo_opts.get( transfo_name, ((), {})) all_args = args or default_args all_kwargs = dict(default_kwargs) all_kwargs.update(kwargs) # keep all kwargs merged together transfo.process(mtree, *all_args, **all_kwargs) # 1- first split our path into dirs + basename + ext apply_transfo('split_path_components') # 2- guess the file type now (will be useful later) apply_transfo('guess_filetype', filetype) if mtree.guess['type'] == 'unknown': return # 3- split each of those into explicit groups (separated by parentheses # or square brackets) apply_transfo('split_explicit_groups') # 4- try to match information for specific patterns # NOTE: order needs to comply to the following: # - website before language (eg: tvu.org.ru vs russian) # - language before episodes_rexps # - properties before language (eg: he-aac vs hebrew) # - release_group before properties (eg: XviD-?? vs xvid) if mtree.guess['type'] in ('episode', 'episodesubtitle', 'episodeinfo'): strategy = [ 'guess_date', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language', 'guess_video_rexps', 'guess_episodes_rexps', 'guess_weak_episodes_rexps' ] else: strategy = [ 'guess_date', 'guess_website', 'guess_release_group', 'guess_properties', 'guess_language', 'guess_video_rexps' ] if 'nolanguage' in opts: strategy.remove('guess_language') for name in strategy: apply_transfo(name) # more guessers for both movies and episodes apply_transfo('guess_bonus_features') apply_transfo('guess_year', skip_first_year=('skip_first_year' in opts)) if 'nocountry' not in opts: apply_transfo('guess_country') apply_transfo('guess_idnumber') # split into '-' separated subgroups (with required separator chars # around the dash) apply_transfo('split_on_dash') # 5- try to identify the remaining unknown groups by looking at their # position relative to other known elements if mtree.guess['type'] in ('episode', 'episodesubtitle', 'episodeinfo'): apply_transfo('guess_episode_info_from_position') else: apply_transfo('guess_movie_title_from_position') # 6- perform some post-processing steps apply_transfo('post_process') log.debug('Found match tree:\n%s' % u(mtree)) def matched(self): return self.match_tree.matched()