kakao.py 5.5 KB

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