def test_fail_generating_subs(self):
     sjson_subs = {
         'start': [100, 200],
         'end': [100],
         'text': [
             'subs #1',
             'subs #2'
         ]
     }
     srt_subs = transcripts_utils.generate_srt_from_sjson(sjson_subs, 1)
     self.assertFalse(srt_subs)
 def test_success_generating_subs_speed_up(self):
     sjson_subs = {
         'start': [100, 200, 240, 390, 54000],
         'end': [200, 240, 380, 1000, 78400],
         'text': ['subs #1', 'subs #2', 'subs #3', 'subs #4', 'subs #5']
     }
     srt_subs = transcripts_utils.generate_srt_from_sjson(sjson_subs, 0.5)
     self.assertTrue(srt_subs)
     expected_subs = [
         '00:00:00,050 --> 00:00:00,100\nsubs #1',
         '00:00:00,100 --> 00:00:00,120\nsubs #2',
         '00:00:00,120 --> 00:00:00,190\nsubs #3',
         '00:00:00,195 --> 00:00:00,500\nsubs #4',
         '00:00:27,000 --> 00:00:39,200\nsubs #5',
     ]
     for sub in expected_subs:
         self.assertIn(sub, srt_subs)
 def test_success_generating_subs_speed_up(self):
     sjson_subs = {
         "start": [100, 200, 240, 390, 54000],
         "end": [200, 240, 380, 1000, 78400],
         "text": ["subs #1", "subs #2", "subs #3", "subs #4", "subs #5"],
     }
     srt_subs = transcripts_utils.generate_srt_from_sjson(sjson_subs, 0.5)
     self.assertTrue(srt_subs)
     expected_subs = [
         "00:00:00,050 --> 00:00:00,100\nsubs #1",
         "00:00:00,100 --> 00:00:00,120\nsubs #2",
         "00:00:00,120 --> 00:00:00,190\nsubs #3",
         "00:00:00,195 --> 00:00:00,500\nsubs #4",
         "00:00:27,000 --> 00:00:39,200\nsubs #5",
     ]
     for sub in expected_subs:
         self.assertIn(sub, srt_subs)
Exemple #4
0
def download_transcripts(request):
    """
    Passes to user requested transcripts file.

    Raises Http404 if unsuccessful.
    """
    locator = request.GET.get('locator')
    if not locator:
        log.debug('GET data without "locator" property.')
        raise Http404

    try:
        item = _get_item(request, request.GET)
    except (InvalidKeyError, ItemNotFoundError):
        log.debug("Can't find item by locator.")
        raise Http404

    subs_id = request.GET.get('subs_id')
    if not subs_id:
        log.debug('GET data without "subs_id" property.')
        raise Http404

    if item.category != 'video':
        log.debug('transcripts are supported only for video" modules.')
        raise Http404

    filename = 'subs_{0}.srt.sjson'.format(subs_id)
    content_location = StaticContent.compute_location(item.location.course_key,
                                                      filename)
    try:
        sjson_transcripts = contentstore().find(content_location)
        log.debug("Downloading subs for %s id", subs_id)
        str_subs = generate_srt_from_sjson(json.loads(sjson_transcripts.data),
                                           speed=1.0)
        if not str_subs:
            log.debug('generate_srt_from_sjson produces no subtitles')
            raise Http404
        response = HttpResponse(str_subs, content_type='application/x-subrip')
        response[
            'Content-Disposition'] = 'attachment; filename="{0}.srt"'.format(
                subs_id)
        return response
    except NotFoundError:
        log.debug("Can't find content in storage for %s subs", subs_id)
        raise Http404
Exemple #5
0
    def test_success_generating_subs_speed_down(self):
        sjson_subs = {
            'start': [100, 200, 240, 390, 54000],
            'end': [200, 240, 380, 1000, 78400],
            'text': ['subs #1', 'subs #2', 'subs #3', 'subs #4', 'subs #5']
        }
        srt_subs = transcripts_utils.generate_srt_from_sjson(sjson_subs, 2)
        self.assertTrue(srt_subs)

        expected_subs = [
            '00:00:00,200 --> 00:00:00,400\nsubs #1',
            '00:00:00,400 --> 00:00:00,480\nsubs #2',
            '00:00:00,480 --> 00:00:00,760\nsubs #3',
            '00:00:00,780 --> 00:00:02,000\nsubs #4',
            '00:01:48,000 --> 00:02:36,800\nsubs #5',
        ]
        for sub in expected_subs:
            self.assertIn(sub, srt_subs)
    def test_success_generating_subs_speed_down(self):
        sjson_subs = {
            "start": [100, 200, 240, 390, 54000],
            "end": [200, 240, 380, 1000, 78400],
            "text": ["subs #1", "subs #2", "subs #3", "subs #4", "subs #5"],
        }
        srt_subs = transcripts_utils.generate_srt_from_sjson(sjson_subs, 2)
        self.assertTrue(srt_subs)

        expected_subs = [
            "00:00:00,200 --> 00:00:00,400\nsubs #1",
            "00:00:00,400 --> 00:00:00,480\nsubs #2",
            "00:00:00,480 --> 00:00:00,760\nsubs #3",
            "00:00:00,780 --> 00:00:02,000\nsubs #4",
            "00:01:48,000 --> 00:02:36,800\nsubs #5",
        ]
        for sub in expected_subs:
            self.assertIn(sub, srt_subs)
