]> jfr.im git - yt-dlp.git/commitdiff
[MetadataFromField] Improve regex and add tests
authorpukkandan <redacted>
Wed, 21 Apr 2021 05:42:04 +0000 (11:12 +0530)
committerpukkandan <redacted>
Wed, 21 Apr 2021 05:42:04 +0000 (11:12 +0530)
test/test_postprocessors.py
yt_dlp/postprocessor/metadatafromfield.py

index 12fc427fbc7c11a8e263fce86c1f2aa32c9c1ff1..7574a0b950a2260596e1c851eb56a04a21fe5869 100644 (file)
@@ -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<title>.+)\ \-\ (?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):
index 195c63f926462e34f0f2d8f5511b03fd93714c14..1def868e812323476c895f75f974a9d815f84f36 100644 (file)
@@ -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']