From a3faeb7de4fa4bf856633266b8ddfb3fc9586018 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Wed, 21 Apr 2021 11:12:04 +0530 Subject: [PATCH] [MetadataFromField] Improve regex and add tests --- test/test_postprocessors.py | 9 +++++++++ yt_dlp/postprocessor/metadatafromfield.py | 9 +++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/test/test_postprocessors.py b/test/test_postprocessors.py index 12fc427fb..7574a0b95 100644 --- a/test/test_postprocessors.py +++ b/test/test_postprocessors.py @@ -16,6 +16,15 @@ def test_format_to_regex(self): pp = MetadataFromFieldPP(None, ['title:%(title)s - %(artist)s']) self.assertEqual(pp._data[0]['regex'], r'(?P.+)\ \-\ (?P<artist>.+)') + def test_field_to_outtmpl(self): + pp = MetadataFromFieldPP(None, ['title:%(title)s : %(artist)s']) + self.assertEqual(pp._data[0]['tmpl'], '%(title)s') + + def test_in_out_seperation(self): + pp = MetadataFromFieldPP(None, ['%(title)s \\: %(artist)s:%(title)s : %(artist)s']) + self.assertEqual(pp._data[0]['in'], '%(title)s : %(artist)s') + self.assertEqual(pp._data[0]['out'], '%(title)s : %(artist)s') + class TestMetadataFromTitle(unittest.TestCase): def test_format_to_regex(self): diff --git a/yt_dlp/postprocessor/metadatafromfield.py b/yt_dlp/postprocessor/metadatafromfield.py index 195c63f92..1def868e8 100644 --- a/yt_dlp/postprocessor/metadatafromfield.py +++ b/yt_dlp/postprocessor/metadatafromfield.py @@ -7,7 +7,7 @@ class MetadataFromFieldPP(PostProcessor): - regex = r'(?P<in>.+):(?P<out>.+)$' + regex = r'(?P<in>.*?)(?<!\\):(?P<out>.+)$' def __init__(self, downloader, formats): PostProcessor.__init__(self, downloader) @@ -17,10 +17,11 @@ def __init__(self, downloader, formats): assert isinstance(f, compat_str) match = re.match(self.regex, f) assert match is not None + inp = match.group('in').replace('\\:', ':') self._data.append({ - 'in': match.group('in'), + 'in': inp, 'out': match.group('out'), - 'tmpl': self.field_to_template(match.group('in')), + 'tmpl': self.field_to_template(inp), 'regex': self.format_to_regex(match.group('out')), }) @@ -68,6 +69,6 @@ def run(self, info): class MetadataFromTitlePP(MetadataFromFieldPP): # for backward compatibility def __init__(self, downloader, titleformat): - super(MetadataFromTitlePP, self).__init__(downloader, ['title:%s' % titleformat]) + super(MetadataFromTitlePP, self).__init__(downloader, ['%%(title)s:%s' % titleformat]) self._titleformat = titleformat self._titleregex = self._data[0]['regex']