]>
Commit | Line | Data |
---|---|---|
88cf6fb3 | 1 | from __future__ import unicode_literals |
e7db87f7 | 2 | |
3 | import re | |
4 | ||
5 | from .common import PostProcessor | |
6 | from ..utils import PostProcessingError | |
7 | ||
8 | ||
9 | class MetadataFromTitlePPError(PostProcessingError): | |
10 | pass | |
11 | ||
12 | ||
13 | class MetadataFromTitlePP(PostProcessor): | |
14 | def __init__(self, downloader, titleformat): | |
88cf6fb3 | 15 | super(MetadataFromTitlePP, self).__init__(downloader) |
e7db87f7 | 16 | self._titleformat = titleformat |
88cf6fb3 | 17 | self._titleregex = self.format_to_regex(titleformat) |
e7db87f7 | 18 | |
88cf6fb3 | 19 | def format_to_regex(self, fmt): |
e7db87f7 | 20 | """ |
21 | Converts a string like | |
22 | '%(title)s - %(artist)s' | |
23 | to a regex like | |
24 | '(?P<title>.+)\ \-\ (?P<artist>.+)' | |
e7db87f7 | 25 | """ |
26 | lastpos = 0 | |
611c1dd9 | 27 | regex = '' |
e7db87f7 | 28 | # replace %(..)s with regex group and escape other string parts |
29 | for match in re.finditer(r'%\((\w+)\)s', fmt): | |
30 | regex += re.escape(fmt[lastpos:match.start()]) | |
31 | regex += r'(?P<' + match.group(1) + '>.+)' | |
32 | lastpos = match.end() | |
33 | if lastpos < len(fmt): | |
34 | regex += re.escape(fmt[lastpos:len(fmt)]) | |
35 | return regex | |
36 | ||
37 | def run(self, info): | |
38 | title = info['title'] | |
39 | match = re.match(self._titleregex, title) | |
40 | if match is None: | |
41 | raise MetadataFromTitlePPError('Could not interpret title of video as "%s"' % self._titleformat) | |
42 | for attribute, value in match.groupdict().items(): | |
43 | value = match.group(attribute) | |
44 | info[attribute] = value | |
45 | self._downloader.to_screen('[fromtitle] parsed ' + attribute + ': ' + value) | |
46 | ||
592e97e8 | 47 | return [], info |