PageRenderTime 87ms CodeModel.GetById 68ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/googlecl/youtube/__init__.py

http://googlecl.googlecode.com/
Python | 134 lines | 85 code | 17 blank | 32 comment | 9 complexity | 0df7eed656140ebbcb68d8fe90f3cde4 MD5 | raw file
  1# Copyright (C) 2010 Google Inc.
  2#
  3# Licensed under the Apache License, Version 2.0 (the "License");
  4# you may not use this file except in compliance with the License.
  5# You may obtain a copy of the License at
  6#
  7#      http://www.apache.org/licenses/LICENSE-2.0
  8#
  9# Unless required by applicable law or agreed to in writing, software
 10# distributed under the License is distributed on an "AS IS" BASIS,
 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12# See the License for the specific language governing permissions and
 13# limitations under the License.
 14import googlecl
 15import googlecl.base
 16import re
 17
 18service_name = __name__.split('.')[-1]
 19LOGGER_NAME = __name__
 20SECTION_HEADER = service_name.upper()
 21
 22
 23class VideoEntryToStringWrapper(googlecl.base.BaseEntryToStringWrapper):
 24  @property
 25  def author(self):
 26    """Author."""
 27    # Name of author 'x' name is in entry.author[x].name.text
 28    text_extractor = lambda entry: getattr(getattr(entry, 'name'), 'text')
 29    return self._join(self.entry.author, text_extractor=text_extractor)
 30  owner = author
 31
 32  @property
 33  def minutes(self):
 34    """Length of the video, in minutes (MM:SS)."""
 35    minutes = int(self.seconds) / 60
 36    seconds = int(self.seconds) % 60
 37    return '%d:%#02d' % (minutes, seconds)
 38  time = minutes
 39  length = minutes
 40  duration = minutes
 41
 42  @property
 43  def seconds(self):
 44    """Length of the video, in seconds."""
 45    return self.entry.media.duration.seconds
 46
 47  @property
 48  def status(self):
 49    print self.xml
 50    """Status of the video."""
 51    if self.entry.control:
 52      # Apparently the structure for video entries isn't fully fleshed out,
 53      # so use a regex on the xml.
 54      xml_string = self.xml
 55      reason_regex = r'<ns1:control .*? name="(\w+)" reasonCode="(\w+)"'
 56      match = re.search(reason_regex, xml_string)
 57      if match:
 58        return '%s (%s)' % (match.group(1), match.group(2))
 59    if self.entry.media.private:
 60      return 'private'
 61    if self.entry.racy:
 62      return 'racy'
 63    else:
 64      # Can't find a difference between public and unlisted videos, in the XML
 65      # or self.entry data structure...
 66      return 'public/unlisted'
 67
 68  @property
 69  def tags(self):
 70    """Tags / keywords or labels."""
 71    tags_text = self.entry.media.keywords.text
 72    tags_text = tags_text.replace(', ', ',')
 73    tags_list = tags_text.split(',')
 74    return self.intra_property_delimiter.join(tags_list)
 75  labels = tags
 76  keywords = tags
 77
 78
 79#===============================================================================
 80# Each of the following _run_* functions execute a particular task.
 81#
 82# Keyword arguments:
 83#  client: Client to the service being used.
 84#  options: Contains all attributes required to perform the task
 85#  args: Additional arguments passed in on the command line, may or may not be
 86#        required
 87#===============================================================================
 88def _run_list(client, options, args):
 89  titles_list = googlecl.build_titles_list(options.title, args)
 90  entries = client.GetVideos(user=options.owner or 'default',
 91                             titles=titles_list)
 92  for vid in entries:
 93    print googlecl.base.compile_entry_string(VideoEntryToStringWrapper(vid),
 94                                             options.fields.split(','),
 95                                             delimiter=options.delimiter)
 96
 97
 98def _run_post(client, options, args):
 99  video_list = options.src + args
100  client.PostVideos(video_list, title=options.title, desc=options.summary,
101                    tags=options.tags, category=options.category,
102                    access=options.access)
103
104
105def _run_tag(client, options, args):
106  titles_list = googlecl.build_titles_list(options.title, args)
107  video_entries = client.GetVideos(titles=titles_list)
108  if options.category:
109    client.CategorizeVideos(video_entries, options.category)
110  if options.tags:
111    client.TagVideos(video_entries, options.tags)
112
113
114def _run_delete(client, options, args):
115  titles_list = googlecl.build_titles_list(options.title, args)
116  entries = client.GetVideos(titles=titles_list)
117  client.DeleteEntryList(entries, 'video', options.prompt)
118
119
120TASKS = {'post': googlecl.base.Task('Post a video.', callback=_run_post,
121                                    required=['src', 'category', 'devkey'],
122                                    optional=['title', 'summary', 'tags',
123                                              'access']),
124         'list': googlecl.base.Task('List videos by user.',
125                                    callback=_run_list,
126                                    required=['fields', 'delimiter'],
127                                    optional=['title', 'owner']),
128         'tag': googlecl.base.Task('Add tags to a video and/or ' +\
129                                   'change its category.',
130                                   callback=_run_tag,
131                                   required=['title', ['tags', 'category'],
132                                             'devkey']),
133         'delete': googlecl.base.Task('Delete videos.', callback=_run_delete,
134                                      required=['title', 'devkey'])}