def test_deprecation_warnings_on_url_set(self): """Deprecation warnings get triggered on url set""" with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") with mock.patch('pytube.api.urlopen') as urlopen: urlopen.return_value.read.return_value = self.mock_html yt = api.YouTube() yt._js_cache = self.mock_js yt.url = 'http://www.youtube.com/watch?v=9bZkp7q19f0' eq_(len(w), 1)
def test_age_restricted_video(self): """Raise exception on age restricted video""" url = 'http://www.youtube.com/watch?v=nzNgkc6t260' with open('tests/mock_data/youtube_age_restricted.html') as fh: mock_html = fh.read() with mock.patch('pytube.api.urlopen') as urlopen: urlopen.return_value.read.return_value = mock_html yt = api.YouTube() yt.from_url(url)
def download_video(url,changed_name=None): yt = api.YouTube(url) pprint(yt.get_videos()) # [<Video: MPEG-4 Visual (.3gp) - 144p>, # <Video: MPEG-4 Visual (.3gp) - 240p>, # <Video: Sorenson H.263 (.flv) - 240p>, # <Video: H.264 (.flv) - 360p>, # <Video: H.264 (.flv) - 480p>, # <Video: H.264 (.mp4) - 360p>, # <Video: H.264 (.mp4) - 720p>, # <Video: VP8 (.webm) - 360p>, # <Video: VP8 (.webm) - 480p>] # set the filename: if changed_name: yt.set_filename(changed_name) yt.set_filename(yt.filename.replace(" ", "_")) yt.set_filename(yt.filename.replace("&", "and")) yt.set_filename((yt.filename).encode('ascii', 'ignore').decode('ascii')) log('Video Name: ', yt.filename) # Notice that the list is ordered by lowest resolution to highest. If you # wanted the highest resolution available for a specific file type, you # can simply do: log('Highest mp4 resolution video: ', yt.filter('mp4')[-1]) if not yt.filter(extension='mp4'): log('No mp4 vidoes found!', color='red') return 'No Video Found' video = yt.filter('mp4')[-1] current_dir = os.path.dirname(os.path.realpath(__file__)) video_folder_path = os.path.join(current_dir, 'example_images', yt.filename) if not os.path.isdir(video_folder_path): os.makedirs(video_folder_path) try: log('Downloading!', color='green') video.download(video_folder_path) log('Finished downloading!', color='green') except: #TODO TODO TODO TEST TEST TEST TEST BELOW CAREFUL in case recursive os.rmdir(video_folder_path) else: log('Folder and file already there:', video_folder_path, color='red') return yt.filename
def setUp(self): url = 'http://www.youtube.com/watch?v=9bZkp7q19f0' with open('tests/mock_data/youtube_gangnam_style.html') as fh: self.mock_html = fh.read() with open('tests/mock_data/youtube_gangnam_style.js') as fh: self.mock_js = 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 yt_video(): url = 'http://www.youtube.com/watch?v=9bZkp7q19f0' mock_html = None mock_js = None with open('tests/mock_data/youtube_gangnam_style.html') as fh: mock_html = fh.read() with open('tests/mock_data/youtube_gangnam_style.js') as fh: mock_js = fh.read() with mock.patch('pytube.api.urlopen') as urlopen: urlopen.return_value.read.return_value = mock_html yt = api.YouTube() yt._js_cache = mock_js yt.from_url(url) return yt
def test_YT_create_from_url(self): 'test creation of YouYube Object from url' yt = api.YouTube(self.url)
def test_YT_create_from_url(self): 'test creation of YouYube Object from url' url = 'http://www.youtube.com/watch?v=9bZkp7q19f0' yt = api.YouTube(url)