def download_transcripts(request):
    """
    Passes to user requested transcripts file.

    Raises Http404 if unsuccessful.
    """
    locator = request.GET.get('locator')
    if not locator:
        log.debug('GET data without "locator" property.')
        raise Http404

    try:
        item = _get_item(request, request.GET)
    except (ItemNotFoundError, InvalidLocationError, InsufficientSpecificationError):
        log.debug("Can't find item by locator.")
        raise Http404

    subs_id = request.GET.get('subs_id')
    if not subs_id:
        log.debug('GET data without "subs_id" property.')
        raise Http404

    if item.category != 'video':
        log.debug('transcripts are supported only for video" modules.')
        raise Http404

    filename = 'subs_{0}.srt.sjson'.format(subs_id)
    content_location = StaticContent.compute_location(
        item.location.org, item.location.course, filename
    )
    try:
        sjson_transcripts = contentstore().find(content_location)
        log.debug("Downloading subs for %s id", subs_id)
        str_subs = generate_srt_from_sjson(json.loads(sjson_transcripts.data), speed=1.0)
        if not str_subs:
            log.debug('generate_srt_from_sjson produces no subtitles')
            raise Http404
        response = HttpResponse(str_subs, content_type='application/x-subrip')
        response['Content-Disposition'] = 'attachment; filename="{0}.srt"'.format(subs_id)
        return response
    except NotFoundError:
        log.debug("Can't find content in storage for %s subs", subs_id)
        raise Http404
 def test_success_generating_subs_speed_up(self):
     sjson_subs = {
         'start': [100, 200, 240, 390, 54000],
         'end': [200, 240, 380, 1000, 78400],
         'text': [
             'subs #1',
             'subs #2',
             'subs #3',
             'subs #4',
             'subs #5'
         ]
     }
     srt_subs = transcripts_utils.generate_srt_from_sjson(sjson_subs, 0.5)
     self.assertTrue(srt_subs)
     expected_subs = [
         '00:00:00,050 --> 00:00:00,100\nsubs #1',
         '00:00:00,100 --> 00:00:00,120\nsubs #2',
         '00:00:00,120 --> 00:00:00,190\nsubs #3',
         '00:00:00,195 --> 00:00:00,500\nsubs #4',
         '00:00:27,000 --> 00:00:39,200\nsubs #5',
     ]
     for sub in expected_subs:
         self.assertIn(sub, srt_subs)
    def test_success_generating_subs_speed_down(self):
        sjson_subs = {
            'start': [100, 200, 240, 390, 54000],
            'end': [200, 240, 380, 1000, 78400],
            'text': [
                'subs #1',
                'subs #2',
                'subs #3',
                'subs #4',
                'subs #5'
            ]
        }
        srt_subs = transcripts_utils.generate_srt_from_sjson(sjson_subs, 2)
        self.assertTrue(srt_subs)

        expected_subs = [
            '00:00:00,200 --> 00:00:00,400\nsubs #1',
            '00:00:00,400 --> 00:00:00,480\nsubs #2',
            '00:00:00,480 --> 00:00:00,760\nsubs #3',
            '00:00:00,780 --> 00:00:02,000\nsubs #4',
            '00:01:48,000 --> 00:02:36,800\nsubs #5',
        ]
        for sub in expected_subs:
            self.assertIn(sub, srt_subs)
 def test_fail_generating_subs(self):
     sjson_subs = {"start": [100, 200], "end": [100], "text": ["subs #1", "subs #2"]}
     srt_subs = transcripts_utils.generate_srt_from_sjson(sjson_subs, 1)
     self.assertFalse(srt_subs)