def load_and_init_from_playback_file(filename, mock_urlopen): """Load a gzip json playback file and create YouTube instance.""" pb = load_playback_file(filename) # Mock the responses to YouTube mock_url_open_object = mock.Mock() mock_url_open_object.read.side_effect = [ pb['watch_html'].encode('utf-8'), pb['js'].encode('utf-8') ] mock_urlopen.return_value = mock_url_open_object # Pytest caches this result, so we can speed up the tests # by causing the object to fetch all the relevant information # it needs. Previously, this was handled by prefetch_init() # and descramble(), but this functionality has since been # deferred v = YouTube(pb["url"]) v.watch_html v._vid_info = pb['vid_info'] v.js v.fmt_streams return v