File size: 2,584 Bytes
bbf7f6e 16f9f74 bbf7f6e 6ef7310 8b33d56 6ef7310 8b33d56 bbf7f6e 6ef7310 42f45d6 8b33d56 28f27a6 6ef7310 8b33d56 6ef7310 6d999e1 6ef7310 8b33d56 16f9f74 8b33d56 16f9f74 8b33d56 28f27a6 8b33d56 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#!/usr/bin/env/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import mock
from nose.tools import eq_, raises
from pytube import api
from pytube.exceptions import MultipleObjectsReturned
class TestPytube(object):
def setUp(self):
url = 'http://www.youtube.com/watch?v=9bZkp7q19f0'
with open('tests/mock_video.js') as fh:
self.mock_js = fh.read()
with open('tests/mock_video.html') as fh:
self.mock_html = fh.read()
with mock.patch('pytube.api.urlopen') as urlopen:
urlopen.return_value.read.return_value = self.mock_html
self.yt = api.YouTube()
self.yt._js_cache = self.mock_js
self.yt.from_url(url)
def test_get_video_id(self):
"""Resolve the video id from url"""
eq_(self.yt.video_id, '9bZkp7q19f0')
def test_auto_filename(self):
"""Generate safe filename based on video title"""
expected = 'PSY - GANGNAM STYLE(\uac15\ub0a8\uc2a4\ud0c0\uc77c) MV'
eq_(self.yt.filename, expected)
def test_manual_filename(self):
"""Manually set a filename"""
expected = 'PSY - Gangnam Style'
self.yt.set_filename(expected)
eq_(self.yt.filename, expected)
def test_get_all_videos(self):
"""Get all videos"""
eq_(len(self.yt.get_videos()), 6)
def test_filter_video_by_extension(self):
"""Filter videos by filetype"""
eq_(len(self.yt.filter('mp4')), 2)
eq_(len(self.yt.filter('3gp')), 2)
eq_(len(self.yt.filter('webm')), 1)
eq_(len(self.yt.filter('flv')), 1)
def test_filter_video_by_extension_and_resolution(self):
"""Filter videos by file extension and resolution"""
eq_(len(self.yt.filter('mp4', '720p')), 1)
eq_(len(self.yt.filter('mp4', '1080p')), 0)
def test_filter_video_by_extension_resolution_profile(self):
"""Filter videos by file extension, resolution, and profile"""
eq_(len(self.yt.filter('mp4', '360p', 'Baseline')), 1)
def test_filter_video_by_profile(self):
"""Filter videos by file profile"""
eq_(len(self.yt.filter(profile='Simple')), 2)
def test_filter_video_by_resolution(self):
"""Filter videos by resolution"""
eq_(len(self.yt.filter(resolution='360p')), 2)
@raises(MultipleObjectsReturned)
def test_get_multiple_itmes(self):
"""get(...) cannot return more than one video"""
self.yt.get(profile='Simple')
self.yt.get('mp4')
self.yt.get(resolution='240p')
|