]> jfr.im git - yt-dlp.git/blame - youtube_dl/postprocessor/metadatafromtitle.py
Fix "invalid escape sequences" error on Python 3.6
[yt-dlp.git] / youtube_dl / postprocessor / metadatafromtitle.py
CommitLineData
88cf6fb3 1from __future__ import unicode_literals
e7db87f7 2
3import re
4
5from .common import PostProcessor
e7db87f7 6
7
8class 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