]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/metadatafromtitle.py
a56077f206b5133f2fae3fadbcad10523353ed5a
[youtubedl] / youtube_dl / postprocessor / metadatafromtitle.py
1 from __future__ import unicode_literals
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):
15 super(MetadataFromTitlePP, self).__init__(downloader)
16 self._titleformat = titleformat
17 self._titleregex = self.format_to_regex(titleformat)
18
19 def format_to_regex(self, fmt):
20 """
21 Converts a string like
22 '%(title)s - %(artist)s'
23 to a regex like
24 '(?P<title>.+)\ \-\ (?P<artist>.+)'
25 """
26 lastpos = 0
27 regex = ""
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
47 return [], info