]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/metadatafromtitle.py
1 from __future__
import unicode_literals
5 from .common
import PostProcessor
6 from ..utils
import PostProcessingError
9 class MetadataFromTitlePPError(PostProcessingError
):
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
)
19 def format_to_regex(self
, fmt
):
21 Converts a string like
22 '%(title)s - %(artist)s'
24 '(?P<title>.+)\ \-\ (?P<artist>.+)'
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) + '>.+)'
33 if lastpos
< len(fmt
):
34 regex
+= re
.escape(fmt
[lastpos
:len(fmt
)])
39 match
= re
.match(self
._titleregex
, title
)
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
)