From 2c72a8b489a2285484dd90ccf04ed2111ab826b7 Mon Sep 17 00:00:00 2001 From: Carl Harris Date: Tue, 1 Dec 2015 17:49:20 -0500 Subject: [PATCH 1/2] allow multiple --reject-title and --match-title options --- youtube_dl/YoutubeDL.py | 15 +++++++++------ youtube_dl/__init__.py | 5 +++-- youtube_dl/options.py | 4 ++-- youtube_dl/utils.py | 4 ++++ 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 9a8c7da05..baf6b2d1a 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -596,12 +596,15 @@ class YoutubeDL(object): title = info_dict['title'] matchtitle = self.params.get('matchtitle', False) if matchtitle: - if not re.search(matchtitle, title, re.IGNORECASE): - return '"' + title + '" title did not match pattern "' + matchtitle + '"' - rejecttitle = self.params.get('rejecttitle', False) - if rejecttitle: - if re.search(rejecttitle, title, re.IGNORECASE): - return '"' + title + '" title matched reject pattern "' + rejecttitle + '"' + for pattern in matchtitle: + if re.search(patten, title, re.IGNORECASE): + break + else: + return '"' + title + '" title did not match patterns "' + ', '.join(matchtitle) + '"' + rejecttitle = self.params.get('rejecttitle', []) + for pattern in rejecttitle: + if re.search(pattern, title, re.IGNORECASE): + return '"' + title + '" title matched reject pattern "' + pattern + '"' date = info_dict.get('upload_date', None) if date is not None: dateRange = self.params.get('daterange', DateRange()) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 9f131f5db..d2e885c84 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -25,6 +25,7 @@ from .compat import ( from .utils import ( DateRange, decodeOption, + decodeOptions, DEFAULT_OUTTMPL, DownloadError, match_filter_func, @@ -324,8 +325,8 @@ def _real_main(argv=None): 'listsubtitles': opts.listsubtitles, 'subtitlesformat': opts.subtitlesformat, 'subtitleslangs': opts.subtitleslangs, - 'matchtitle': decodeOption(opts.matchtitle), - 'rejecttitle': decodeOption(opts.rejecttitle), + 'matchtitle': decodeOptions(opts.matchtitle), + 'rejecttitle': decodeOptions(opts.rejecttitle), 'max_downloads': opts.max_downloads, 'prefer_free_formats': opts.prefer_free_formats, 'verbose': opts.verbose, diff --git a/youtube_dl/options.py b/youtube_dl/options.py index c46e136bf..91a084443 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -222,11 +222,11 @@ def parseOpts(overrideArguments=None): help='Playlist video items to download. Specify indices of the videos in the playlist separated by commas like: "--playlist-items 1,2,5,8" if you want to download videos indexed 1, 2, 5, 8 in the playlist. You can specify range: "--playlist-items 1-3,7,10-13", it will download the videos at index 1, 2, 3, 7, 10, 11, 12 and 13.') selection.add_option( '--match-title', - dest='matchtitle', metavar='REGEX', + action='append', dest='matchtitle', metavar='REGEX', help='Download only matching titles (regex or caseless sub-string)') selection.add_option( '--reject-title', - dest='rejecttitle', metavar='REGEX', + action='append', dest='rejecttitle', metavar='REGEX', help='Skip download for matching titles (regex or caseless sub-string)') selection.add_option( '--max-downloads', diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index d0606b4bc..2a95a5647 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -487,6 +487,10 @@ def decodeOption(optval): assert isinstance(optval, compat_str) return optval +def decodeOptions(optvals): + if optvals is None: + return optvals + return list(filter(lambda o: o, map(decodeOption, optvals))) def formatSeconds(secs): if secs > 3600: From bd05b7fd4cef4f0b6834d2dac0d4817e0ec1d4d3 Mon Sep 17 00:00:00 2001 From: Carl Harris Date: Tue, 1 Dec 2015 17:50:08 -0500 Subject: [PATCH 2/2] test for multiple --reject-title and --match-title options --- test/test_options.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/test_options.py diff --git a/test/test_options.py b/test/test_options.py new file mode 100644 index 000000000..7cde7e085 --- /dev/null +++ b/test/test_options.py @@ -0,0 +1,28 @@ +#!/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 itertools import repeat + +from youtube_dl.options import parseOpts + +URL = 'someurl' + +class TestOptions(unittest.TestCase): + + def test_multiple_titles(self): + # test that parser will accept multiple reject-title and match-title options + regexes = ['regex' + i for i in map(str, range(10))] + for name in ['reject', 'match']: + options = [x for opts in zip(repeat('--' + name + '-title'), regexes) for x in opts] + parser, opts, args = parseOpts(options + [URL]) + self.assertEqual(getattr(opts, name + 'title'), regexes) + + +if __name__ == '__main__': + unittest.main()