kakao.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from .common import InfoExtractor
  2. from ..networking.exceptions import HTTPError
  3. from ..utils import (
  4. ExtractorError,
  5. int_or_none,
  6. str_or_none,
  7. strip_or_none,
  8. traverse_obj,
  9. unified_timestamp,
  10. )
  11. class KakaoIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:play-)?tv\.kakao\.com/(?:channel/\d+|embed/player)/cliplink/(?P<id>\d+|[^?#&]+@my)'
  13. _API_BASE_TMPL = 'http://tv.kakao.com/api/v1/ft/playmeta/cliplink/%s/'
  14. _CDN_API = 'https://tv.kakao.com/katz/v1/ft/cliplink/%s/readyNplay?'
  15. _TESTS = [{
  16. 'url': 'http://tv.kakao.com/channel/2671005/cliplink/301965083',
  17. 'md5': '702b2fbdeb51ad82f5c904e8c0766340',
  18. 'info_dict': {
  19. 'id': '301965083',
  20. 'ext': 'mp4',
  21. 'title': '乃木坂46 バナナマン 「3期生紹介コーナーが始動!顔高低差GPも!」 『乃木坂工事中』',
  22. 'description': '',
  23. 'uploader_id': '2671005',
  24. 'uploader': '그랑그랑이',
  25. 'timestamp': 1488160199,
  26. 'upload_date': '20170227',
  27. 'like_count': int,
  28. 'thumbnail': r're:http://.+/thumb\.png',
  29. 'tags': ['乃木坂'],
  30. 'view_count': int,
  31. 'duration': 1503,
  32. 'comment_count': int,
  33. },
  34. }, {
  35. 'url': 'http://tv.kakao.com/channel/2653210/cliplink/300103180',
  36. 'md5': 'a8917742069a4dd442516b86e7d66529',
  37. 'info_dict': {
  38. 'id': '300103180',
  39. 'ext': 'mp4',
  40. 'description': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)\r\n\r\n[쇼! 음악중심] 20160611, 507회',
  41. 'title': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)',
  42. 'uploader_id': '2653210',
  43. 'uploader': '쇼! 음악중심',
  44. 'timestamp': 1485684628,
  45. 'upload_date': '20170129',
  46. 'like_count': int,
  47. 'thumbnail': r're:http://.+/thumb\.png',
  48. 'tags': 'count:28',
  49. 'view_count': int,
  50. 'duration': 184,
  51. 'comment_count': int,
  52. },
  53. }, {
  54. # geo restricted
  55. 'url': 'https://tv.kakao.com/channel/3643855/cliplink/412069491',
  56. 'only_matching': True,
  57. }]
  58. def _real_extract(self, url):
  59. video_id = self._match_id(url)
  60. api_base = self._API_BASE_TMPL % video_id
  61. cdn_api_base = self._CDN_API % video_id
  62. query = {
  63. 'player': 'monet_html5',
  64. 'referer': url,
  65. 'uuid': '',
  66. 'service': 'kakao_tv',
  67. 'section': '',
  68. 'dteType': 'PC',
  69. 'fields': ','.join([
  70. '-*', 'tid', 'clipLink', 'displayTitle', 'clip', 'title',
  71. 'description', 'channelId', 'createTime', 'duration', 'playCount',
  72. 'likeCount', 'commentCount', 'tagList', 'channel', 'name',
  73. 'clipChapterThumbnailList', 'thumbnailUrl', 'timeInSec', 'isDefault',
  74. 'videoOutputList', 'width', 'height', 'kbps', 'profile', 'label']),
  75. }
  76. api_json = self._download_json(
  77. api_base, video_id, 'Downloading video info')
  78. clip_link = api_json['clipLink']
  79. clip = clip_link['clip']
  80. title = clip.get('title') or clip_link.get('displayTitle')
  81. formats = []
  82. for fmt in clip.get('videoOutputList') or []:
  83. profile_name = fmt.get('profile')
  84. if not profile_name or profile_name == 'AUDIO':
  85. continue
  86. query.update({
  87. 'profile': profile_name,
  88. 'fields': '-*,code,message,url',
  89. })
  90. try:
  91. fmt_url_json = self._download_json(
  92. cdn_api_base, video_id, query=query,
  93. note=f'Downloading video URL for profile {profile_name}')
  94. except ExtractorError as e:
  95. if isinstance(e.cause, HTTPError) and e.cause.status == 403:
  96. resp = self._parse_json(e.cause.response.read().decode(), video_id)
  97. if resp.get('code') == 'GeoBlocked':
  98. self.raise_geo_restricted()
  99. raise
  100. fmt_url = traverse_obj(fmt_url_json, ('videoLocation', 'url'))
  101. if not fmt_url:
  102. continue
  103. formats.append({
  104. 'url': fmt_url,
  105. 'format_id': profile_name,
  106. 'width': int_or_none(fmt.get('width')),
  107. 'height': int_or_none(fmt.get('height')),
  108. 'format_note': fmt.get('label'),
  109. 'filesize': int_or_none(fmt.get('filesize')),
  110. 'tbr': int_or_none(fmt.get('kbps')),
  111. })
  112. thumbs = []
  113. for thumb in clip.get('clipChapterThumbnailList') or []:
  114. thumbs.append({
  115. 'url': thumb.get('thumbnailUrl'),
  116. 'id': str(thumb.get('timeInSec')),
  117. 'preference': -1 if thumb.get('isDefault') else 0,
  118. })
  119. top_thumbnail = clip.get('thumbnailUrl')
  120. if top_thumbnail:
  121. thumbs.append({
  122. 'url': top_thumbnail,
  123. 'preference': 10,
  124. })
  125. return {
  126. 'id': video_id,
  127. 'title': title,
  128. 'description': strip_or_none(clip.get('description')),
  129. 'uploader': traverse_obj(clip_link, ('channel', 'name')),
  130. 'uploader_id': str_or_none(clip_link.get('channelId')),
  131. 'thumbnails': thumbs,
  132. 'timestamp': unified_timestamp(clip_link.get('createTime')),
  133. 'duration': int_or_none(clip.get('duration')),
  134. 'view_count': int_or_none(clip.get('playCount')),
  135. 'like_count': int_or_none(clip.get('likeCount')),
  136. 'comment_count': int_or_none(clip.get('commentCount')),
  137. 'formats': formats,
  138. 'tags': clip.get('tagList'),
  139. }