some changes to keep the same standard
[youtube-dl] / test / test_download.py
1 #!/usr/bin/env python2
2 import unittest
3 import hashlib
4 import os
5
6 from youtube_dl.FileDownloader import FileDownloader
7 from youtube_dl.InfoExtractors  import YoutubeIE
8
9 class DownloadTest(unittest.TestCase):
10         #calculated with md5sum:
11         #       md5sum (GNU coreutils) 8.19
12         YOUTUBE_MD5 = "8547978241cb87dd6782b10b8e90acc3"
13         YOUTUBE_URL = "http://www.youtube.com/watch?v=BaW_jenozKc"
14         YOUTUBE_FILE = "BaW_jenozKc.flv"
15
16         def test_youtube(self):
17                 #let's download a file from youtube
18                 fd = FileDownloader({})
19                 fd.add_info_extractor(YoutubeIE())
20                 fd.download([DownloadTest.YOUTUBE_URL])
21                 self.assertTrue(os.path.exists(DownloadTest.YOUTUBE_FILE))
22                 md5_down_file = md5_for_file(DownloadTest.YOUTUBE_FILE)
23                 self.assertEqual(md5_down_file, DownloadTest.YOUTUBE_MD5)
24
25         def cleanUp(self):
26                 if os.path.exists(DownloadTest.YOUTUBE_FILE):
27                         os.remove(DownloadTest.YOUTUBE_FILE)
28
29 def md5_for_file(f, block_size=2**20):
30         md5 = hashlib.md5()
31         while True:
32                 data = f.read(block_size)
33                 if not data:
34                         break
35                 md5.update(data)
36                 return md5.digest()