Commit 1f5c95ba by Alexander Kryklia

Remove unused code when download from youtube.

parent 54399533
...@@ -164,9 +164,11 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase): ...@@ -164,9 +164,11 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase):
number = '999' number = '999'
display_name = 'Test course' display_name = 'Test course'
def clear_subs_content(self, youtube_subs):
"""Remove, if subtitles content exists.""" def clear_sub_content(self, subs_id):
for subs_id in youtube_subs.values(): """
Remove, if subtitle content exists.
"""
filename = 'subs_{0}.srt.sjson'.format(subs_id) filename = 'subs_{0}.srt.sjson'.format(subs_id)
content_location = StaticContent.compute_location(self.course.id, filename) content_location = StaticContent.compute_location(self.course.id, filename)
try: try:
...@@ -175,6 +177,15 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase): ...@@ -175,6 +177,15 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase):
except NotFoundError: except NotFoundError:
pass pass
def clear_subs_content(self, youtube_subs):
"""
Remove, if subtitles content exists.
youtube_subs: dict of '{speed: youtube_id}' format for different speeds.
"""
for subs_id in youtube_subs.values():
self.clear_sub_content(subs_id)
def setUp(self): def setUp(self):
self.course = CourseFactory.create( self.course = CourseFactory.create(
org=self.org, number=self.number, display_name=self.display_name) org=self.org, number=self.number, display_name=self.display_name)
...@@ -193,29 +204,22 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase): ...@@ -193,29 +204,22 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase):
<text start="5.43" dur="1.73">Test text 3.</text> <text start="5.43" dur="1.73">Test text 3.</text>
</transcript> </transcript>
""") """)
good_youtube_subs = { good_youtube_sub = 'good_id_2'
0.5: 'good_id_1', self.clear_sub_content(good_youtube_sub)
1.0: 'good_id_2',
2.0: 'good_id_3'
}
self.clear_subs_content(good_youtube_subs)
with patch('xmodule.video_module.transcripts_utils.requests.get') as mock_get: with patch('xmodule.video_module.transcripts_utils.requests.get') as mock_get:
mock_get.return_value = Mock(status_code=200, text=response, content=response) mock_get.return_value = Mock(status_code=200, text=response, content=response)
# Check transcripts_utils.GetTranscriptsFromYouTubeException not thrown # Check transcripts_utils.GetTranscriptsFromYouTubeException not thrown
transcripts_utils.download_youtube_subs(good_youtube_subs, self.course, settings) transcripts_utils.download_youtube_subs(good_youtube_sub, self.course, settings)
mock_get.assert_any_call('http://video.google.com/timedtext', params={'lang': 'en', 'v': 'good_id_1'})
mock_get.assert_any_call('http://video.google.com/timedtext', params={'lang': 'en', 'v': 'good_id_2'}) mock_get.assert_any_call('http://video.google.com/timedtext', params={'lang': 'en', 'v': 'good_id_2'})
mock_get.assert_any_call('http://video.google.com/timedtext', params={'lang': 'en', 'v': 'good_id_3'})
# Check assets status after importing subtitles. # Check asset status after import of transcript.
for subs_id in good_youtube_subs.values(): filename = 'subs_{0}.srt.sjson'.format(good_youtube_sub)
filename = 'subs_{0}.srt.sjson'.format(subs_id)
content_location = StaticContent.compute_location(self.course.id, filename) content_location = StaticContent.compute_location(self.course.id, filename)
self.assertTrue(contentstore().find(content_location)) self.assertTrue(contentstore().find(content_location))
self.clear_subs_content(good_youtube_subs) self.clear_sub_content(good_youtube_sub)
def test_subs_for_html5_vid_with_periods(self): def test_subs_for_html5_vid_with_periods(self):
""" """
...@@ -235,25 +239,21 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase): ...@@ -235,25 +239,21 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase):
mock_get.return_value = Mock(status_code=404, text='Error 404') mock_get.return_value = Mock(status_code=404, text='Error 404')
bad_youtube_subs = { bad_youtube_sub = 'BAD_YOUTUBE_ID2'
0.5: 'BAD_YOUTUBE_ID1', self.clear_sub_content(bad_youtube_sub)
1.0: 'BAD_YOUTUBE_ID2',
2.0: 'BAD_YOUTUBE_ID3'
}
self.clear_subs_content(bad_youtube_subs)
with self.assertRaises(transcripts_utils.GetTranscriptsFromYouTubeException): with self.assertRaises(transcripts_utils.GetTranscriptsFromYouTubeException):
transcripts_utils.download_youtube_subs(bad_youtube_subs, self.course, settings) transcripts_utils.download_youtube_subs(bad_youtube_sub, self.course, settings)
# Check assets status after importing subtitles. # Check asset status after import of transcript.
for subs_id in bad_youtube_subs.values(): filename = 'subs_{0}.srt.sjson'.format(bad_youtube_sub)
filename = 'subs_{0}.srt.sjson'.format(subs_id)
content_location = StaticContent.compute_location( content_location = StaticContent.compute_location(
self.course.id, filename self.course.id, filename
) )
with self.assertRaises(NotFoundError): with self.assertRaises(NotFoundError):
contentstore().find(content_location) contentstore().find(content_location)
self.clear_subs_content(bad_youtube_subs) self.clear_sub_content(bad_youtube_sub)
def test_success_downloading_chinese_transcripts(self): def test_success_downloading_chinese_transcripts(self):
...@@ -262,13 +262,11 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase): ...@@ -262,13 +262,11 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase):
# Re-enable when `requests.get` is patched using `mock.patch` # Re-enable when `requests.get` is patched using `mock.patch`
raise SkipTest raise SkipTest
good_youtube_subs = { good_youtube_sub = 'j_jEn79vS3g' # Chinese, utf-8
1.0: 'j_jEn79vS3g', # Chinese, utf-8 self.clear_sub_content(good_youtube_sub)
}
self.clear_subs_content(good_youtube_subs)
# Check transcripts_utils.GetTranscriptsFromYouTubeException not thrown # Check transcripts_utils.GetTranscriptsFromYouTubeException not thrown
transcripts_utils.download_youtube_subs(good_youtube_subs, self.course, settings) transcripts_utils.download_youtube_subs(good_youtube_sub, self.course, settings)
# Check assets status after importing subtitles. # Check assets status after importing subtitles.
for subs_id in good_youtube_subs.values(): for subs_id in good_youtube_subs.values():
...@@ -278,7 +276,7 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase): ...@@ -278,7 +276,7 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase):
) )
self.assertTrue(contentstore().find(content_location)) self.assertTrue(contentstore().find(content_location))
self.clear_subs_content(good_youtube_subs) self.clear_sub_content(good_youtube_sub)
class TestGenerateSubsFromSource(TestDownloadYoutubeSubs): class TestGenerateSubsFromSource(TestDownloadYoutubeSubs):
......
...@@ -402,7 +402,7 @@ def replace_transcripts(request): ...@@ -402,7 +402,7 @@ def replace_transcripts(request):
return error_response(response, 'YouTube id {} is not presented in request data.'.format(youtube_id)) return error_response(response, 'YouTube id {} is not presented in request data.'.format(youtube_id))
try: try:
download_youtube_subs({1.0: youtube_id}, item, settings) download_youtube_subs(youtube_id, item, settings)
except GetTranscriptsFromYouTubeException as e: except GetTranscriptsFromYouTubeException as e:
return error_response(response, e.message) return error_response(response, e.message)
......
...@@ -132,67 +132,29 @@ def get_transcripts_from_youtube(youtube_id, settings, i18n): ...@@ -132,67 +132,29 @@ def get_transcripts_from_youtube(youtube_id, settings, i18n):
return {'start': sub_starts, 'end': sub_ends, 'text': sub_texts} return {'start': sub_starts, 'end': sub_ends, 'text': sub_texts}
def download_youtube_subs(youtube_subs, item, settings): def download_youtube_subs(youtube_id, video_descriptor, settings):
""" """
Download transcripts from Youtube and save them to assets. Download transcripts from Youtube and save them to assets.
Args: Args:
youtube_subs: dictionary of `speed: youtube_id` key:value pairs. youtube_id: str, actual youtube_id of the video.
item: video module instance. video_descriptor: video descriptor instance.
Returns: None, if transcripts were successfully downloaded and saved. We save transcripts for 1.0 speed, as for other speed conversion is done on front-end.
Otherwise raises GetTranscriptsFromYouTubeException.
Returns:
None, if transcripts were successfully downloaded and saved.
Raises:
GetTranscriptsFromYouTubeException, if fails.
""" """
i18n = item.runtime.service(item, "i18n") i18n = video_descriptor.runtime.service(video_descriptor, "i18n")
_ = i18n.ugettext _ = i18n.ugettext
highest_speed = highest_speed_subs = None
missed_speeds = []
# Iterate from lowest to highest speed and try to do download transcripts
# from the Youtube service.
for speed, youtube_id in sorted(youtube_subs.iteritems()):
if not youtube_id:
continue
try:
subs = get_transcripts_from_youtube(youtube_id, settings, i18n) subs = get_transcripts_from_youtube(youtube_id, settings, i18n)
if not subs: # if empty subs are returned save_subs_to_store(subs, youtube_id, video_descriptor)
raise GetTranscriptsFromYouTubeException
except GetTranscriptsFromYouTubeException:
missed_speeds.append(speed)
continue
save_subs_to_store(subs, youtube_id, item)
log.info( log.info("Transcripts for youtube_id %s for 1.0 speed are downloaded and saved.", youtube_id)
"Transcripts for YouTube id %s (speed %s)"
"are downloaded and saved.", youtube_id, speed
)
highest_speed = speed
highest_speed_subs = subs
if not highest_speed:
raise GetTranscriptsFromYouTubeException(_("Can't find any transcripts on the Youtube service."))
# When we exit from the previous loop, `highest_speed` and `highest_speed_subs`
# are the transcripts data for the highest speed available on the
# Youtube service. We use the highest speed as main speed for the
# generation other transcripts, cause during calculation timestamps
# for lower speeds we just use multiplication instead of division.
for speed in missed_speeds: # Generate transcripts for missed speeds.
save_subs_to_store(
generate_subs(speed, highest_speed, highest_speed_subs),
youtube_subs[speed],
item
)
log.info(
"Transcripts for YouTube id %s (speed %s)"
"are generated from YouTube id %s (speed %s) and saved",
youtube_subs[speed], speed,
youtube_subs[highest_speed],
highest_speed
)
def remove_subs_from_store(subs_id, item, lang='en'): def remove_subs_from_store(subs_id, item, lang='en'):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment