1 from __future__ import unicode_literals
5 from .common import PostProcessor
8 class MetadataFromTitlePP(PostProcessor):
9 def __init__(self, downloader, titleformat):
10 super(MetadataFromTitlePP, self).__init__(downloader)
11 self._titleformat = titleformat
12 self._titleregex = self.format_to_regex(titleformat)
14 def format_to_regex(self, fmt):
16 Converts a string like
17 '%(title)s - %(artist)s'
19 '(?P<title>.+)\ \-\ (?P<artist>.+)'
23 # replace %(..)s with regex group and escape other string parts
24 for match in re.finditer(r'%\((\w+)\)s', fmt):
25 regex += re.escape(fmt[lastpos:match.start()])
26 regex += r'(?P<' + match.group(1) + '>.+)'
28 if lastpos < len(fmt):
29 regex += re.escape(fmt[lastpos:len(fmt)])
34 match = re.match(self._titleregex, title)
36 self._downloader.to_screen('[fromtitle] Could not interpret title of video as "%s"' % self._titleformat)
38 for attribute, value in match.groupdict().items():
39 value = match.group(attribute)
40 info[attribute] = value
41 self._downloader.to_screen('[fromtitle] parsed ' + attribute + ': ' + value)