]> jfr.im git - yt-dlp.git/blame - youtube_dlc/postprocessor/metadatafromtitle.py
Merge branch 'rai-update' of https://github.com/iamleot/youtube-dl into iamleot-rai...
[yt-dlp.git] / youtube_dlc / 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
fa26734e
S
12 self._titleregex = (self.format_to_regex(titleformat)
13 if re.search(r'%\(\w+\)s', titleformat)
14 else titleformat)
e7db87f7 15
88cf6fb3 16 def format_to_regex(self, fmt):
ec85ded8 17 r"""
e7db87f7 18 Converts a string like
19 '%(title)s - %(artist)s'
20 to a regex like
21 '(?P<title>.+)\ \-\ (?P<artist>.+)'
e7db87f7 22 """
23 lastpos = 0
611c1dd9 24 regex = ''
e7db87f7 25 # replace %(..)s with regex group and escape other string parts
26 for match in re.finditer(r'%\((\w+)\)s', fmt):
27 regex += re.escape(fmt[lastpos:match.start()])
28 regex += r'(?P<' + match.group(1) + '>.+)'
29 lastpos = match.end()
30 if lastpos < len(fmt):
40fcba5e 31 regex += re.escape(fmt[lastpos:])
e7db87f7 32 return regex
33
34 def run(self, info):
35 title = info['title']
36 match = re.match(self._titleregex, title)
37 if match is None:
be80986e
S
38 self._downloader.to_screen(
39 '[fromtitle] Could not interpret title of video as "%s"'
40 % self._titleformat)
5e42f8a0 41 return [], info
e7db87f7 42 for attribute, value in match.groupdict().items():
e7db87f7 43 info[attribute] = value
be80986e
S
44 self._downloader.to_screen(
45 '[fromtitle] parsed %s: %s'
46 % (attribute, value if value is not None else 'NA'))
e7db87f7 47
592e97e8 48 return [], info