logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

coub.py (4555B)


  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': r're:^https?://.*\.jpg$',
  20. 'duration': 4.6,
  21. 'timestamp': 1428527772,
  22. 'upload_date': '20150408',
  23. 'uploader': 'Artyom Loskutnikov',
  24. 'uploader_id': 'artyom.loskutnikov',
  25. 'view_count': int,
  26. 'like_count': int,
  27. 'repost_count': int,
  28. 'age_limit': 0,
  29. },
  30. }, {
  31. 'url': 'http://c-cdn.coub.com/fb-player.swf?bot_type=vk&coubID=7w5a4',
  32. 'only_matching': True,
  33. }, {
  34. 'url': 'coub:5u5n1',
  35. 'only_matching': True,
  36. }, {
  37. # longer video id
  38. 'url': 'http://coub.com/view/237d5l5h',
  39. 'only_matching': True,
  40. }]
  41. def _real_extract(self, url):
  42. video_id = self._match_id(url)
  43. coub = self._download_json(
  44. 'http://coub.com/api/v2/coubs/%s.json' % video_id, video_id)
  45. if coub.get('error'):
  46. raise ExtractorError(
  47. '%s said: %s' % (self.IE_NAME, coub['error']), expected=True)
  48. title = coub['title']
  49. file_versions = coub['file_versions']
  50. QUALITIES = ('low', 'med', 'high')
  51. MOBILE = 'mobile'
  52. IPHONE = 'iphone'
  53. HTML5 = 'html5'
  54. SOURCE_PREFERENCE = (MOBILE, IPHONE, HTML5)
  55. quality_key = qualities(QUALITIES)
  56. preference_key = qualities(SOURCE_PREFERENCE)
  57. formats = []
  58. for kind, items in file_versions.get(HTML5, {}).items():
  59. if kind not in ('video', 'audio'):
  60. continue
  61. if not isinstance(items, dict):
  62. continue
  63. for quality, item in items.items():
  64. if not isinstance(item, dict):
  65. continue
  66. item_url = item.get('url')
  67. if not item_url:
  68. continue
  69. formats.append({
  70. 'url': item_url,
  71. 'format_id': '%s-%s-%s' % (HTML5, kind, quality),
  72. 'filesize': int_or_none(item.get('size')),
  73. 'vcodec': 'none' if kind == 'audio' else None,
  74. 'quality': quality_key(quality),
  75. 'preference': preference_key(HTML5),
  76. })
  77. iphone_url = file_versions.get(IPHONE, {}).get('url')
  78. if iphone_url:
  79. formats.append({
  80. 'url': iphone_url,
  81. 'format_id': IPHONE,
  82. 'preference': preference_key(IPHONE),
  83. })
  84. mobile_url = file_versions.get(MOBILE, {}).get('audio_url')
  85. if mobile_url:
  86. formats.append({
  87. 'url': mobile_url,
  88. 'format_id': '%s-audio' % MOBILE,
  89. 'preference': preference_key(MOBILE),
  90. })
  91. self._sort_formats(formats)
  92. thumbnail = coub.get('picture')
  93. duration = float_or_none(coub.get('duration'))
  94. timestamp = parse_iso8601(coub.get('published_at') or coub.get('created_at'))
  95. uploader = coub.get('channel', {}).get('title')
  96. uploader_id = coub.get('channel', {}).get('permalink')
  97. view_count = int_or_none(coub.get('views_count') or coub.get('views_increase_count'))
  98. like_count = int_or_none(coub.get('likes_count'))
  99. repost_count = int_or_none(coub.get('recoubs_count'))
  100. age_restricted = coub.get('age_restricted', coub.get('age_restricted_by_admin'))
  101. if age_restricted is not None:
  102. age_limit = 18 if age_restricted is True else 0
  103. else:
  104. age_limit = None
  105. return {
  106. 'id': video_id,
  107. 'title': title,
  108. 'thumbnail': thumbnail,
  109. 'duration': duration,
  110. 'timestamp': timestamp,
  111. 'uploader': uploader,
  112. 'uploader_id': uploader_id,
  113. 'view_count': view_count,
  114. 'like_count': like_count,
  115. 'repost_count': repost_count,
  116. 'age_limit': age_limit,
  117. 'formats': formats,
  118. }