From 88cf6fb3685c4e012c9f574cbc5f1836c42fc06d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Marqui=CC=81nez=20Ferra=CC=81ndiz?= Date: Sat, 14 Mar 2015 19:55:42 +0100 Subject: [PATCH] [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 --- test/test_postprocessors.py | 17 +++++++++++++++++ youtube_dl/options.py | 6 +++--- youtube_dl/postprocessor/ffmpeg.py | 4 +--- youtube_dl/postprocessor/metadatafromtitle.py | 9 ++++----- 4 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 test/test_postprocessors.py diff --git a/test/test_postprocessors.py b/test/test_postprocessors.py new file mode 100644 index 000000000..addb69d6f --- /dev/null +++ b/test/test_postprocessors.py @@ -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.+)\ \-\ (?P<artist>.+)') diff --git a/youtube_dl/options.py b/youtube_dl/options.py index be9402fdb..4e6e47d6f 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -738,10 +738,10 @@ def parseOpts(overrideArguments=None): postproc.add_option( '--metadata-from-title', 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 parsed parameters replace existing values.\n' - 'Additional templates: %(songtitle), %(album), %(artist). \n' + 'the parsed parameters replace existing values. ' + 'Additional templates: %(album), %(artist). ' 'Example: --metadata-from-title "%(artist)s - %(title)s" matches a title like ' '"Coldplay - Paradise"') postproc.add_option( diff --git a/youtube_dl/postprocessor/ffmpeg.py b/youtube_dl/postprocessor/ffmpeg.py index a17113cbf..b6f51cfd5 100644 --- a/youtube_dl/postprocessor/ffmpeg.py +++ b/youtube_dl/postprocessor/ffmpeg.py @@ -541,9 +541,7 @@ class FFmpegEmbedSubtitlePP(FFmpegPostProcessor): class FFmpegMetadataPP(FFmpegPostProcessor): def run(self, info): metadata = {} - if info.get('songtitle') is not None: - metadata['title'] = info['songtitle'] - elif info.get('title') is not None: + if info.get('title') is not None: metadata['title'] = info['title'] if info.get('upload_date') is not None: metadata['date'] = info['upload_date'] diff --git a/youtube_dl/postprocessor/metadatafromtitle.py b/youtube_dl/postprocessor/metadatafromtitle.py index 4c9d3aafe..5019433d3 100644 --- a/youtube_dl/postprocessor/metadatafromtitle.py +++ b/youtube_dl/postprocessor/metadatafromtitle.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +from __future__ import unicode_literals import re @@ -12,20 +12,19 @@ class MetadataFromTitlePPError(PostProcessingError): class MetadataFromTitlePP(PostProcessor): def __init__(self, downloader, titleformat): + super(MetadataFromTitlePP, self).__init__(downloader) 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 '%(title)s - %(artist)s' to a regex like '(?P<title>.+)\ \-\ (?P<artist>.+)' - and a list of the named groups [title, artist] """ lastpos = 0 regex = "" - groups = [] # replace %(..)s with regex group and escape other string parts for match in re.finditer(r'%\((\w+)\)s', fmt): regex += re.escape(fmt[lastpos:match.start()])