Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_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, video_id: str, language: str = 'en') -> str:
result = self.get_result(video_id, language)
if result != 0:
raise Exception(
'Unable to download and extract captions: {0}'.format(result))
storage = Storage(video_id, language)
file_path = storage.get_file_path()
with open(file_path) as f:
output = self.get_captions_from_output(f.read(), language)
storage.remove_file()
return output
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