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.

149 lines
5.2 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. int_or_none,
  7. unified_timestamp,
  8. update_url_query,
  9. )
  10. class KakaoIE(InfoExtractor):
  11. _VALID_URL = r'https?://tv\.kakao\.com/channel/(?P<channel>\d+)/cliplink/(?P<id>\d+)'
  12. _API_BASE = 'http://tv.kakao.com/api/v1/ft/cliplinks'
  13. _TESTS = [{
  14. 'url': 'http://tv.kakao.com/channel/2671005/cliplink/301965083',
  15. 'md5': '702b2fbdeb51ad82f5c904e8c0766340',
  16. 'info_dict': {
  17. 'id': '301965083',
  18. 'ext': 'mp4',
  19. 'title': '乃木坂46 バナナマン 「3期生紹介コーナーが始動!顔高低差GPも!」 『乃木坂工事中』',
  20. 'uploader_id': 2671005,
  21. 'uploader': '그랑그랑이',
  22. 'timestamp': 1488160199,
  23. 'upload_date': '20170227',
  24. }
  25. }, {
  26. 'url': 'http://tv.kakao.com/channel/2653210/cliplink/300103180',
  27. 'md5': 'a8917742069a4dd442516b86e7d66529',
  28. 'info_dict': {
  29. 'id': '300103180',
  30. 'ext': 'mp4',
  31. 'description': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)\r\n\r\n[쇼! 음악중심] 20160611, 507회',
  32. 'title': '러블리즈 - Destiny (나의 지구) (Lovelyz - Destiny)',
  33. 'uploader_id': 2653210,
  34. 'uploader': '쇼 음악중심',
  35. 'timestamp': 1485684628,
  36. 'upload_date': '20170129',
  37. }
  38. }]
  39. def _real_extract(self, url):
  40. video_id = self._match_id(url)
  41. player_header = {
  42. 'Referer': update_url_query(
  43. 'http://tv.kakao.com/embed/player/cliplink/%s' % video_id, {
  44. 'service': 'kakao_tv',
  45. 'autoplay': '1',
  46. 'profile': 'HIGH',
  47. 'wmode': 'transparent',
  48. })
  49. }
  50. QUERY_COMMON = {
  51. 'player': 'monet_html5',
  52. 'referer': url,
  53. 'uuid': '',
  54. 'service': 'kakao_tv',
  55. 'section': '',
  56. 'dteType': 'PC',
  57. }
  58. query = QUERY_COMMON.copy()
  59. query['fields'] = 'clipLink,clip,channel,hasPlusFriend,-service,-tagList'
  60. impress = self._download_json(
  61. '%s/%s/impress' % (self._API_BASE, video_id),
  62. video_id, 'Downloading video info',
  63. query=query, headers=player_header)
  64. clip_link = impress['clipLink']
  65. clip = clip_link['clip']
  66. title = clip.get('title') or clip_link.get('displayTitle')
  67. tid = impress.get('tid', '')
  68. query = QUERY_COMMON.copy()
  69. query.update({
  70. 'tid': tid,
  71. 'profile': 'HIGH',
  72. })
  73. raw = self._download_json(
  74. '%s/%s/raw' % (self._API_BASE, video_id),
  75. video_id, 'Downloading video formats info',
  76. query=query, headers=player_header)
  77. formats = []
  78. for fmt in raw.get('outputList', []):
  79. try:
  80. profile_name = fmt['profile']
  81. fmt_url_json = self._download_json(
  82. '%s/%s/raw/videolocation' % (self._API_BASE, video_id),
  83. video_id,
  84. 'Downloading video URL for profile %s' % profile_name,
  85. query={
  86. 'service': 'kakao_tv',
  87. 'section': '',
  88. 'tid': tid,
  89. 'profile': profile_name
  90. }, headers=player_header, fatal=False)
  91. if fmt_url_json is None:
  92. continue
  93. fmt_url = fmt_url_json['url']
  94. formats.append({
  95. 'url': fmt_url,
  96. 'format_id': profile_name,
  97. 'width': int_or_none(fmt.get('width')),
  98. 'height': int_or_none(fmt.get('height')),
  99. 'format_note': fmt.get('label'),
  100. 'filesize': int_or_none(fmt.get('filesize'))
  101. })
  102. except KeyError:
  103. pass
  104. self._sort_formats(formats)
  105. thumbs = []
  106. for thumb in clip.get('clipChapterThumbnailList', []):
  107. thumbs.append({
  108. 'url': thumb.get('thumbnailUrl'),
  109. 'id': compat_str(thumb.get('timeInSec')),
  110. 'preference': -1 if thumb.get('isDefault') else 0
  111. })
  112. top_thumbnail = clip.get('thumbnailUrl')
  113. if top_thumbnail:
  114. thumbs.append({
  115. 'url': top_thumbnail,
  116. 'preference': 10,
  117. })
  118. return {
  119. 'id': video_id,
  120. 'title': title,
  121. 'description': clip.get('description'),
  122. 'uploader': clip_link.get('channel', {}).get('name'),
  123. 'uploader_id': clip_link.get('channelId'),
  124. 'thumbnails': thumbs,
  125. 'timestamp': unified_timestamp(clip_link.get('createTime')),
  126. 'duration': int_or_none(clip.get('duration')),
  127. 'view_count': int_or_none(clip.get('playCount')),
  128. 'like_count': int_or_none(clip.get('likeCount')),
  129. 'comment_count': int_or_none(clip.get('commentCount')),
  130. 'formats': formats,
  131. }