aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl
diff options
context:
space:
mode:
authorPhilipp Hagemeister <phihag@phihag.de>2014-02-19 01:06:16 +0100
committerPhilipp Hagemeister <phihag@phihag.de>2014-02-19 01:06:16 +0100
commita9c7198a0b3f6a94c3f3132f8ea5e7585478f344 (patch)
tree1ce100ff88c097123ec936fc9130805368174fee /youtube_dl
parentf6f01ea17b553dfb78e4305a665c966db342fcdb (diff)
[testurl] Add extractor
This is a pseudo extractor that can be used to quickly look up test URLs, or test without the test harness.
Diffstat (limited to 'youtube_dl')
-rw-r--r--youtube_dl/extractor/__init__.py1
-rw-r--r--youtube_dl/extractor/testurl.py66
2 files changed, 67 insertions, 0 deletions
diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py
index 725371883..e35287f88 100644
--- a/youtube_dl/extractor/__init__.py
+++ b/youtube_dl/extractor/__init__.py
@@ -216,6 +216,7 @@ from .sztvhu import SztvHuIE
from .teamcoco import TeamcocoIE
from .techtalks import TechTalksIE
from .ted import TEDIE
+from .testurl import TestURLIE
from .tf1 import TF1IE
from .theplatform import ThePlatformIE
from .thisav import ThisAVIE
diff --git a/youtube_dl/extractor/testurl.py b/youtube_dl/extractor/testurl.py
new file mode 100644
index 000000000..bdc6e2064
--- /dev/null
+++ b/youtube_dl/extractor/testurl.py
@@ -0,0 +1,66 @@
+from __future__ import unicode_literals
+
+import re
+
+from .common import InfoExtractor
+from ..utils import ExtractorError
+
+
+class TestURLIE(InfoExtractor):
+ """ Allows adressing of the test cases as test:yout.*be_1 """
+
+ IE_DESC = False # Do not list
+ _VALID_URL = r'test(?:url)?:(?P<id>(?P<extractor>.+?)(?:_(?P<num>[0-9]+))?)$'
+
+ def _real_extract(self, url):
+ from ..extractor import gen_extractors
+
+ mobj = re.match(self._VALID_URL, url)
+ video_id = mobj.group('id')
+ extractor_id = mobj.group('extractor')
+ all_extractors = gen_extractors()
+
+ rex = re.compile(extractor_id, flags=re.IGNORECASE)
+ matching_extractors = [
+ e for e in all_extractors if rex.search(e.IE_NAME)]
+
+ if len(matching_extractors) == 0:
+ raise ExtractorError(
+ 'No extractors matching %r found' % extractor_id,
+ expected=True)
+ elif len(matching_extractors) > 1:
+ # Is it obvious which one to pick?
+ try:
+ extractor = next(
+ ie for ie in matching_extractors
+ if ie.IE_NAME.lower() == extractor_id.lower())
+ except StopIteration:
+ raise ExtractorError(
+ ('Found multiple matching extractors: %s' %
+ ' '.join(ie.IE_NAME for ie in matching_extractors)),
+ expected=True)
+
+ num_str = mobj.group('num')
+ num = int(num_str) if num_str else 0
+
+ testcases = []
+ t = getattr(extractor, '_TEST', None)
+ if t:
+ testcases.append(t)
+ testcases.extend(getattr(extractor, '_TESTS', []))
+
+ try:
+ tc = testcases[num]
+ except IndexError:
+ raise ExtractorError(
+ ('Test case %d not found, got only %d tests' %
+ (num, len(testcases))),
+ expected=True)
+
+ self.to_screen('Test URL: %s' % tc['url'])
+
+ return {
+ '_type': 'url',
+ 'url': tc['url'],
+ 'id': video_id,
+ }