]>
Commit | Line | Data |
---|---|---|
88cf6fb3 | 1 | from __future__ import unicode_literals |
e7db87f7 | 2 | |
3 | import re | |
4 | ||
5 | from .common import PostProcessor | |
e7db87f7 | 6 | |
7 | ||
8 | class MetadataFromTitlePP(PostProcessor): | |
9 | def __init__(self, downloader, titleformat): | |
88cf6fb3 | 10 | super(MetadataFromTitlePP, self).__init__(downloader) |
e7db87f7 | 11 | self._titleformat = titleformat |
88cf6fb3 | 12 | self._titleregex = self.format_to_regex(titleformat) |
e7db87f7 | 13 | |
88cf6fb3 | 14 | def format_to_regex(self, fmt): |
ec85ded8 | 15 | r""" |
e7db87f7 | 16 | Converts a string like |
17 | '%(title)s - %(artist)s' | |
18 | to a regex like | |
19 | '(?P<title>.+)\ \-\ (?P<artist>.+)' | |
e7db87f7 | 20 | """ |
21 | lastpos = 0 | |
611c1dd9 | 22 | regex = '' |
e7db87f7 | 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) + '>.+)' | |
27 | lastpos = match.end() | |
28 | if lastpos < len(fmt): | |
29 | regex += re.escape(fmt[lastpos:len(fmt)]) | |
30 | return regex | |
31 | ||
32 | def run(self, info): | |
33 | title = info['title'] | |
34 | match = re.match(self._titleregex, title) | |
35 | if match is None: | |
5e42f8a0 | 36 | self._downloader.to_screen('[fromtitle] Could not interpret title of video as "%s"' % self._titleformat) |
37 | return [], info | |
e7db87f7 | 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) | |
42 | ||
592e97e8 | 43 | return [], info |