Ejemplo n.º 1
0
 def to_internal_value(self, value):
     if not isinstance(value, basestring):
         raise serializers.ValidationError("Invalid subtitle data")
     try:
         return load_subtitles(
             self.context['language_code'], value,
             self.context['sub_format'])
     except babelsubs.SubtitleParserError:
         raise serializers.ValidationError("Invalid subtitle data")
Ejemplo n.º 2
0
 def to_internal_value(self, value):
     if not isinstance(value, basestring):
         raise serializers.ValidationError("Invalid subtitle data")
     if isinstance(value, unicode):
         value = value.encode('utf-8')
     try:
         return load_subtitles(self.context['language_code'], value,
                               self.context['sub_format'])
     except babelsubs.SubtitleParserError, e:
         logger.warn("Error parsing subtitles ({}/{})".format(
             self.context['video'].video_id, self.context['language_code']),
                     exc_info=True)
         raise serializers.ValidationError("Invalid subtitle data")
Ejemplo n.º 3
0
    def clean_draft(self):
        data = self.cleaned_data['draft']

        if data.size > SUBTITLE_FILESIZE_LIMIT_KB * 1024:
            raise forms.ValidationError(
                fmt(_(u'File size must be less than %(size)s kb.'),
                    size=SUBTITLE_FILESIZE_LIMIT_KB))

        parts = data.name.rsplit('.', 1)
        self.extension = parts[-1].lower()

        if self.extension not in SUBTITLE_FILE_FORMATS:
            raise forms.ValidationError(
                fmt(_(u'Unsupported format. Please upload one of '
                      u'the following: %(formats)s'),
                    formats=", ".join(SUBTITLE_FILE_FORMATS)))

        text = data.read()
        encoding = chardet.detect(text)['encoding']

        if not encoding:
            raise forms.ValidationError(_(u'Can not detect file encoding'))

        # For xml based formats we can't just convert to unicode, as the parser
        # will complain that the string encoding doesn't match the encoding
        # declaration in the xml file if it's not utf-8.
        is_xml = self.extension in ('dfxp', 'ttml', 'xml')
        decoded = force_unicode(text, encoding) if not is_xml else text

        try:
            # we don't know the language code yet, since we are early in the
            # clean process.  Set it to blank for now and we'll set it to the
            # correct value in save()
            self._parsed_subtitles = load_subtitles('', decoded,
                                                    self.extension)
        except TypeError, e:
            raise forms.ValidationError(e)
Ejemplo n.º 4
0
def add_team_videos(team_pk, user_pk, videos):
    from .permissions import can_add_videos_bulk
    from teams.models import Team, Project, TeamVideo
    from videos.models import Video
    from videos.types import video_type_registrar
    from auth.models import CustomUser as User
    from utils.subtitles import load_subtitles
    from subtitles.pipeline import add_subtitles
    user = User.objects.get(pk=int(user_pk))
    team = Team.objects.get(pk=int(team_pk))
    num_successful_videos = 0
    messages = []
    if can_add_videos_bulk(user):
        for video_item in videos:
            video_url = video_item['url']
            try:
                video_type = video_type_registrar.video_type_for_url(video_url)
                video_url = video_type.convert_to_video_url()
            except:
                messages.append(
                    fmt(_(u"Unknown video type: %(url)s\n"), url=video_url))
                continue
            title = u""
            description = u""
            if len(video_item['description']) > 0:
                description = video_item['description']
            set_values = {'is_public': team.is_visible}
            if len(video_item['title']) > 0:
                set_values['title'] = video_item['title']
            if len(video_item['description']) > 0:
                set_values['description'] = video_item['description']
            video, created = Video.get_or_create_for_url(video_url,
                                                         video_type,
                                                         user,
                                                         set_values=set_values,
                                                         in_task=True)
            if not created and video.get_team_video() is not None:
                messages.append(
                    fmt(_(u"Video is already part of a team: %(url)s\n"),
                        url=video_url))
                continue
            modified_video = False
            if not created:
                video.is_public = team.is_visible
                video.title = video_item['title']
                video.description = video_item['description']
                modified_video = True
            if 'language' in video_item and len(video_item['language']) > 0:
                language = video_item['language'].lower()
                if language in LANGUAGE_CHOICES:
                    video.primary_audio_language_code = language
                    modified_video = True
                else:
                    messages.append(
                        fmt(_(
                            u"Badly formated language for %(url)s: %(language)s, ignoring it."
                        ),
                            url=video_url,
                            language=video_item['language']))
            if 'duration' in video_item and len(video_item['duration']) > 0:
                try:
                    video.duration = int(video_item['duration'])
                    modified_video = True
                except:
                    messages.append(
                        fmt(_(
                            u"Badly formated duration for %(url)s: %(duration)s, ignoring it."
                        ),
                            url=video_url,
                            duration=video_item['duration']))
            if modified_video:
                video.save()
            if 'transcript' in video_item and len(
                    video_item['transcript']
            ) > 0 and video.primary_audio_language_code:
                try:
                    sub_type = video_item['transcript'].split(".")[-1]
                    r = requests.get(video_item['transcript'])
                    if r.ok:
                        subs = load_subtitles(
                            video.primary_audio_language_code, r.text,
                            sub_type)
                        version = add_subtitles(
                            video, video.primary_audio_language_code, subs)
                    else:
                        raise Exception("Request not successful")
                except Exception, e:
                    logger.error(
                        "Error while importing transcript file: {}".format(
                            str(e)))
                    messages.append(
                        fmt(_(
                            u"Invalid transcript file or language code for video %(url)s\n"
                        ),
                            url=video_url))
            if created:
                num_successful_videos += 1
            if 'project' in video_item and len(video_item['project']) > 0:
                project, created = Project.objects.get_or_create(
                    team=team,
                    slug=pan_slugify(video_item['project']),
                    defaults={'name': video_item['project']})
                project_id = project.id
            else:
                project_id = None
            team_video = TeamVideo.objects.create(video=video,
                                                  team=team,
                                                  project_id=project_id)
