]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/metadatafromtitle.py
164edd3a820af4d0c3d1af48b9cf81a6b5460e9b
[youtubedl] / youtube_dl / postprocessor / metadatafromtitle.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import PostProcessor
6
7
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)
13
14 def format_to_regex(self, fmt):
15 r"""
16 Converts a string like
17 '%(title)s - %(artist)s'
18 to a regex like
19 '(?P<title>.+)\ \-\ (?P<artist>.+)'
20 """
21 lastpos = 0
22 regex = ''
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:
36 self._downloader.to_screen('[fromtitle] Could not interpret title of video as "%s"' % self._titleformat)
37 return [], info
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
43 return [], info