]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/postprocessor/metadatafromtitle.py
1 from __future__
import unicode_literals
5 from .common
import PostProcessor
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 if re
.search(r
'%\(\w+\)s', titleformat
)
16 def format_to_regex(self
, fmt
):
18 Converts a string like
19 '%(title)s - %(artist)s'
21 '(?P<title>.+)\ \-\ (?P<artist>.+)'
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) + '>.+)'
30 if lastpos
< len(fmt
):
31 regex
+= re
.escape(fmt
[lastpos
:])
36 match
= re
.match(self
._titleregex
, title
)
38 self
._downloader
.to_screen(
39 '[fromtitle] Could not interpret title of video as "%s"'
42 for attribute
, value
in match
.groupdict().items():
43 info
[attribute
] = value
44 self
._downloader
.to_screen(
45 '[fromtitle] parsed %s: %s'
46 % (attribute
, value
if value
is not None else 'NA'))