From da4506c4e79c275630a4948d2cba2bac63e70b01 Mon Sep 17 00:00:00 2001 From: Mort Yao Date: Fri, 14 Feb 2014 21:09:57 +0100 Subject: [PATCH] fix url_to_module --- src/you_get/extractor/__main__.py | 26 ++++++++++---------------- tests/test.py | 14 +++++++------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/you_get/extractor/__main__.py b/src/you_get/extractor/__main__.py index 30bfa41c..b2974021 100644 --- a/src/you_get/extractor/__main__.py +++ b/src/you_get/extractor/__main__.py @@ -8,12 +8,12 @@ def url_to_module(url): video_host = r1(r'http://([^/]+)/', url) video_url = r1(r'http://[^/]+(.*)', url) assert video_host and video_url, 'invalid url: ' + url - + if video_host.endswith('.com.cn'): video_host = video_host[:-3] domain = r1(r'(\.[^.]+\.[^.]+)$', video_host) or video_host assert domain, 'unsupported url: ' + url - + k = r1(r'([^.]+)', domain) downloads = { '163': netease, @@ -66,7 +66,7 @@ def url_to_module(url): #TODO } if k in downloads: - return downloads[k] + return downloads[k], url else: import http.client conn = http.client.HTTPConnection(video_host) @@ -76,21 +76,15 @@ def url_to_module(url): if location is None: raise NotImplementedError(url) else: - return url_to_module(location), location + return url_to_module(location) -def any_download(url, output_dir = '.', merge = True, info_only = False): - try: - m, url = url_to_module(url) - except: - m = url_to_module(url) - m.download(url, output_dir = output_dir, merge = merge, info_only = info_only) +def any_download(url, output_dir='.', merge=True, info_only=False): + m, url = url_to_module(url) + m.download(url, output_dir=output_dir, merge=merge, info_only=info_only) -def any_download_playlist(url, output_dir = '.', merge = True, info_only = False): - try: - m, url = url_to_module(url) - except: - m = url_to_module(url) - m.download_playlist(url, output_dir = output_dir, merge = merge, info_only = info_only) +def any_download_playlist(url, output_dir='.', merge=True, info_only=False): + m, url = url_to_module(url) + m.download_playlist(url, output_dir=output_dir, merge=merge, info_only=info_only) def main(): script_main('you-get', any_download, any_download_playlist) diff --git a/tests/test.py b/tests/test.py index 34a6d8cc..05883d4b 100644 --- a/tests/test.py +++ b/tests/test.py @@ -8,39 +8,39 @@ from you_get.extractor.__main__ import url_to_module def test_urls(urls): for url in urls: - url_to_module(url).download(url, info_only = True) + url_to_module(url)[0].download(url, info_only = True) class YouGetTests(unittest.TestCase): - + def test_freesound(self): test_urls([ "http://www.freesound.org/people/Corsica_S/sounds/184419/", ]) - + def test_mixcloud(self): test_urls([ "http://www.mixcloud.com/beatbopz/beat-bopz-disco-mix/", "http://www.mixcloud.com/DJVadim/north-america-are-you-ready/", ]) - + def test_sohu(self): test_urls([ "http://tv.sohu.com/20120522/n343785589.shtml", "http://tv.sohu.com/20130103/n362246415.shtml", "http://tv.sohu.com/20130103/n362251239.shtml" ]) - + def test_ted(self): test_urls([ "http://www.ted.com/talks/jennifer_lin_improvs_piano_magic.html", "http://www.ted.com/talks/derek_paravicini_and_adam_ockelford_in_the_key_of_genius.html", ]) - + def test_vimeo(self): test_urls([ "http://vimeo.com/56810854", ]) - + def test_youtube(self): test_urls([ "http://www.youtube.com/watch?v=pzKerr0JIPA",