Exemple #1
0
def GetAndPrintUserUploads(uri):
    yt_service = YouTubeService()

    try:
        return yt_service.GetYouTubeVideoEntry(
            video_id=uri).media.duration.seconds
    except:
        return False
Exemple #2
0
def add_video(request):
    err = check_arguments(request, ["videoID"])
    if err: return err

    videoID = request.POST["videoID"]

    # check to see if video exists for user
    # TODO refactor to use get_video_query
    if Video.objects.filter(user=request.user, videoID=videoID).count() != 0:
        return HttpResponse(status=409,
                            content="videoID %s already exists for user" %
                            videoID)

    # look up video in youtube api
    from gdata.youtube.service import YouTubeService
    yt_service = YouTubeService()
    data = yt_service.GetYouTubeVideoEntry(video_id=videoID)

    # save new video for user
    v = Video(videoID=videoID, user=request.user, title=data.media.title.text)
    v.save()

    return HttpResponse(status=201, content=json.dumps(v.render()))
Exemple #3
0
    def manageProperties(self, REQUEST=None, **kwargs):
        """ """
        if not self.checkPermissionEditObject():
            raise EXCEPTION_NOTAUTHORIZED(EXCEPTION_NOTAUTHORIZED_MSG)

        if REQUEST is not None:
            schema_raw_data = dict(REQUEST.form)
        else:
            schema_raw_data = kwargs
        _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None))
        _releasedate = self.process_releasedate(
            schema_raw_data.pop('releasedate', ''), self.releasedate)
        _approved = int(bool(schema_raw_data.pop('approved', False)))

        if schema_raw_data['iframe_width'] in ['', '0']:
            schema_raw_data['iframe_width'] = 640
        if schema_raw_data['iframe_height'] in ['', '0']:
            schema_raw_data['iframe_height'] = 360

        if len(schema_raw_data['youtube_id']) > 11:
            try:
                schema_raw_data['youtube_id'] = schema_raw_data[
                    'youtube_id'].split('watch?v=')[1][:11]
            except IndexError:
                schema_raw_data['youtube_id'] = schema_raw_data[
                    'youtube_id'].split('&v=')[1][:11]

        form_errors = self.process_submitted_form(
            schema_raw_data, _lang, _override_releasedate=_releasedate)
        try:
            schema_raw_data['iframe_width'] = int(
                schema_raw_data['iframe_width'])
        except ValueError:
            form_errors['iframe_width'] = ['Integer value required.']
        try:
            schema_raw_data['iframe_height'] = int(
                schema_raw_data['iframe_height'])
        except ValueError:
            form_errors['iframe_height'] = ['Integer value required.']
        if not schema_raw_data['youtube_id']:
            form_errors['youtube_id'] = ['Youtube Id is mandatory']

        if schema_raw_data['youtube_id']:
            yt_service = YouTubeService()
            try:
                yt_service.GetYouTubeVideoEntry(
                    video_id=schema_raw_data['youtube_id'])
            except RequestError:
                form_errors['youtube_id'] = [
                    'Invalid Youtube ID (inexisting video)'
                ]

        if form_errors:
            raise ValueError(form_errors.popitem()[1])  # pick a random error

        if _approved != self.approved:
            if _approved == 0:
                _approved_by = None
            else:
                _approved_by = self.REQUEST.AUTHENTICATED_USER.getUserName()
            self.approveThis(_approved, _approved_by)
        self._p_changed = 1
        if self.discussion:
            self.open_for_comments()
        else:
            self.close_for_comments()
        self.recatalogNyObject(self)
        if REQUEST:
            REQUEST.RESPONSE.redirect('manage_edit_html?save=ok')
Exemple #4
0
    def saveProperties(self, REQUEST=None, **kwargs):
        """ """
        if not self.checkPermissionEditObject():
            raise EXCEPTION_NOTAUTHORIZED(EXCEPTION_NOTAUTHORIZED_MSG)

        if REQUEST is not None:
            schema_raw_data = dict(REQUEST.form)
        else:
            schema_raw_data = kwargs
        _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None))
        _releasedate = self.process_releasedate(
            schema_raw_data.pop('releasedate', ''), self.releasedate)

        if schema_raw_data['iframe_width'] in ['', '0']:
            schema_raw_data['iframe_width'] = 640
        if schema_raw_data['iframe_height'] in ['', '0']:
            schema_raw_data['iframe_height'] = 360

        if len(schema_raw_data['youtube_id']) > 11:
            try:
                schema_raw_data['youtube_id'] = schema_raw_data[
                    'youtube_id'].split('watch?v=')[1][:11]
            except IndexError:
                schema_raw_data['youtube_id'] = schema_raw_data[
                    'youtube_id'].split('&v=')[1][:11]

        form_errors = self.process_submitted_form(
            schema_raw_data, _lang, _override_releasedate=_releasedate)

        try:
            schema_raw_data['iframe_width'] = int(
                schema_raw_data['iframe_width'])
        except ValueError:
            form_errors['iframe_width'] = ['Integer value required.']
        try:
            schema_raw_data['iframe_height'] = int(
                schema_raw_data['iframe_height'])
        except ValueError:
            form_errors['iframe_height'] = ['Integer value required.']
        if not schema_raw_data['youtube_id']:
            form_errors['youtube_id'] = ['Youtube Id is mandatory']

        if schema_raw_data['youtube_id']:
            yt_service = YouTubeService()
            try:
                yt_service.GetYouTubeVideoEntry(
                    video_id=schema_raw_data['youtube_id'])
            except RequestError:
                form_errors['youtube_id'] = [
                    'Invalid Youtube ID (inexisting video)'
                ]

        if not form_errors:
            if self.discussion:
                self.open_for_comments()
            else:
                self.close_for_comments()
            self._p_changed = 1
            self.recatalogNyObject(self)
            # log date
            contributor = self.REQUEST.AUTHENTICATED_USER.getUserName()
            auth_tool = self.getAuthenticationTool()
            auth_tool.changeLastPost(contributor)
            notify(NyContentObjectEditEvent(self, contributor))
            if REQUEST:
                self.setSessionInfoTrans(MESSAGE_SAVEDCHANGES,
                                         date=self.utGetTodayDate())
                REQUEST.RESPONSE.redirect('%s/edit_html?lang=%s' %
                                          (self.absolute_url(), _lang))
        else:
            if REQUEST is not None:
                self._prepare_error_response(REQUEST, form_errors,
                                             schema_raw_data)
                REQUEST.RESPONSE.redirect('%s/edit_html?lang=%s' %
                                          (self.absolute_url(), _lang))
            else:
                raise ValueError(form_errors.popitem()[1])  # pick an error
