[metadatafromtitle] Some improvements and cleanup
* Remove the 'songtitle' field, 'title' can be used instead. * Remove newlines in the help text, for consistency with other options. * Add 'from __future__ import unicode_literals'. * Call '__init__' from the parent class. * Add test for the format_to_regex method
This commit is contained in:
parent
e7db87f700
commit
88cf6fb368
17
test/test_postprocessors.py
Normal file
17
test/test_postprocessors.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
# Allow direct execution
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
|
|
||||||
|
from youtube_dl.postprocessor import MetadataFromTitlePP
|
||||||
|
|
||||||
|
|
||||||
|
class TestMetadataFromTitle(unittest.TestCase):
|
||||||
|
def test_format_to_regex(self):
|
||||||
|
pp = MetadataFromTitlePP(None, '%(title)s - %(artist)s')
|
||||||
|
self.assertEqual(pp._titleregex, '(?P<title>.+)\ \-\ (?P<artist>.+)')
|
@ -738,10 +738,10 @@ def parseOpts(overrideArguments=None):
|
|||||||
postproc.add_option(
|
postproc.add_option(
|
||||||
'--metadata-from-title',
|
'--metadata-from-title',
|
||||||
metavar='FORMAT', dest='metafromtitle',
|
metavar='FORMAT', dest='metafromtitle',
|
||||||
help='parse additional metadata like song title / artist from the video title. \n'
|
help='parse additional metadata like song title / artist from the video title. '
|
||||||
'The format syntax is the same as --output, '
|
'The format syntax is the same as --output, '
|
||||||
'the parsed parameters replace existing values.\n'
|
'the parsed parameters replace existing values. '
|
||||||
'Additional templates: %(songtitle), %(album), %(artist). \n'
|
'Additional templates: %(album), %(artist). '
|
||||||
'Example: --metadata-from-title "%(artist)s - %(title)s" matches a title like '
|
'Example: --metadata-from-title "%(artist)s - %(title)s" matches a title like '
|
||||||
'"Coldplay - Paradise"')
|
'"Coldplay - Paradise"')
|
||||||
postproc.add_option(
|
postproc.add_option(
|
||||||
|
@ -541,9 +541,7 @@ class FFmpegEmbedSubtitlePP(FFmpegPostProcessor):
|
|||||||
class FFmpegMetadataPP(FFmpegPostProcessor):
|
class FFmpegMetadataPP(FFmpegPostProcessor):
|
||||||
def run(self, info):
|
def run(self, info):
|
||||||
metadata = {}
|
metadata = {}
|
||||||
if info.get('songtitle') is not None:
|
if info.get('title') is not None:
|
||||||
metadata['title'] = info['songtitle']
|
|
||||||
elif info.get('title') is not None:
|
|
||||||
metadata['title'] = info['title']
|
metadata['title'] = info['title']
|
||||||
if info.get('upload_date') is not None:
|
if info.get('upload_date') is not None:
|
||||||
metadata['date'] = info['upload_date']
|
metadata['date'] = info['upload_date']
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# -*- coding: utf-8 -*-
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
@ -12,20 +12,19 @@ class MetadataFromTitlePPError(PostProcessingError):
|
|||||||
|
|
||||||
class MetadataFromTitlePP(PostProcessor):
|
class MetadataFromTitlePP(PostProcessor):
|
||||||
def __init__(self, downloader, titleformat):
|
def __init__(self, downloader, titleformat):
|
||||||
|
super(MetadataFromTitlePP, self).__init__(downloader)
|
||||||
self._titleformat = titleformat
|
self._titleformat = titleformat
|
||||||
self._titleregex = self.fmtToRegex(titleformat)
|
self._titleregex = self.format_to_regex(titleformat)
|
||||||
|
|
||||||
def fmtToRegex(self, fmt):
|
def format_to_regex(self, fmt):
|
||||||
"""
|
"""
|
||||||
Converts a string like
|
Converts a string like
|
||||||
'%(title)s - %(artist)s'
|
'%(title)s - %(artist)s'
|
||||||
to a regex like
|
to a regex like
|
||||||
'(?P<title>.+)\ \-\ (?P<artist>.+)'
|
'(?P<title>.+)\ \-\ (?P<artist>.+)'
|
||||||
and a list of the named groups [title, artist]
|
|
||||||
"""
|
"""
|
||||||
lastpos = 0
|
lastpos = 0
|
||||||
regex = ""
|
regex = ""
|
||||||
groups = []
|
|
||||||
# replace %(..)s with regex group and escape other string parts
|
# replace %(..)s with regex group and escape other string parts
|
||||||
for match in re.finditer(r'%\((\w+)\)s', fmt):
|
for match in re.finditer(r'%\((\w+)\)s', fmt):
|
||||||
regex += re.escape(fmt[lastpos:match.start()])
|
regex += re.escape(fmt[lastpos:match.start()])
|
||||||
|
Loading…
Reference in New Issue
Block a user