Пример #1
0
    def _create_video_entry(self, title, description, category, keywords=None,
            location=None, private=False, unlisted=False, recorded=None, 
            nocomments=False, noratings=False):
        self.categories = self.get_categories()
        if category not in self.categories:
            valid = " ".join(self.categories.keys())
            raise InvalidCategory("Invalid category '%s' (valid: %s)" % (category, valid))
        media_group = gdata.media.Group(
            title=gdata.media.Title(text=title),
            description=gdata.media.Description(description_type='plain', text=description),
            keywords=gdata.media.Keywords(text=keywords),
            category=gdata.media.Category(
                text=category,
                label=self.categories[category],
                scheme=self.CATEGORIES_SCHEME),
            private=(gdata.media.Private() if private else None),
            player=None)
        
        if location:
            where = gdata.geo.Where()
            where.set_location(location)
        else:
            where = None
        
        extensions = []
        
        if unlisted:
            list_denied = {
                "namespace": YOUTUBE_NAMESPACE,
                "attributes": {'action': 'list', 'permission': 'denied'},
            }
            extensions.append(ExtensionElement('accessControl', **list_denied))

        if nocomments:
            comment_denied = {
                "namespace": YOUTUBE_NAMESPACE,
                "attributes": {'action': 'comment', 'permission': 'denied'},
            }
            extensions.append(ExtensionElement('accessControl', **comment_denied))

        if noratings:
            rate_denied = {
                "namespace": YOUTUBE_NAMESPACE,
                "attributes": {'action': 'rate', 'permission': 'denied'},
            }
            extensions.append(ExtensionElement('accessControl', **rate_denied))
        when = (gdata.youtube.Recorded(None, None, recorded) if recorded else None)
        return gdata.youtube.YouTubeVideoEntry(media=media_group, geo=where,
            recorded=when, extension_elements=extensions)
Пример #2
0
    def _access_control(self, access_control, my_media_group=None):
        """
        Prepares the extension element for access control
        Extension element is the optional parameter for the YouTubeVideoEntry
        We use extension element to modify access control settings

        Returns:
            tuple of extension elements
        """
        # Access control
        extension = None
        if access_control is AccessControl.Private:
            # WARNING: this part of code is not tested
            # set video as private
            if my_media_group:
                my_media_group.private = gdata.media.Private()
        elif access_control is AccessControl.Unlisted:
            # set video as unlisted
            from gdata.media import YOUTUBE_NAMESPACE
            from atom import ExtensionElement
            kwargs = {
                "namespace": YOUTUBE_NAMESPACE,
                "attributes": {
                    'action': 'list',
                    'permission': 'denied'
                },
            }
            extension = ([ExtensionElement('accessControl', **kwargs)])
        return extension
Пример #3
0
def oauth(request):
    if 'code' in request.GET:
        code = request.GET.get('code')
        client_id = settings.GOOGLE_CLIENT_ID
        client_secret = settings.GOOGLE_CLIENT_SECRET
        redirect_uri = "http://" + request.META['HTTP_HOST'] + "/oauth2callback"

        post_data = [('code', code), ('client_id', client_id),
                     ('client_secret', client_secret),
                     ('redirect_uri', redirect_uri),
                     ('grant_type', 'authorization_code')]
        result = urllib2.urlopen('https://accounts.google.com/o/oauth2/token',
                                 urllib.urlencode(post_data))
        content = json.loads(result.read())

        yt_service = gdata.youtube.service.YouTubeService(
            additional_headers={
                'Authorization': "Bearer " + content['access_token']
            })
        yt_service.developer_key = settings.YT_SERVICE_DEVELOPER_KEY

        video = Video.objects.get(pk=request.GET.get('state'))

        my_media_group = gdata.media.Group(
            title=gdata.media.Title(text=video.title),
            description=gdata.media.Description(description_type='plain',
                                                text=video.description),
            category=[
                gdata.media.Category(text='Education', label='Education')
            ],
        )

        if request.session['video_privacy'] == "public":
            video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group)
        else:
            #upload as unlisted
            extension = ExtensionElement('accessControl',
                                         namespace=YOUTUBE_NAMESPACE,
                                         attributes={
                                             'action': 'list',
                                             'permission': 'denied'
                                         })
            video_entry = gdata.youtube.YouTubeVideoEntry(
                media=my_media_group, extension_elements=[extension])

        video.file.len = video.file.size  # monkeypatch bug in InsertVideoEntry
        entry = yt_service.InsertVideoEntry(video_entry, video.file)
        match = re.search(
            'http://gdata.youtube.com/feeds/api/videos/([a-zA-Z0-9_-]+)</ns0:id>',
            entry.id.ToString())
        video.url = match.group(1)
        video.duration = entry.media.duration.seconds
        video.save()
        video.image.url = video.url
        video.image.duration = video.duration
        video.image.save()

        parts = str(video.handle).split("--")
        return HttpResponseRedirect(
            reverse('courses.videos.views.list', args=(parts[0], parts[1])))
Пример #4
0
    def set_permission(self, url, permission="allowed"):
        # docs
        # https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:accessControl

        yt_service = self.auth()

        video_id = self.get_id_from_url(url)
        # uri= 'http://gdata.youtube.com/feeds/api/users/default/uploads/%s' % (id,)
        # entry = yt_service.GetYouTubeVideoEntry(uri=uri)
        entry = yt_service.GetYouTubeVideoEntry(video_id=video_id)

        entry.extension_elements = [
            ExtensionElement('accessControl',
                             namespace=YOUTUBE_NAMESPACE,
                             attributes={
                                 'action': 'list',
                                 'permission': permission
                             })
        ]

        updated_entry = yt_service.UpdateVideoEntry(entry)

        if self.debug_mode:
            import code
            code.interact(local=locals())

        return True
Пример #5
0
    def upload(self):

        yt_service = self.auth()

        if self.unlisted:
            acl = [
                ExtensionElement('accessControl',
                                 namespace=YOUTUBE_NAMESPACE,
                                 attributes={
                                     'action': 'list',
                                     'permission': 'denied'
                                 })
            ]
        else:
            acl = []

        video_entry = gdata.youtube.YouTubeVideoEntry(media=self.media_group(),
                                                      extension_elements=acl)

        if self.meta.has_key('latlon'):
            video_entry.geo = self.geo()

        # add some more metadata -  more tags
        # tags = self.meta['tags']
        # tags = [tag for tag in tags if " " not in tag]
        # https://developers.google.com/youtube/2.0/developers_guide_protocol#Assigning_Developer_Tags
        # video_entry.AddDeveloperTags(tags)

        pathname = self.files[0]['pathname']

        pf = ProgressFile(pathname, 'r')
        try:
            # down to the next moduel layer to upload
            self.new_entry = yt_service.InsertVideoEntry(video_entry, pf)

            self.ret_text = self.new_entry.__str__()
            link = self.new_entry.GetHtmlLink()
            self.new_url = link.href.split('&')[0]
            ret = True

        except gdata.youtube.service.YouTubeError, e:
            self.ret_text = 'request: %s\nerror: %s' % (video_entry.ToString(),
                                                        e.__str__())
            ret = False
            print "e:", e
            if self.debug_mode:
                import code
                code.interact(local=locals())