diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..05dd519a8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: python +#specify the python version +python: + - "2.6" + - "2.7" +# command to run tests +script: nosetests tests diff --git a/Makefile b/Makefile index f9c0aef0b..4677ff684 100644 --- a/Makefile +++ b/Makefile @@ -45,3 +45,8 @@ youtube-dl.bash-completion: README.md LATEST_VERSION: youtube_dl/__init__.py python -m youtube_dl --version > LATEST_VERSION + +test: + nosetests2 test + +.PHONY: default compile update update-latest update-readme test clean diff --git a/test/test_div.py b/test/test_div.py deleted file mode 100644 index 4d4819b3c..000000000 --- a/test/test_div.py +++ /dev/null @@ -1,29 +0,0 @@ -# -*- coding: utf-8 -*- - -# Various small unit tests - -import os,sys -sys.path.append(os.path.dirname(os.path.dirname(__file__))) - -import youtube_dl - -def test_simplify_title(): - assert youtube_dl._simplify_title(u'abc') == u'abc' - assert youtube_dl._simplify_title(u'abc_d-e') == u'abc_d-e' - - assert youtube_dl._simplify_title(u'123') == u'123' - - assert u'/' not in youtube_dl._simplify_title(u'abc/de') - assert u'abc' in youtube_dl._simplify_title(u'abc/de') - assert u'de' in youtube_dl._simplify_title(u'abc/de') - assert u'/' not in youtube_dl._simplify_title(u'abc/de///') - - assert u'\\' not in youtube_dl._simplify_title(u'abc\\de') - assert u'abc' in youtube_dl._simplify_title(u'abc\\de') - assert u'de' in youtube_dl._simplify_title(u'abc\\de') - - assert youtube_dl._simplify_title(u'ä') == u'ä' - assert youtube_dl._simplify_title(u'кириллица') == u'кириллица' - - # Strip underlines - assert youtube_dl._simplify_title(u'\'a_') == u'a' diff --git a/test/test_download.py b/test/test_download.py new file mode 100644 index 000000000..a2f5abcb2 --- /dev/null +++ b/test/test_download.py @@ -0,0 +1,148 @@ +#!/usr/bin/env python2 +import unittest +import hashlib +import os + +from youtube_dl.FileDownloader import FileDownloader +from youtube_dl.InfoExtractors import YoutubeIE, DailymotionIE +from youtube_dl.InfoExtractors import MetacafeIE, PhotobucketIE +from youtube_dl.InfoExtractors import FacebookIE, BlipTVIE +from youtube_dl.InfoExtractors import VimeoIE, XVideosIE + + +class DownloadTest(unittest.TestCase): + #calculated with md5sum: + #md5sum (GNU coreutils) 8.19 + YOUTUBE_MD5 = "8547978241cb87dd6782b10b8e90acc3" + YOUTUBE_URL = "http://www.youtube.com/watch?v=BaW_jenozKc" + YOUTUBE_FILE = "BaW_jenozKc.flv" + + + DAILYMOTION_MD5 = "" + DAILYMOTION_URL = "http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech" + DAILYMOTION_FILE = "" + + + METACAFE_MD5 = "" + METACAFE_URL = "http://www.metacafe.com/watch/yt-bV9L5Ht9LgY/download_youtube_playlist_with_youtube_dl/" + METACAFE_FILE = "" + + + PHOTOBUCKET_MD5 = "" + PHOTOBUCKET_URL = "http://www.metacafe.com/watch/yt-bV9L5Ht9LgY/download_youtube_playlist_with_youtube_dl/" + PHOTOBUCKET_FILE = "" + + + FACEBOOK_MD5 = "" + FACEBOOK_URL = "https://www.facebook.com/video/video.php?v=207446242657384" + FACEBOOK_FILE = "" + + + BLIP_MD5 = "" + BLIP_URL = "https://www.facebook.com/video/video.php?v=207446242657384" + BLIP_FILE = "" + + VIMEO_MD5 = "" + VIMEO_URL = "https://www.facebook.com/video/video.php?v=207446242657384" + VIMEO_FILE = "" + + XVIDEO_MD5 = "" + XVIDEO_URL = "https://www.facebook.com/video/video.php?v=207446242657384" + XVIDEO_FILE = "" + + + def test_youtube(self): + #let's download a file from youtube + fd = FileDownloader({}) + fd.add_info_extractor(YoutubeIE()) + fd.download([DownloadTest.YOUTUBE_URL]) + self.assertTrue(os.path.exists(DownloadTest.YOUTUBE_FILE)) + md5_down_file = md5_for_file(DownloadTest.YOUTUBE_FILE) + self.assertEqual(md5_down_file, DownloadTest.YOUTUBE_MD5) + + def test_dailymotion(self): + fd = FileDownloader({}) + fd.add_info_extractor(DailymotionIE()) + fd.download([DownloadTest.DAILYMOTION_URL]) + self.assertTrue(os.path.exists(DownloadTest.DAILYMOTION_FILE)) + md5_down_file = md5_for_file(DownloadTest.DAILYMOTION_FILE) + self.assertEqual(md5_down_file, DownloadTest.DAILYMOTION_MD5) + + + def test_metacafe(self): + fd = FileDownloader({}) + fd.add_info_extractor(MetacafeIE()) + fd.download([DownloadTest.METACAFE_URL]) + self.assertTrue(os.path.exists(DownloadTest.METACAFE_FILE)) + md5_down_file = md5_for_file(DownloadTest.METACAFE_FILE) + self.assertEqual(md5_down_file, DownloadTest.METACAFE_MD5) + + def test_photobucket(self): + fd = FileDownloader({}) + fd.add_info_extractor(PhotobucketIE()) + fd.download([DownloadTest.PHOTOBUCKET_URL]) + self.assertTrue(os.path.exists(DownloadTest.PHOTOBUCKET_FILE)) + md5_down_file = md5_for_file(DownloadTest.PHOTOBUCKET_FILE) + self.assertEqual(md5_down_file, DownloadTest.PHOTOBUCKET_MD5) + + + def test_facebook(self): + fd = FileDownloader({}) + fd.add_info_extractor(FacebookIE()) + fd.download([DownloadTest.FACEBOOK_URL]) + self.assertTrue(os.path.exists(DownloadTest.FACEBOOK_FILE)) + md5_down_file = md5_for_file(DownloadTest.FACEBOOK_FILE) + self.assertEqual(md5_down_file, DownloadTest.FACEBOOK_MD5) + + def test_blip(self): + fd = FileDownloader({}) + fd.add_info_extractor(BlipTVIE()) + fd.download([DownloadTest.BLIP_URL]) + self.assertTrue(os.path.exists(DownloadTest.BLIP_FILE)) + md5_down_file = md5_for_file(DownloadTest.BLIP_FILE) + self.assertEqual(md5_down_file, DownloadTest.BLIP_MD5) + + + def test_vimeo(self): + fd = FileDownloader({}) + fd.add_info_extractor(VimeoIE()) + fd.download([DownloadTest.VIMEO_URL]) + self.assertTrue(os.path.exists(DownloadTest.VIMEO_FILE)) + md5_down_file = md5_for_file(DownloadTest.VIMEO_FILE) + self.assertEqual(md5_down_file, DownloadTest.VIMEO_MD5) + + + def test_xvideo(self): + fd = FileDownloader({}) + fd.add_info_extractor(XVideosIE()) + fd.download([DownloadTest.XVIDEO_URL]) + self.assertTrue(os.path.exists(DownloadTest.XVIDEO_FILE)) + md5_down_file = md5_for_file(DownloadTest.XVIDEO_FILE) + self.assertEqual(md5_down_file, DownloadTest.XVIDEO_MD5) + + def cleanUp(self): + if os.path.exists(DownloadTest.YOUTUBE_FILE): + os.remove(DownloadTest.YOUTUBE_FILE) + if os.path.exists(DownloadTest.DAILYMOTION_FILE): + os.remove(DownloadTest.DAILYMOTION_FILE) + if os.path.exists(DownloadTest.METACAFE_FILE): + os.remove(DownloadTest.METACAFE_FILE) + if os.path.exists(DownloadTest.PHOTOBUCKET_FILE): + os.remove(DownloadTest.PHOTOBUCKET_FILE) + if os.path.exists(DownloadTest.FACEBOOK_FILE): + os.remove(DownloadTest.FACEBOOK_FILE) + if os.path.exists(DownloadTest.BLIP_FILE): + os.remove(DownloadTest.BLIP_FILE) + if os.path.exists(DownloadTest.VIMEO_FILE): + os.remove(DownloadTest.VIMEO_FILE) + if os.path.exists(DownloadTest.XVIDEO_FILE): + os.remove(DownloadTest.XVIDEO_FILE) + +def md5_for_file(f, block_size=2**20): + md5 = hashlib.md5() + while True: + data = f.read(block_size) + if not data: + break + md5.update(data) + return md5.digest() diff --git a/test/test_utils.py b/test/test_utils.py new file mode 100644 index 000000000..fd3ac4d46 --- /dev/null +++ b/test/test_utils.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- + +# Various small unit tests + +import unittest + +#from youtube_dl.utils import htmlentity_transform +from youtube_dl.utils import timeconvert +from youtube_dl.utils import sanitize_filename +from youtube_dl.utils import unescapeHTML +from youtube_dl.utils import orderedSet + + +class TestUtil(unittest.TestCase): + def test_timeconvert(self): + self.assertIsNone(timeconvert('')) + self.assertIsNone(timeconvert('bougrg')) + + def test_sanitize_filename(self): + self.assertEqual(sanitize_filename(u'abc'), u'abc') + self.assertEqual(sanitize_filename(u'abc_d-e'), u'abc_d-e') + + self.assertEqual(sanitize_filename(u'123'), u'123') + + self.assertEqual(u'abc_de', sanitize_filename(u'abc/de')) + self.assertIn(u'de', sanitize_filename(u'abc/de')) + self.assertNotIn(u'/', sanitize_filename(u'abc/de///')) + + self.assertEqual(u'abc_de', sanitize_filename(u'abc\\de')) + self.assertEqual(u'abc_de', sanitize_filename(u'abc\\de')) + self.assertIn(u'de', sanitize_filename(u'abc\\de')) + + self.assertEqual(sanitize_filename(u'ä'), u'ä') + self.assertEqual(sanitize_filename(u'кириллица'), u'кириллица') + + def test_ordered_set(self): + self.assertEqual(orderedSet([1,1,2,3,4,4,5,6,7,3,5]), [1,2,3,4,5,6,7]) + self.assertEqual(orderedSet([]), []) + self.assertEqual(orderedSet([1]), [1]) + #keep the list ordered + self.assertEqual(orderedSet([135,1,1,1]), [135,1]) + + def test_unescape_html(self): + self.assertEqual(unescapeHTML(u"%20;"), u"%20;") diff --git a/youtube_dl/FileDownloader.py b/youtube_dl/FileDownloader.py index 38c6a519a..ed5a79f13 100644 --- a/youtube_dl/FileDownloader.py +++ b/youtube_dl/FileDownloader.py @@ -13,7 +13,7 @@ import urllib2 if os.name == 'nt': import ctypes - + from utils import * @@ -173,7 +173,6 @@ class FileDownloader(object): if not self.params.get('quiet', False): terminator = [u'\n', u''][skip_eol] output = message + terminator - if 'b' not in self._screen_file.mode or sys.version_info[0] < 3: # Python 2 lies about the mode of sys.stdout/sys.stderr output = output.encode(preferredencoding(), 'ignore') self._screen_file.write(output) @@ -357,7 +356,7 @@ class FileDownloader(object): raise MaxDownloadsReached() filename = self.prepare_filename(info_dict) - + # Forced printings if self.params.get('forcetitle', False): print info_dict['title'].encode(preferredencoding(), 'xmlcharrefreplace') @@ -399,10 +398,10 @@ class FileDownloader(object): except (OSError, IOError): self.trouble(u'ERROR: Cannot write description file ' + descfn) return - + if self.params.get('writesubtitles', False) and 'subtitles' in info_dict and info_dict['subtitles']: # subtitles download errors are already managed as troubles in relevant IE - # that way it will silently go on when used with unsupporting IE + # that way it will silently go on when used with unsupporting IE try: srtfn = filename.rsplit('.', 1)[0] + u'.srt' self.report_writesubtitles(srtfn) @@ -448,7 +447,7 @@ class FileDownloader(object): except (ContentTooShortError, ), err: self.trouble(u'ERROR: content too short (expected %s bytes and served %s)' % (err.expected, err.downloaded)) return - + if success: try: self.post_process(filename, info_dict)