Exemple #5
0
def addNyYoutube(self, id='', REQUEST=None, contributor=None, **kwargs):
    """
    Create a Youtube embeded video.
    """
    if REQUEST is not None:
        schema_raw_data = dict(REQUEST.form)
    else:
        schema_raw_data = kwargs
    _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None))
    _releasedate = self.process_releasedate(
        schema_raw_data.pop('releasedate', ''))
    recaptcha_response = schema_raw_data.get('g-recaptcha-response', '')

    id = make_id(self,
                 id=id,
                 title=schema_raw_data.get('title', ''),
                 prefix='ep')
    if contributor is None:
        contributor = self.REQUEST.AUTHENTICATED_USER.getUserName()

    ob = _create_NyYoutube_object(self, id, contributor)

    if schema_raw_data['iframe_width'] in ['', '0']:
        schema_raw_data['iframe_width'] = 640
    if schema_raw_data['iframe_height'] in ['', '0']:
        schema_raw_data['iframe_height'] = 360

    if len(schema_raw_data['youtube_id']) > 11:
        try:
            schema_raw_data['youtube_id'] = schema_raw_data[
                'youtube_id'].split('watch?v=')[1][:11]
        except IndexError:
            schema_raw_data['youtube_id'] = schema_raw_data[
                'youtube_id'].split('&v=')[1][:11]

    form_errors = ob.process_submitted_form(schema_raw_data,
                                            _lang,
                                            _override_releasedate=_releasedate)

    try:
        schema_raw_data['iframe_width'] = int(schema_raw_data['iframe_width'])
    except ValueError:
        form_errors['iframe_width'] = ['Integer value required.']
    try:
        schema_raw_data['iframe_height'] = int(
            schema_raw_data['iframe_height'])
    except ValueError:
        form_errors['iframe_height'] = ['Integer value required.']
    if not schema_raw_data['youtube_id']:
        form_errors['youtube_id'] = ['Youtube Id is mandatory']

    if schema_raw_data['youtube_id']:
        yt_service = YouTubeService()
        try:
            yt_service.GetYouTubeVideoEntry(
                video_id=schema_raw_data['youtube_id'])
        except RequestError:
            form_errors['youtube_id'] = [
                'Invalid Youtube ID (inexisting video)'
            ]

    # check Captcha/reCaptcha
    if not self.checkPermissionSkipCaptcha():
        captcha_validator = self.validateCaptcha(recaptcha_response, REQUEST)
        if captcha_validator:
            form_errors['captcha'] = captcha_validator

    if form_errors:
        if REQUEST is None:
            raise ValueError(form_errors.popitem()[1])  # pick a random error
        else:
            import transaction
            transaction.abort()
            # because we already called _crete_NyZzz_object
            ob._prepare_error_response(REQUEST, form_errors, schema_raw_data)
            REQUEST.RESPONSE.redirect('%s/youtube_add_html' %
                                      self.absolute_url())
            return

    # process parameters
    if self.glCheckPermissionPublishObjects():
        approved, approved_by = (1,
                                 self.REQUEST.AUTHENTICATED_USER.getUserName())
    else:
        approved, approved_by = 0, None
    ob.approveThis(approved, approved_by)
    ob.submitThis()

    if ob.discussion:
        ob.open_for_comments()
    self.recatalogNyObject(ob)
    notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data))
    # log post date
    auth_tool = self.getAuthenticationTool()
    auth_tool.changeLastPost(contributor)
    # redirect if case
    if REQUEST is not None:
        l_referer = REQUEST['HTTP_REFERER'].split('/')[-1]
        if (l_referer == 'youtube_manage_add'
                or l_referer.find('youtube_manage_add') != -1):
            return self.manage_main(self, REQUEST, update_menu=1)
        elif l_referer == 'youtube_add_html':
            self.setSession('referer', self.absolute_url())
            return ob.object_submitted_message(REQUEST)
            REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url())

    return ob.getId()