]> gitweb @ CieloNegro.org - youtube-dl.git/blob - test/test_download.py
9e0c525510f6aa12f4f8e248a1e5ce117c114375
[youtube-dl.git] / test / test_download.py
1 #!/usr/bin/env python2
2 import unittest
3 import hashlib
4 import os
5 import json
6
7 from youtube_dl.FileDownloader import FileDownloader
8 from youtube_dl.InfoExtractors  import YoutubeIE, DailymotionIE
9 from youtube_dl.InfoExtractors import  MetacafeIE, BlipTVIE
10 from youtube_dl.InfoExtractors import  XVideosIE, VimeoIE
11 from youtube_dl.InfoExtractors import  SoundcloudIE
12
13
14 class DownloadTest(unittest.TestCase):
15         PARAMETERS_FILE = "test/parameters.json"
16         #calculated with md5sum:
17         #md5sum (GNU coreutils) 8.19
18
19         YOUTUBE_SIZE = 1993883
20         YOUTUBE_URL = "http://www.youtube.com/watch?v=BaW_jenozKc"
21         YOUTUBE_FILE = "BaW_jenozKc.mp4"
22
23         DAILYMOTION_MD5 = "d363a50e9eb4f22ce90d08d15695bb47"
24         DAILYMOTION_URL = "http://www.dailymotion.com/video/x33vw9_tutoriel-de-youtubeur-dl-des-video_tech"
25         DAILYMOTION_FILE = "x33vw9.mp4"
26
27         METACAFE_SIZE = 5754305
28         METACAFE_URL = "http://www.metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/"
29         METACAFE_FILE = "_aUehQsCQtM.flv"
30
31         BLIP_MD5 = "93c24d2f4e0782af13b8a7606ea97ba7"
32         BLIP_URL = "http://blip.tv/cbr/cbr-exclusive-gotham-city-imposters-bats-vs-jokerz-short-3-5796352"
33         BLIP_FILE = "5779306.m4v"
34
35         XVIDEO_MD5 = "1ab4dedc01f771cb2a65e91caa801aaf"
36         XVIDEO_URL = "http://www.xvideos.com/video939581/funny_porns_by_s_-1"
37         XVIDEO_FILE = "939581.flv"
38
39         VIMEO_MD5 = "1ab4dedc01f771cb2a65e91caa801aaf"
40         VIMEO_URL = "http://vimeo.com/14160053"
41         VIMEO_FILE = ""
42
43         SOUNDCLOUD_MD5 = "ce3775768ebb6432fa8495d446a078ed"
44         SOUNDCLOUD_URL = "http://soundcloud.com/ethmusic/lostin-powers-she-so-heavy"
45         SOUNDCLOUD_FILE = "n6FLbx6ZzMiu.mp3"
46
47         def test_youtube(self):
48                 #let's download a file from youtube
49                 with open(DownloadTest.PARAMETERS_FILE) as f:
50                         fd = FileDownloader(json.load(f))
51                 fd.add_info_extractor(YoutubeIE())
52                 fd.download([DownloadTest.YOUTUBE_URL])
53                 self.assertTrue(os.path.exists(DownloadTest.YOUTUBE_FILE))
54                 self.assertEqual(os.path.getsize(DownloadTest.YOUTUBE_FILE), DownloadTest.YOUTUBE_SIZE)
55
56         def test_dailymotion(self):
57                 with open(DownloadTest.PARAMETERS_FILE) as f:
58                         fd = FileDownloader(json.load(f))
59                 fd.add_info_extractor(DailymotionIE())
60                 fd.download([DownloadTest.DAILYMOTION_URL])
61                 self.assertTrue(os.path.exists(DownloadTest.DAILYMOTION_FILE))
62                 md5_down_file = md5_for_file(DownloadTest.DAILYMOTION_FILE)
63                 self.assertEqual(md5_down_file, DownloadTest.DAILYMOTION_MD5)
64
65         def test_metacafe(self):
66                 #this emulate a skip,to be 2.6 compatible
67                 with open(DownloadTest.PARAMETERS_FILE) as f:
68                         fd = FileDownloader(json.load(f))
69                 fd.add_info_extractor(MetacafeIE())
70                 fd.add_info_extractor(YoutubeIE())
71                 fd.download([DownloadTest.METACAFE_URL])
72                 self.assertTrue(os.path.exists(DownloadTest.METACAFE_FILE))
73                 self.assertEqual(os.path.getsize(DownloadTest.METACAFE_FILE), DownloadTest.METACAFE_SIZE)
74
75         def test_blip(self):
76                 with open(DownloadTest.PARAMETERS_FILE) as f:
77                         fd = FileDownloader(json.load(f))
78                 fd.add_info_extractor(BlipTVIE())
79                 fd.download([DownloadTest.BLIP_URL])
80                 self.assertTrue(os.path.exists(DownloadTest.BLIP_FILE))
81                 md5_down_file = md5_for_file(DownloadTest.BLIP_FILE)
82                 self.assertEqual(md5_down_file, DownloadTest.BLIP_MD5)
83
84         def test_xvideo(self):
85                 with open(DownloadTest.PARAMETERS_FILE) as f:
86                         fd = FileDownloader(json.load(f))
87                 fd.add_info_extractor(XVideosIE())
88                 fd.download([DownloadTest.XVIDEO_URL])
89                 self.assertTrue(os.path.exists(DownloadTest.XVIDEO_FILE))
90                 md5_down_file = md5_for_file(DownloadTest.XVIDEO_FILE)
91                 self.assertEqual(md5_down_file, DownloadTest.XVIDEO_MD5)
92
93         def test_vimeo(self):
94                 with open(DownloadTest.PARAMETERS_FILE) as f:
95                         fd = FileDownloader(json.load(f))
96                 fd.add_info_extractor(VimeoIE())
97                 fd.download([DownloadTest.VIMEO_URL])
98                 self.assertTrue(os.path.exists(DownloadTest.VIMEO_FILE))
99                 md5_down_file = md5_for_file(DownloadTest.VIMEO_FILE)
100                 self.assertEqual(md5_down_file, DownloadTest.VIMEO_MD5)
101
102         def test_soundcloud(self):
103                 with open(DownloadTest.PARAMETERS_FILE) as f:
104                         fd = FileDownloader(json.load(f))
105                 fd.add_info_extractor(SoundcloudIE())
106                 fd.download([DownloadTest.SOUNDCLOUD_URL])
107                 self.assertTrue(os.path.exists(DownloadTest.SOUNDCLOUD_FILE))
108                 md5_down_file = md5_for_file(DownloadTest.SOUNDCLOUD_FILE)
109                 self.assertEqual(md5_down_file, DownloadTest.SOUNDCLOUD_MD5)
110
111         def tearDown(self):
112                 if os.path.exists(DownloadTest.YOUTUBE_FILE):
113                         os.remove(DownloadTest.YOUTUBE_FILE)
114                 if os.path.exists(DownloadTest.DAILYMOTION_FILE):
115                         os.remove(DownloadTest.DAILYMOTION_FILE)
116                 if os.path.exists(DownloadTest.METACAFE_FILE):
117                         os.remove(DownloadTest.METACAFE_FILE)
118                 if os.path.exists(DownloadTest.BLIP_FILE):
119                         os.remove(DownloadTest.BLIP_FILE)
120                 if os.path.exists(DownloadTest.XVIDEO_FILE):
121                         os.remove(DownloadTest.XVIDEO_FILE)
122                 if os.path.exists(DownloadTest.VIMEO_FILE):
123                         os.remove(DownloadTest.VIMEO_FILE)
124                 if os.path.exists(DownloadTest.SOUNDCLOUD_FILE):
125                         os.remove(DownloadTest.SOUNDCLOUD_FILE)
126
127 def md5_for_file(filename, block_size=2**20):
128         with open(filename) as f:
129                 md5 = hashlib.md5()
130                 while True:
131                         data = f.read(block_size)
132                         if not data:
133                                 break
134                         md5.update(data)
135                         return md5.hexdigest()