You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.6 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. float_or_none,
  7. int_or_none,
  8. parse_iso8601,
  9. qualities,
  10. )
  11. class CoubIE(InfoExtractor):
  12. _VALID_URL = r'(?:coub:|https?://(?:coub\.com/(?:view|embed|coubs)/|c-cdn\.coub\.com/fb-player\.swf\?.*\bcoub(?:ID|id)=))(?P<id>[\da-z]+)'
  13. _TESTS = [{
  14. 'url': 'http://coub.com/view/5u5n1',
  15. 'info_dict': {
  16. 'id': '5u5n1',
  17. 'ext': 'mp4',
  18. 'title': 'The Matrix Moonwalk',
  19. 'thumbnail': 're:^https?://.*\.jpg$',
  20. 'duration': 4.6,
  21. 'timestamp': 1428527772,
  22. 'upload_date': '20150408',
  23. 'uploader': 'Артём Лоскутников',
  24. 'uploader_id': 'artyom.loskutnikov',
  25. 'view_count': int,
  26. 'like_count': int,
  27. 'repost_count': int,
  28. 'comment_count': int,
  29. 'age_limit': 0,
  30. },
  31. }, {
  32. 'url': 'http://c-cdn.coub.com/fb-player.swf?bot_type=vk&coubID=7w5a4',
  33. 'only_matching': True,
  34. }, {
  35. 'url': 'coub:5u5n1',
  36. 'only_matching': True,
  37. }, {
  38. # longer video id
  39. 'url': 'http://coub.com/view/237d5l5h',
  40. 'only_matching': True,
  41. }]
  42. def _real_extract(self, url):
  43. video_id = self._match_id(url)
  44. coub = self._download_json(
  45. 'http://coub.com/api/v2/coubs/%s.json' % video_id, video_id)
  46. if coub.get('error'):
  47. raise ExtractorError(
  48. '%s said: %s' % (self.IE_NAME, coub['error']), expected=True)
  49. title = coub['title']
  50. file_versions = coub['file_versions']
  51. QUALITIES = ('low', 'med', 'high')
  52. MOBILE = 'mobile'
  53. IPHONE = 'iphone'
  54. HTML5 = 'html5'
  55. SOURCE_PREFERENCE = (MOBILE, IPHONE, HTML5)
  56. quality_key = qualities(QUALITIES)
  57. preference_key = qualities(SOURCE_PREFERENCE)
  58. formats = []
  59. for kind, items in file_versions.get(HTML5, {}).items():
  60. if kind not in ('video', 'audio'):
  61. continue
  62. if not isinstance(items, dict):
  63. continue
  64. for quality, item in items.items():
  65. if not isinstance(item, dict):
  66. continue
  67. item_url = item.get('url')
  68. if not item_url:
  69. continue
  70. formats.append({
  71. 'url': item_url,
  72. 'format_id': '%s-%s-%s' % (HTML5, kind, quality),
  73. 'filesize': int_or_none(item.get('size')),
  74. 'vcodec': 'none' if kind == 'audio' else None,
  75. 'quality': quality_key(quality),
  76. 'preference': preference_key(HTML5),
  77. })
  78. iphone_url = file_versions.get(IPHONE, {}).get('url')
  79. if iphone_url:
  80. formats.append({
  81. 'url': iphone_url,
  82. 'format_id': IPHONE,
  83. 'preference': preference_key(IPHONE),
  84. })
  85. mobile_url = file_versions.get(MOBILE, {}).get('audio_url')
  86. if mobile_url:
  87. formats.append({
  88. 'url': mobile_url,
  89. 'format_id': '%s-audio' % MOBILE,
  90. 'preference': preference_key(MOBILE),
  91. })
  92. self._sort_formats(formats)
  93. thumbnail = coub.get('picture')
  94. duration = float_or_none(coub.get('duration'))
  95. timestamp = parse_iso8601(coub.get('published_at') or coub.get('created_at'))
  96. uploader = coub.get('channel', {}).get('title')
  97. uploader_id = coub.get('channel', {}).get('permalink')
  98. view_count = int_or_none(coub.get('views_count') or coub.get('views_increase_count'))
  99. like_count = int_or_none(coub.get('likes_count'))
  100. repost_count = int_or_none(coub.get('recoubs_count'))
  101. comment_count = int_or_none(coub.get('comments_count'))
  102. age_restricted = coub.get('age_restricted', coub.get('age_restricted_by_admin'))
  103. if age_restricted is not None:
  104. age_limit = 18 if age_restricted is True else 0
  105. else:
  106. age_limit = None
  107. return {
  108. 'id': video_id,
  109. 'title': title,
  110. 'thumbnail': thumbnail,
  111. 'duration': duration,
  112. 'timestamp': timestamp,
  113. 'uploader': uploader,
  114. 'uploader_id': uploader_id,
  115. 'view_count': view_count,
  116. 'like_count': like_count,
  117. 'repost_count': repost_count,
  118. 'comment_count': comment_count,
  119. 'age_limit': age_limit,
  120. 'formats': formats,
  121. }