Exemplo n.º 1
0
    def tag_videos(self, video_entries, tags):
        """Add or remove tags on a list of videos.

    Keyword arguments:
      video_entries: List of YouTubeVideoEntry objects.
      tags: String representation of tags in a comma separated list. For how
            tags are generated from the string, see
            googlecl.base.generate_tag_sets().

    """
        from gdata.media import Group, Keywords
        remove_set, add_set, replace_tags = googlecl.base.generate_tag_sets(
            tags)
        for video in video_entries:
            if not video.media:
                video.media = Group()
            if not video.media.keywords:
                video.media.keywords = Keywords()

            # No point removing tags if the video has no keywords,
            # or we're replacing the keywords.
            if video.media.keywords.text and remove_set and not replace_tags:
                current_tags = video.media.keywords.text.replace(', ', ',')
                current_set = set(current_tags.split(','))
                video.media.keywords.text = ','.join(current_set - remove_set)

            if replace_tags or not video.media.keywords.text:
                video.media.keywords.text = ','.join(add_set)
            elif add_set:
                video.media.keywords.text += ',' + ','.join(add_set)

            self.UpdateVideoEntry(video)
Exemplo n.º 2
0
    def post_videos(self,
                    paths,
                    category,
                    title=None,
                    desc=None,
                    tags=None,
                    devtags=None,
                    is_private=None):
        """Post video(s) to YouTube.

    Keyword arguments:
      paths: List of paths to videos.
      category: YouTube category for the video.
      title: Title of the video. (Default is the filename of the video).
      desc: Video summary (Default None).
      tags: Tags of the video as a string, separated by commas (Default None).
      devtags: Developer tags for the video (Default None).

    """
        from gdata.media import Group, Title, Description, Keywords, Private
        if isinstance(paths, basestring):
            paths = [paths]
        set_private = lambda private: Private() if private else None
        for path in paths:
            filename = os.path.basename(path).split('.')[0]
            my_media_group = Group(
                title=Title(text=title or filename),
                description=Description(text=desc or 'A video'),
                keywords=Keywords(text=tags),
                category=build_category(category),
                private=set_private(is_private))

            video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group)
            if devtags:
                taglist = devtags.replace(', ', ',')
                taglist = taglist.split(',')
                video_entry.AddDeveloperTags(taglist)
            LOG.info(safe_encode('Loading ' + path))
            try:
                entry = self.InsertVideoEntry(video_entry, path)
            except gdata.service.RequestError, err:
                LOG.error('Failed to upload video: %s' % err)
            except gdata.youtube.service.YouTubeError, err:
                err_str = str(err)
                if err_str.find('path name or a file-like object') != -1:
                    err_str = safe_encode('Could not find file ' + path)
                if (err.args[0]['body'].find('invalid_value') != -1
                        and err.args[0]['body'].find('media:category') != -1):
                    err_str = 'Invalid category: %s' % category
                    err_str += (
                        '\nFor a list of valid categories, see '
                        'http://code.google.com/p/googlecl/wiki/Manual#YouTube'
                    )
                LOG.error(err_str)
Exemplo n.º 3
0
    def tag_photos(self, photo_entries, tags, caption):
        """Add or remove tags on a list of photos.

        Keyword arguments:
          photo_entries: List of photo entry objects.
          tags: String representation of tags in a comma separated list.
                For how tags are generated from the string,
                see googlecl.base.generate_tag_sets(). Set None to leave the tags as
                they currently are.
          caption: New caption for the photo. Set None to leave the caption as it
              is.
        """
        from gdata.media import Group, Keywords
        from atom import Summary
        if tags is not None:
            remove_set, add_set, replace_tags = googlecl.base.generate_tag_sets(
                tags)
        for photo in photo_entries:
            if tags is not None:
                if not photo.media:
                    photo.media = Group()
                if not photo.media.keywords:
                    photo.media.keywords = Keywords()
                # No point removing tags if the photo has no keywords,
                # or we're replacing the keywords.
                if photo.media.keywords.text and remove_set and not replace_tags:
                    current_tags = photo.media.keywords.text.replace(', ', ',')
                    current_set = set(current_tags.split(','))
                    photo.media.keywords.text = ','.join(current_set -
                                                         remove_set)

                if replace_tags or not photo.media.keywords.text:
                    photo.media.keywords.text = ','.join(add_set)
                elif add_set:
                    photo.media.keywords.text += ',' + ','.join(add_set)

            if caption is not None:
                if not photo.summary:
                    photo.summary = Summary(text=caption, summary_type='text')
                else:
                    photo.summary.text = caption

            self.UpdatePhotoMetadata(photo)
Exemplo n.º 4
0
    def post_videos(self,
                    paths,
                    category,
                    title=None,
                    desc=None,
                    tags=None,
                    devtags=None,
                    access=None):
        """Post video(s) to YouTube.

    Keyword arguments:
      paths: List of paths to videos.
      category: YouTube category for the video.
      title: Title of the video. (Default is the filename of the video).
      desc: Video summary (Default None).
      tags: Tags of the video as a string, separated by commas (Default None).
      devtags: Developer tags for the video (Default None).
      access: 'private' or 'unlisted', anything else = 'public'

    """
        from gdata.media import Group, Title, Description, Keywords, Private
        if isinstance(paths, basestring):
            paths = [paths]

        if access is None:
            access = 'public'

        access = access.lower()
        private = None
        if access == 'private':
            private = Private()

        for path in paths:
            filename = os.path.basename(path).split('.')[0]
            my_media_group = Group(
                title=Title(text=title or filename),
                description=Description(text=desc or 'A video'),
                keywords=Keywords(text=tags),
                category=build_category(category),
                private=private)

            if access == 'unlisted':
                extension_elements = [
                    atom.ExtensionElement(
                        'accessControl',
                        namespace=gdata.media.YOUTUBE_NAMESPACE,
                        attributes={
                            'action': 'list',
                            'permission': 'denied'
                        })
                ]
                video_entry = gdata.youtube.YouTubeVideoEntry(
                    media=my_media_group,
                    extension_elements=extension_elements)
            else:
                video_entry = gdata.youtube.YouTubeVideoEntry(
                    media=my_media_group)

            if devtags:
                taglist = devtags.replace(', ', ',')
                taglist = taglist.split(',')
                video_entry.AddDeveloperTags(taglist)
            LOG.info(safe_encode('Loading ' + path))
            try:
                entry = self.InsertVideoEntry(video_entry, path)
            except gdata.service.RequestError, err:
                LOG.error('Failed to upload video: %s' % err)
            except gdata.youtube.service.YouTubeError, err:
                err_str = str(err)
                if err_str.find('path name or a file-like object') != -1:
                    err_str = safe_encode('Could not find file ' + path)
                if (err.args[0]['body'].find('invalid_value') != -1
                        and err.args[0]['body'].find('media:category') != -1):
                    err_str = 'Invalid category: %s' % category
                    err_str += (
                        '\nFor a list of valid categories, see '
                        'http://code.google.com/p/googlecl/wiki/Manual#YouTube'
                    )
                LOG.error(err_str)