コード例 #1
0
 def test_get_transcript_val_exceptions(self, exception_to_raise, mock_Transcript):
     """
     Verify that `get_transcript_from_val` function raises `NotFoundError` when specified exceptions raised.
     """
     mock_Transcript.convert.side_effect = exception_to_raise
     transcripts_info = self.video.get_transcripts_info()
     lang = self.video.get_default_transcript_language(transcripts_info)
     edx_video_id = transcripts_utils.clean_video_id(self.video.edx_video_id)
     with self.assertRaises(NotFoundError):
         transcripts_utils.get_transcript_from_val(
             edx_video_id,
             lang=lang,
             output_format=transcripts_utils.Transcript.SRT
         )
コード例 #2
0
ファイル: test_transcripts_utils.py プロジェクト: saadow123/1
 def test_get_transcript_val_exceptions(self, exception_to_raise, mock_Transcript):
     """
     Verify that `get_transcript_from_val` function raises `NotFoundError` when specified exceptions raised.
     """
     mock_Transcript.convert.side_effect = exception_to_raise
     transcripts_info = self.video.get_transcripts_info()
     lang = self.video.get_default_transcript_language(transcripts_info)
     edx_video_id = transcripts_utils.clean_video_id(self.video.edx_video_id)
     with self.assertRaises(NotFoundError):
         transcripts_utils.get_transcript_from_val(
             edx_video_id,
             lang=lang,
             output_format=transcripts_utils.Transcript.SRT
         )
コード例 #3
0
def check_transcripts(request):
    """
    Check state of transcripts availability.

    request.GET['data'] has key `videos`, which can contain any of the following::

        [
            {u'type': u'youtube', u'video': u'OEoXaMPEzfM', u'mode': u'youtube'},
            {u'type': u'html5',    u'video': u'video1',             u'mode': u'mp4'}
            {u'type': u'html5',    u'video': u'video2',             u'mode': u'webm'}
        ]
        `type` is youtube or html5
        `video` is html5 or youtube video_id
        `mode` is youtube, ,p4 or webm

    Returns transcripts_presence dict::

        html5_local: list of html5 ids, if subtitles exist locally for them;
        is_youtube_mode: bool, if we have youtube_id, and as youtube mode is of higher priority, reflect this with flag;
        youtube_local: bool, if youtube transcripts exist locally;
        youtube_server: bool, if youtube transcripts exist on server;
        youtube_diff: bool, if youtube transcripts exist on youtube server, and are different from local youtube ones;
        current_item_subs: string, value of item.sub field;
        status: string, 'Error' or 'Success';
        subs: string, new value of item.sub field, that should be set in module;
        command: string, action to front-end what to do and what to show to user.
    """
    transcripts_presence = {
        'html5_local': [],
        'html5_equal': False,
        'is_youtube_mode': False,
        'youtube_local': False,
        'youtube_server': False,
        'youtube_diff': True,
        'current_item_subs': None,
        'status': 'Error',
    }

    try:
        __, videos, item = _validate_transcripts_data(request)
    except TranscriptsRequestValidationException as e:
        return error_response(transcripts_presence, text_type(e))

    transcripts_presence['status'] = 'Success'

    try:
        edx_video_id = clean_video_id(videos.get('edx_video_id'))
        get_transcript_from_val(edx_video_id=edx_video_id, lang=u'en')
        command = 'found'
    except NotFoundError:
        filename = 'subs_{0}.srt.sjson'.format(item.sub)
        content_location = StaticContent.compute_location(item.location.course_key, filename)
        try:
            local_transcripts = contentstore().find(content_location).data
            transcripts_presence['current_item_subs'] = item.sub
        except NotFoundError:
            pass

        # Check for youtube transcripts presence
        youtube_id = videos.get('youtube', None)
        if youtube_id:
            transcripts_presence['is_youtube_mode'] = True

            # youtube local
            filename = 'subs_{0}.srt.sjson'.format(youtube_id)
            content_location = StaticContent.compute_location(item.location.course_key, filename)
            try:
                local_transcripts = contentstore().find(content_location).data
                transcripts_presence['youtube_local'] = True
            except NotFoundError:
                log.debug(u"Can't find transcripts in storage for youtube id: %s", youtube_id)

            # youtube server
            youtube_text_api = copy.deepcopy(settings.YOUTUBE['TEXT_API'])
            youtube_text_api['params']['v'] = youtube_id
            youtube_transcript_name = youtube_video_transcript_name(youtube_text_api)
            if youtube_transcript_name:
                youtube_text_api['params']['name'] = youtube_transcript_name
            youtube_response = requests.get('http://' + youtube_text_api['url'], params=youtube_text_api['params'])

            if youtube_response.status_code == 200 and youtube_response.text:
                transcripts_presence['youtube_server'] = True
            #check youtube local and server transcripts for equality
            if transcripts_presence['youtube_server'] and transcripts_presence['youtube_local']:
                try:
                    youtube_server_subs = get_transcripts_from_youtube(
                        youtube_id,
                        settings,
                        item.runtime.service(item, "i18n")
                    )
                    if json.loads(local_transcripts) == youtube_server_subs:  # check transcripts for equality
                        transcripts_presence['youtube_diff'] = False
                except GetTranscriptsFromYouTubeException:
                    pass

        # Check for html5 local transcripts presence
        html5_subs = []
        for html5_id in videos['html5']:
            filename = 'subs_{0}.srt.sjson'.format(html5_id)
            content_location = StaticContent.compute_location(item.location.course_key, filename)
            try:
                html5_subs.append(contentstore().find(content_location).data)
                transcripts_presence['html5_local'].append(html5_id)
            except NotFoundError:
                log.debug(u"Can't find transcripts in storage for non-youtube video_id: %s", html5_id)
            if len(html5_subs) == 2:  # check html5 transcripts for equality
                transcripts_presence['html5_equal'] = json.loads(html5_subs[0]) == json.loads(html5_subs[1])

        command, __ = _transcripts_logic(transcripts_presence, videos)

    transcripts_presence.update({'command': command})
    return JsonResponse(transcripts_presence)
