PageRenderTime 97ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/googlecl/youtube/__init__.py

http://googlecl.googlecode.com/
Python | 134 lines | 85 code | 17 blank | 32 comment | 10 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.
  14. import googlecl
  15. import googlecl.base
  16. import re
  17. service_name = __name__.split('.')[-1]
  18. LOGGER_NAME = __name__
  19. SECTION_HEADER = service_name.upper()
  20. class VideoEntryToStringWrapper(googlecl.base.BaseEntryToStringWrapper):
  21. @property
  22. def author(self):
  23. """Author."""
  24. # Name of author 'x' name is in entry.author[x].name.text
  25. text_extractor = lambda entry: getattr(getattr(entry, 'name'), 'text')
  26. return self._join(self.entry.author, text_extractor=text_extractor)
  27. owner = author
  28. @property
  29. def minutes(self):
  30. """Length of the video, in minutes (MM:SS)."""
  31. minutes = int(self.seconds) / 60
  32. seconds = int(self.seconds) % 60
  33. return '%d:%#02d' % (minutes, seconds)
  34. time = minutes
  35. length = minutes
  36. duration = minutes
  37. @property
  38. def seconds(self):
  39. """Length of the video, in seconds."""
  40. return self.entry.media.duration.seconds
  41. @property
  42. def status(self):
  43. print self.xml
  44. """Status of the video."""
  45. if self.entry.control:
  46. # Apparently the structure for video entries isn't fully fleshed out,
  47. # so use a regex on the xml.
  48. xml_string = self.xml
  49. reason_regex = r'<ns1:control .*? name="(\w+)" reasonCode="(\w+)"'
  50. match = re.search(reason_regex, xml_string)
  51. if match:
  52. return '%s (%s)' % (match.group(1), match.group(2))
  53. if self.entry.media.private:
  54. return 'private'
  55. if self.entry.racy:
  56. return 'racy'
  57. else:
  58. # Can't find a difference between public and unlisted videos, in the XML
  59. # or self.entry data structure...
  60. return 'public/unlisted'
  61. @property
  62. def tags(self):
  63. """Tags / keywords or labels."""
  64. tags_text = self.entry.media.keywords.text
  65. tags_text = tags_text.replace(', ', ',')
  66. tags_list = tags_text.split(',')
  67. return self.intra_property_delimiter.join(tags_list)
  68. labels = tags
  69. keywords = tags
  70. #===============================================================================
  71. # Each of the following _run_* functions execute a particular task.
  72. #
  73. # Keyword arguments:
  74. # client: Client to the service being used.
  75. # options: Contains all attributes required to perform the task
  76. # args: Additional arguments passed in on the command line, may or may not be
  77. # required
  78. #===============================================================================
  79. def _run_list(client, options, args):
  80. titles_list = googlecl.build_titles_list(options.title, args)
  81. entries = client.GetVideos(user=options.owner or 'default',
  82. titles=titles_list)
  83. for vid in entries:
  84. print googlecl.base.compile_entry_string(VideoEntryToStringWrapper(vid),
  85. options.fields.split(','),
  86. delimiter=options.delimiter)
  87. def _run_post(client, options, args):
  88. video_list = options.src + args
  89. client.PostVideos(video_list, title=options.title, desc=options.summary,
  90. tags=options.tags, category=options.category,
  91. access=options.access)
  92. def _run_tag(client, options, args):
  93. titles_list = googlecl.build_titles_list(options.title, args)
  94. video_entries = client.GetVideos(titles=titles_list)
  95. if options.category:
  96. client.CategorizeVideos(video_entries, options.category)
  97. if options.tags:
  98. client.TagVideos(video_entries, options.tags)
  99. def _run_delete(client, options, args):
  100. titles_list = googlecl.build_titles_list(options.title, args)
  101. entries = client.GetVideos(titles=titles_list)
  102. client.DeleteEntryList(entries, 'video', options.prompt)
  103. TASKS = {'post': googlecl.base.Task('Post a video.', callback=_run_post,
  104. required=['src', 'category', 'devkey'],
  105. optional=['title', 'summary', 'tags',
  106. 'access']),
  107. 'list': googlecl.base.Task('List videos by user.',
  108. callback=_run_list,
  109. required=['fields', 'delimiter'],
  110. optional=['title', 'owner']),
  111. 'tag': googlecl.base.Task('Add tags to a video and/or ' +\
  112. 'change its category.',
  113. callback=_run_tag,
  114. required=['title', ['tags', 'category'],
  115. 'devkey']),
  116. 'delete': googlecl.base.Task('Delete videos.', callback=_run_delete,
  117. required=['title', 'devkey'])}