Ejemplo n.º 5
0
def add_team_videos(team_pk, user_pk, videos):
    from .permissions import can_add_videos_bulk
    from teams.models import Team, Project, TeamVideo
    from videos.models import Video
    from videos.types import video_type_registrar
    from auth.models import CustomUser as User
    from utils.subtitles import load_subtitles
    from subtitles.pipeline import add_subtitles
    user = User.objects.get(pk=int(user_pk))
    team = Team.objects.get(pk=int(team_pk))
    num_successful_videos = 0
    messages = []
    if can_add_videos_bulk(user):
        for video_item in videos:
            video_url = video_item['url']
            try:
                video_type = video_type_registrar.video_type_for_url(video_url)
                video_url = video_type.convert_to_video_url()
            except:
                messages.append(
                    fmt(_(u"Unknown video type: %(url)s\n"), url=video_url))
                continue

            def setup_video(video, video_url):
                video.is_public = team.videos_public()
                if video_item.get('title'):
                    video.title = video_item['title']
                if video_item.get('description'):
                    video.description = video_item['description']
                if video_item.get('language'):
                    language = video_item['language'].lower()
                    if language in SUPPORTED_LANGUAGE_CODES:
                        video.primary_audio_language_code = language
                    else:
                        messages.append(
                            fmt(_(
                                u"Badly formated language for %(url)s: %(language)s, ignoring it."
                            ),
                                url=video_url,
                                language=video_item['language']))
                if video_item.get('duration') and not video.duration:
                    try:
                        video.duration = int(video_item['duration'])
                    except:
                        messages.append(
                            fmt(_(
                                u"Badly formated duration for %(url)s: %(duration)s, ignoring it."
                            ),
                                url=video_url,
                                duration=video_item['duration']))
                if video_item.get('project'):
                    project, created = Project.objects.get_or_create(
                        team=team,
                        slug=pan_slugify(video_item['project']),
                        defaults={'name': video_item['project']})
                else:
                    project = team.default_project
                team_video = TeamVideo.objects.create(video=video,
                                                      team=team,
                                                      project=project,
                                                      added_by=user)

            try:
                video, video_url = Video.add(video_type, user, setup_video,
                                             team)
            except Video.DuplicateUrlError, e:
                messages.append(
                    fmt(_(u"Video is already added to team: %(url)s\n"),
                        url=e.video_url))
                continue

            if 'transcript' in video_item and len(
                    video_item['transcript']
            ) > 0 and video.primary_audio_language_code:
                try:
                    sub_type = video_item['transcript'].split(".")[-1]
                    r = requests.get(video_item['transcript'])
                    if r.ok:
                        subs = load_subtitles(
                            video.primary_audio_language_code, r.text,
                            sub_type)
                        version = add_subtitles(
                            video, video.primary_audio_language_code, subs)
                    else:
                        raise Exception("Request not successful")
                except Exception, e:
                    logger.error(
                        "Error while importing transcript file: {}".format(
                            str(e)))
                    messages.append(
                        fmt(_(
                            u"Invalid transcript file or language code for video %(url)s\n"
                        ),
                            url=video_url))
            num_successful_videos += 1