def test_success_download_nonyoutube(self): subs_id = str(uuid4()) self.item.data = textwrap.dedent(""" <video youtube="" sub="{}"> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"/> </video> """.format(subs_id)) modulestore().update_item(self.item, self.user.id) subs = { 'start': [100, 200, 240], 'end': [200, 240, 380], 'text': [ 'subs #1', 'subs #2', 'subs #3' ] } self.save_subs_to_store(subs, subs_id) link = reverse('download_transcripts') resp = self.client.get(link, {'locator': self.video_usage_key, 'subs_id': subs_id}) self.assertEqual(resp.status_code, 200) self.assertEqual( resp.content, '0\n00:00:00,100 --> 00:00:00,200\nsubs #1\n\n1\n00:00:00,200 --> ' '00:00:00,240\nsubs #2\n\n2\n00:00:00,240 --> 00:00:00,380\nsubs #3\n\n' ) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def choose_transcripts(request): """ Replaces html5 subtitles, presented for both html5 sources, with chosen one. Code removes rejected html5 subtitles and updates sub attribute with chosen html5_id. It does nothing with youtube id's. Returns: status `Success` and resulted item.sub value or status `Error` and HTTP 400. """ response = { 'status': 'Error', 'subs': '', } try: data, videos, item = _validate_transcripts_data(request) except TranscriptsRequestValidationException as e: return error_response(response, e.message) html5_id = data.get('html5_id') # html5_id chosen by user # find rejected html5_id and remove appropriate subs from store html5_id_to_remove = [x for x in videos['html5'] if x != html5_id] if html5_id_to_remove: remove_subs_from_store(html5_id_to_remove, item) if item.sub != html5_id: # update sub value item.sub = html5_id item.save_with_metadata(request.user) response = {'status': 'Success', 'subs': item.sub} return JsonResponse(response)
def test_success_download_nonyoutube(self): subs_id = str(uuid4()) self.item.data = textwrap.dedent(""" <video youtube="" sub="{}"> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"/> </video> """.format(subs_id)) modulestore().update_item(self.item, self.user.id) subs = { 'start': [100, 200, 240], 'end': [200, 240, 380], 'text': [ 'subs #1', 'subs #2', 'subs #3' ] } self.save_subs_to_store(subs, subs_id) link = reverse('download_transcripts') resp = self.client.get(link, {'locator': self.video_usage_key, 'subs_id': subs_id}) self.assertEqual(resp.status_code, 200) self.assertEqual( resp.content, '0\n00:00:00,100 --> 00:00:00,200\nsubs #1\n\n1\n00:00:00,200 --> ' '00:00:00,240\nsubs #2\n\n2\n00:00:00,240 --> 00:00:00,380\nsubs #3\n\n' ) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def test_successful_download_youtube(self): """ Tests downloading and saving transcripts from youtube """ subs_id = 'JMD_ifUUfsU' self.item.data = '<video youtube="1:' + subs_id + '" />' modulestore().update_item(self.item, self.user.id) subs = { 'start': [100, 200, 240], 'end': [200, 240, 380], 'text': [ 'subs #1', 'subs #2', 'subs #3' ] } def mock_youtube_download(youtube_id, video_descriptor, settings): # Mocks downloading from youtube by saving straight into modulestore self.save_subs_to_store(subs, youtube_id) with patch('contentstore.views.utilities.captions.download_youtube_subs') as youtube_download: youtube_download.side_effect = mock_youtube_download link = self.captions_url data = { 'update_array': json.dumps([self.item_location_string]), } resp = self.client.post(link, data, HTTP_ACCEPT='application/json') self.assertEqual(resp.status_code, 200) video_response_status = json.loads(resp.content)[0] self.assertDictEqual( video_response_status, { u'status': u'Success', u'html5_equal': False, u'youtube_local': True, u'is_youtube_mode': True, u'youtube_server': False, u'command': u'found', u'location': unicode(self.item_location_string), u'current_item_subs': subs_id, u'youtube_diff': True, u'html5_local': [], u'subs': subs_id, } ) # Ensure subs for video properly set item = modulestore().get_item(self.item_location) self.assertEqual(item.sub, subs_id) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def test_success_download_nonyoutube(self): subs_id = str(uuid4()) self.item.data = textwrap.dedent(""" <video youtube="" sub="{}"> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"/> </video> """.format(subs_id)) modulestore().update_item(self.item, self.user.id) subs = { 'start': [100, 200, 240], 'end': [200, 240, 380], 'text': [ 'subs #1', 'subs #2', 'subs #3' ] } self.save_subs_to_store(subs, subs_id) data = { 'locator': unicode(self.video_usage_key), 'videos': [{ 'type': 'html5', 'video': subs_id, 'mode': 'mp4', }] } link = reverse('check_transcripts') resp = self.client.get(link, {'data': json.dumps(data)}) self.assertEqual(resp.status_code, 200) self.assertDictEqual( json.loads(resp.content), { u'status': u'Success', u'subs': unicode(subs_id), u'youtube_local': False, u'is_youtube_mode': False, u'youtube_server': False, u'command': u'found', u'current_item_subs': unicode(subs_id), u'youtube_diff': True, u'html5_local': [unicode(subs_id)], u'html5_equal': False, } ) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def test_success_download_nonyoutube(self): subs_id = str(uuid4()) self.item.data = textwrap.dedent(""" <video youtube="" sub="{}"> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"/> </video> """.format(subs_id)) modulestore().update_item(self.item, self.user.id) subs = { 'start': [100, 200, 240], 'end': [200, 240, 380], 'text': [ 'subs #1', 'subs #2', 'subs #3' ] } self.save_subs_to_store(subs, subs_id) data = { 'locator': unicode(self.video_usage_key), 'videos': [{ 'type': 'html5', 'video': subs_id, 'mode': 'mp4', }] } link = reverse('check_transcripts') resp = self.client.get(link, {'data': json.dumps(data)}) self.assertEqual(resp.status_code, 200) self.assertDictEqual( json.loads(resp.content), { u'status': u'Success', u'subs': unicode(subs_id), u'youtube_local': False, u'is_youtube_mode': False, u'youtube_server': False, u'command': u'found', u'current_item_subs': unicode(subs_id), u'youtube_diff': True, u'html5_local': [unicode(subs_id)], u'html5_equal': False, } ) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def test_successful_download_youtube(self): """ Tests downloading and saving transcripts from youtube """ subs_id = 'JMD_ifUUfsU' self.item.data = '<video youtube="1:' + subs_id + '" />' modulestore().update_item(self.item, self.user.id) subs = { 'start': [100, 200, 240], 'end': [200, 240, 380], 'text': ['subs #1', 'subs #2', 'subs #3'] } def mock_youtube_download(youtube_id, video_descriptor, settings): # Mocks downloading from youtube by saving straight into modulestore self.save_subs_to_store(subs, youtube_id) with patch( 'contentstore.views.utilities.captions.download_youtube_subs' ) as youtube_download: youtube_download.side_effect = mock_youtube_download link = self.captions_url data = { 'update_array': json.dumps([self.item_location_string]), } resp = self.client.post(link, data, HTTP_ACCEPT='application/json') self.assertEqual(resp.status_code, 200) video_response_status = json.loads(resp.content)[0] self.assertDictEqual( video_response_status, { u'status': u'Success', u'html5_equal': False, u'youtube_local': True, u'is_youtube_mode': True, u'youtube_server': False, u'command': u'found', u'location': unicode(self.item_location_string), u'current_item_subs': subs_id, u'youtube_diff': True, u'html5_local': [], u'subs': subs_id, }) # Ensure subs for video properly set item = modulestore().get_item(self.item_location) self.assertEqual(item.sub, subs_id) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def test_success_download_nonyoutube(self): subs_id = str(uuid4()) self.item.data = textwrap.dedent(""" <video youtube="" sub="{}"> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"/> </video> """.format(subs_id)) modulestore().update_item(self.item, self.user.id) subs = { 'start': [100, 200, 240], 'end': [200, 240, 380], 'text': [ 'subs #1', 'subs #2', 'subs #3' ] } self.save_subs_to_store(subs, subs_id) link = self.captions_url data = { 'video': json.dumps({'location': self.item_location_string}), } resp = self.client.get(link, data, HTTP_ACCEPT='application/json') self.assertEqual(resp.status_code, 200) self.assertDictEqual( json.loads(resp.content), { u'status': True, u'location': self.item_location_string, u'command': u'use_existing', u'current_item_subs': subs_id, u'html5_equal': False, u'html5_local': [], u'is_youtube_mode': False, u'status': u'Success', u'subs': u'', u'youtube_diff': True, u'youtube_local': False, u'youtube_server': False } ) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def test_success_download_nonyoutube(self): subs_id = str(uuid4()) self.item.data = textwrap.dedent( """ <video youtube="" sub="{}"> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"/> </video> """.format( subs_id ) ) modulestore().update_item(self.item, self.user.id) subs = {"start": [100, 200, 240], "end": [200, 240, 380], "text": ["subs #1", "subs #2", "subs #3"]} self.save_subs_to_store(subs, subs_id) data = { "locator": unicode(self.video_usage_key), "videos": [{"type": "html5", "video": subs_id, "mode": "mp4"}], } link = reverse("check_transcripts") resp = self.client.get(link, {"data": json.dumps(data)}) self.assertEqual(resp.status_code, 200) self.assertDictEqual( json.loads(resp.content), { u"status": u"Success", u"subs": unicode(subs_id), u"youtube_local": False, u"is_youtube_mode": False, u"youtube_server": False, u"command": u"found", u"current_item_subs": unicode(subs_id), u"youtube_diff": True, u"html5_local": [unicode(subs_id)], u"html5_equal": False, }, ) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def test_success_download_nonyoutube(self): subs_id = str(uuid4()) self.item.data = textwrap.dedent(""" <video youtube="" sub="{}"> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"/> <source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.ogv"/> </video> """.format(subs_id)) modulestore().update_item(self.item, self.user.id) subs = { 'start': [100, 200, 240], 'end': [200, 240, 380], 'text': ['subs #1', 'subs #2', 'subs #3'] } self.save_subs_to_store(subs, subs_id) link = self.captions_url data = { 'video': json.dumps({'location': self.item_location_string}), } resp = self.client.get(link, data, HTTP_ACCEPT='application/json') self.assertEqual(resp.status_code, 200) self.assertDictEqual( json.loads(resp.content), { u'status': True, u'location': self.item_location_string, u'command': u'use_existing', u'current_item_subs': subs_id, u'html5_equal': False, u'html5_local': [], u'is_youtube_mode': False, u'status': u'Success', u'subs': u'', u'youtube_diff': True, u'youtube_local': False, u'youtube_server': False }) transcripts_utils.remove_subs_from_store(subs_id, self.item)
def save_transcripts(request): """ Saves video module with updated values of fields. Returns: status `Success` or status `Error` and HTTP 400. """ response = {'status': 'Error'} data = json.loads(request.GET.get('data', '{}')) if not data: return error_response(response, 'Incoming video data is empty.') try: item = _get_item(request, data) except (InvalidKeyError, ItemNotFoundError): return error_response(response, "Can't find item by locator.") metadata = data.get('metadata') if metadata is not None: new_sub = metadata.get('sub') for metadata_key, value in metadata.items(): setattr(item, metadata_key, value) item.save_with_metadata( request.user) # item becomes updated with new values if new_sub: manage_video_subtitles_save(item, request.user) else: # If `new_sub` is empty, it means that user explicitly does not want to use # transcripts for current video ids and we remove all transcripts from storage. current_subs = data.get('current_subs') if current_subs is not None: for sub in current_subs: remove_subs_from_store(sub, item) response['status'] = 'Success' return JsonResponse(response)
def save_transcripts(request): """ Saves video module with updated values of fields. Returns: status `Success` or status `Error` and HTTP 400. """ response = {'status': 'Error'} data = json.loads(request.GET.get('data', '{}')) if not data: return error_response(response, 'Incoming video data is empty.') try: item = _get_item(request, data) except (ItemNotFoundError, InvalidLocationError, InsufficientSpecificationError): return error_response(response, "Can't find item by locator.") metadata = data.get('metadata') if metadata is not None: new_sub = metadata.get('sub') for metadata_key, value in metadata.items(): setattr(item, metadata_key, value) item.save_with_metadata(request.user) # item becomes updated with new values if new_sub: manage_video_subtitles_save(item, request.user) else: # If `new_sub` is empty, it means that user explicitly does not want to use # transcripts for current video ids and we remove all transcripts from storage. current_subs = data.get('current_subs') if current_subs is not None: for sub in current_subs: remove_subs_from_store(sub, item) response['status'] = 'Success' return JsonResponse(response)
def save_transcripts(request): """ Saves video module with updated values of fields. Returns: status `Success` or status `Error` and HTTP 400. """ response = {"status": "Error"} data = json.loads(request.GET.get("data", "{}")) if not data: return error_response(response, "Incoming video data is empty.") try: item = _get_item(request, data) except (InvalidKeyError, ItemNotFoundError): return error_response(response, "Can't find item by locator.") metadata = data.get("metadata") if metadata is not None: new_sub = metadata.get("sub") for metadata_key, value in metadata.items(): setattr(item, metadata_key, value) item.save_with_metadata(request.user) # item becomes updated with new values if new_sub: manage_video_subtitles_save(item, request.user) else: # If `new_sub` is empty, it means that user explicitly does not want to use # transcripts for current video ids and we remove all transcripts from storage. current_subs = data.get("current_subs") if current_subs is not None: for sub in current_subs: remove_subs_from_store(sub, item) response["status"] = "Success" return JsonResponse(response)