def test_remove_file(self, mock): file_path = 'subtitle_' + \ hashlib.md5('v2309jfGew'.encode('utf-8')).hexdigest() + '.en.vtt' video_id = 'v2309jfGew' storage = Storage(video_id) storage.remove_file() mock.assert_called_with(file_path)
def get_captions(self) -> str: output = '' for url in self.urls: result = self.get_result(url) if result != 0: raise Exception( 'Unable to download and extract captions: {0}'.format( result)) storage = Storage(url) file_path = storage.get_file_path() try: with open(file_path) as f: output += self.get_captions_from_output(f.read(), url) storage.remove_file() except FileNotFoundError: if len(self.urls) == 1: raise NoCaptionsException("no captions found.") else: print("WARNING: no captions found for {}".format(url)) # remove final newline if len(output) > 0 and output[-1] == '\n': output = output[:-1] return output
def get_captions(self, video_id: str) -> str: result = self.get_result(video_id) if result != 0: raise Exception( 'Unable to download and extract captions: {0}'.format(result)) storage = Storage(video_id) file_path = storage.get_file_path() with open(file_path) as f: output = self.get_captions_from_output(f.read()) storage.remove_file() return output
def get_captions(self, video_id: str, language: str = 'it') -> dict: result = self.get_result(video_id, language) if result != 0: raise Exception( 'Unable to download and extract captions: {0}'.format(result)) output = {} for file_path in glob.glob("*.vtt"): storage = Storage(file_path) id = storage.get_video_id() with open(file_path) as f: output[id] = (self.get_captions_from_output(f.read())) storage.remove_file() return output
def test_valid(self): video_id = 'jNQXAC9IVRw' hashed_video_id = hashlib.md5(video_id.encode('utf-8')).hexdigest() storage = Storage(video_id) expected = 'subtitle_{0}.en.vtt'.format(hashed_video_id) self.assertEqual(expected, storage.get_file_path())
def test_valid(self): video_id = 'jNQXAC9IVRw' storage = Storage(video_id) expected = 'subtitle_{0}.en.vtt'.format(video_id) self.assertEqual(expected, storage.get_file_path())
def test_invalid(self): video_id = 'vsdoi&*@//../../' storage = Storage(video_id) with self.assertRaises(ValueError): storage.get_file_path()
def test_valid_with_dash(self): video_id = 'x1E0AzpWM-o' storage = Storage(video_id) expected = 'subtitle_{0}.en.vtt'.format(video_id) self.assertEqual(expected, storage.get_file_path())
def test_valid_with_underscore(self): video_id = 'w8U6VI_51Bg' storage = Storage(video_id) expected = 'subtitle_{0}.en.vtt'.format(video_id) self.assertEqual(expected, storage.get_file_path())
def test_remove_file(self, mock): video_id = 'v2309jfGew' file_path = 'subtitle_v2309jfGew.en.vtt' storage = Storage(video_id) storage.remove_file() mock.assert_called_with(file_path)