コード例 #4
0
def check_transcripts(request):
    """
    Check state of transcripts availability.

    request.GET['data'] has key `videos`, which can contain any of the following::

        [
            {u'type': u'youtube', u'video': u'OEoXaMPEzfM', u'mode': u'youtube'},
            {u'type': u'html5',    u'video': u'video1',             u'mode': u'mp4'}
            {u'type': u'html5',    u'video': u'video2',             u'mode': u'webm'}
        ]
        `type` is youtube or html5
        `video` is html5 or youtube video_id
        `mode` is youtube, ,p4 or webm

    Returns transcripts_presence dict::

        html5_local: list of html5 ids, if subtitles exist locally for them;
        is_youtube_mode: bool, if we have youtube_id, and as youtube mode is of higher priority, reflect this with flag;
        youtube_local: bool, if youtube transcripts exist locally;
        youtube_server: bool, if youtube transcripts exist on server;
        youtube_diff: bool, if youtube transcripts exist on youtube server, and are different from local youtube ones;
        current_item_subs: string, value of item.sub field;
        status: string, 'Error' or 'Success';
        subs: string, new value of item.sub field, that should be set in module;
        command: string, action to front-end what to do and what to show to user.
    """
    transcripts_presence = {
        'html5_local': [],
        'html5_equal': False,
        'is_youtube_mode': False,
        'youtube_local': False,
        'youtube_server': False,
        'youtube_diff': True,
        'current_item_subs': None,
        'status': 'Error',
    }

    try:
        __, videos, item = _validate_transcripts_data(request)
    except TranscriptsRequestValidationException as e:
        return error_response(transcripts_presence, text_type(e))

    transcripts_presence['status'] = 'Success'

    try:
        edx_video_id = clean_video_id(videos.get('edx_video_id'))
        get_transcript_from_val(edx_video_id=edx_video_id, lang=u'en')
        command = 'found'
    except NotFoundError:
        filename = 'subs_{0}.srt.sjson'.format(item.sub)
        content_location = StaticContent.compute_location(
            item.location.course_key, filename)
        try:
            local_transcripts = contentstore().find(
                content_location).data.decode('utf-8')
            transcripts_presence['current_item_subs'] = item.sub
        except NotFoundError:
            pass

        # Check for youtube transcripts presence
        youtube_id = videos.get('youtube', None)
        if youtube_id:
            transcripts_presence['is_youtube_mode'] = True

            # youtube local
            filename = 'subs_{0}.srt.sjson'.format(youtube_id)
            content_location = StaticContent.compute_location(
                item.location.course_key, filename)
            try:
                local_transcripts = contentstore().find(
                    content_location).data.decode('utf-8')
                transcripts_presence['youtube_local'] = True
            except NotFoundError:
                log.debug(
                    u"Can't find transcripts in storage for youtube id: %s",
                    youtube_id)

            # youtube server
            youtube_text_api = copy.deepcopy(settings.YOUTUBE['TEXT_API'])
            youtube_text_api['params']['v'] = youtube_id
            youtube_transcript_name = youtube_video_transcript_name(
                youtube_text_api)
            if youtube_transcript_name:
                youtube_text_api['params']['name'] = youtube_transcript_name
            youtube_response = requests.get('http://' +
                                            youtube_text_api['url'],
                                            params=youtube_text_api['params'])

            if youtube_response.status_code == 200 and youtube_response.text:
                transcripts_presence['youtube_server'] = True
            #check youtube local and server transcripts for equality
            if transcripts_presence['youtube_server'] and transcripts_presence[
                    'youtube_local']:
                try:
                    youtube_server_subs = get_transcripts_from_youtube(
                        youtube_id, settings,
                        item.runtime.service(item, "i18n"))
                    if json.loads(
                            local_transcripts
                    ) == youtube_server_subs:  # check transcripts for equality
                        transcripts_presence['youtube_diff'] = False
                except GetTranscriptsFromYouTubeException:
                    pass

        # Check for html5 local transcripts presence
        html5_subs = []
        for html5_id in videos['html5']:
            filename = 'subs_{0}.srt.sjson'.format(html5_id)
            content_location = StaticContent.compute_location(
                item.location.course_key, filename)
            try:
                html5_subs.append(contentstore().find(content_location).data)
                transcripts_presence['html5_local'].append(html5_id)
            except NotFoundError:
                log.debug(
                    u"Can't find transcripts in storage for non-youtube video_id: %s",
                    html5_id)
            if len(html5_subs) == 2:  # check html5 transcripts for equality
                transcripts_presence['html5_equal'] = (json.loads(
                    html5_subs[0].decode('utf-8')) == json.loads(
                        html5_subs[1].decode('utf-8')))

        command, __ = _transcripts_logic(transcripts_presence, videos)

    transcripts_presence.update({'command': command})
    return JsonResponse(transcripts_presence)