kakao.py (5493B)
1 # coding: utf-8 2 3 from __future__ import unicode_literals 4 5 from .common import InfoExtractor 6 from ..compat import compat_HTTPError 7 from ..utils import ( 8 ExtractorError, 9 int_or_none, 10 str_or_none, 11 strip_or_none, 12 try_get, 13 unified_timestamp, 14 update_url_query, 15 ) 16 17 18 class KakaoIE(InfoExtractor): 19 _VALID_URL = r'https?://(?:play-)?tv\.kakao\.com/(?:channel/\d+|embed/player)/cliplink/(?P<id>\d+|[^?#&]+@my)' 20 _API_BASE_TMPL = 'http://tv.kakao.com/api/v1/ft/cliplinks/%s/' 21 22 _TESTS = [{ 23 'url': 'http://tv.kakao.com/channel/2671005/cliplink/301965083', 24 'md5': '702b2fbdeb51ad82f5c904e8c0766340', 25 'info_dict': { 26 'id': '301965083', 27 'ext': 'mp4', 28 'title': '乃木坂46 バナナマン 「3期生紹介コーナーが始動!顔高低差GPも!」 『乃木坂工事中』', 29 'uploader_id': '2671005', 30 'uploader': '그랑그랑이', 31 'timestamp': 1488160199, 32 'upload_date': '20170227', 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 } 47 }, { 48 # geo restricted 49 'url': 'https://tv.kakao.com/channel/3643855/cliplink/412069491', 50 'only_matching': True, 51 }] 52 53 def _real_extract(self, url): 54 video_id = self._match_id(url) 55 display_id = video_id.rstrip('@my') 56 api_base = self._API_BASE_TMPL % video_id 57 58 player_header = { 59 'Referer': update_url_query( 60 'http://tv.kakao.com/embed/player/cliplink/%s' % video_id, { 61 'service': 'kakao_tv', 62 'autoplay': '1', 63 'profile': 'HIGH', 64 'wmode': 'transparent', 65 }) 66 } 67 68 query = { 69 'player': 'monet_html5', 70 'referer': url, 71 'uuid': '', 72 'service': 'kakao_tv', 73 'section': '', 74 'dteType': 'PC', 75 'fields': ','.join([ 76 '-*', 'tid', 'clipLink', 'displayTitle', 'clip', 'title', 77 'description', 'channelId', 'createTime', 'duration', 'playCount', 78 'likeCount', 'commentCount', 'tagList', 'channel', 'name', 'thumbnailUrl', 79 'videoOutputList', 'width', 'height', 'kbps', 'profile', 'label']) 80 } 81 82 impress = self._download_json( 83 api_base + 'impress', display_id, 'Downloading video info', 84 query=query, headers=player_header) 85 86 clip_link = impress['clipLink'] 87 clip = clip_link['clip'] 88 89 title = clip.get('title') or clip_link.get('displayTitle') 90 91 query.update({ 92 'fields': '-*,code,message,url', 93 'tid': impress.get('tid') or '', 94 }) 95 96 formats = [] 97 for fmt in (clip.get('videoOutputList') or []): 98 try: 99 profile_name = fmt['profile'] 100 if profile_name == 'AUDIO': 101 continue 102 query['profile'] = profile_name 103 try: 104 fmt_url_json = self._download_json( 105 api_base + 'raw/videolocation', display_id, 106 'Downloading video URL for profile %s' % profile_name, 107 query=query, headers=player_header) 108 except ExtractorError as e: 109 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: 110 resp = self._parse_json(e.cause.read().decode(), video_id) 111 if resp.get('code') == 'GeoBlocked': 112 self.raise_geo_restricted() 113 continue 114 115 fmt_url = fmt_url_json['url'] 116 formats.append({ 117 'url': fmt_url, 118 'format_id': profile_name, 119 'width': int_or_none(fmt.get('width')), 120 'height': int_or_none(fmt.get('height')), 121 'format_note': fmt.get('label'), 122 'filesize': int_or_none(fmt.get('filesize')), 123 'tbr': int_or_none(fmt.get('kbps')), 124 }) 125 except KeyError: 126 pass 127 self._sort_formats(formats) 128 129 return { 130 'id': display_id, 131 'title': title, 132 'description': strip_or_none(clip.get('description')), 133 'uploader': try_get(clip_link, lambda x: x['channel']['name']), 134 'uploader_id': str_or_none(clip_link.get('channelId')), 135 'thumbnail': clip.get('thumbnailUrl'), 136 'timestamp': unified_timestamp(clip_link.get('createTime')), 137 'duration': int_or_none(clip.get('duration')), 138 'view_count': int_or_none(clip.get('playCount')), 139 'like_count': int_or_none(clip.get('likeCount')), 140 'comment_count': int_or_none(clip.get('commentCount')), 141 'formats': formats, 142 'tags': clip.get('tagList'), 143 }